darryncampbell / react-native-datawedge-intents

React Native interface for Zebra's DataWedge Intent API
MIT License
43 stars 46 forks source link

Register for Notifications #28

Closed MartinMasek closed 2 years ago

MartinMasek commented 2 years ago

HI Darryn,

Thank you for the package, it makes the life easier :)

I wanted to try to register for notifications as described in the Zebra documentation here https://techdocs.zebra.com/datawedge/11-2/guide/api/registerfornotification/

I'm not able to figure out what code to write. I tried this but with no success. Would you be so kind as to provide an example how to do it please?

            var broadcastExtras: any = {};
            broadcastExtras[
              "com.symbol.datawedge.api.REGISTER_FOR_NOTIFICATION"
            ] = {
              APPLICATION_NAME: "com.demodw",
              "com.symbol.datawedge.api.NOTIFICATION_TYPE": "SCANNER_STATUS",
            };
            broadcastExtras["SEND_RESULT"] = "true";
            DataWedgeIntents.sendBroadcastWithExtras({
              action: "com.symbol.datawedge.api.ACTION",
              extras: broadcastExtras,
            });
darryncampbell commented 2 years ago

Hi @MartinMasek , sure, please see this app: https://github.com/darryncampbell/DataWedge-Quick-Suspend-Scanner

MartinMasek commented 2 years ago

Thanks for responding so quick @darryncampbell It seems like it's Android project not React-native. Is there a way to do it in React-native or am I overlooking something?

darryncampbell commented 2 years ago

Fair point, I don't have anything in React Native but it would look something like:

var broadcastExtras = {};
var notificationExtras = {};
notificationExtras["com.symbol.datawedge.api.APPLICATION_NAME"] = getPackageName();
notificationExtras["NOTIFICATION_TYPE"] = "SCANNER_STATUS";
broadcastExtras[""com.symbol.datawedge.api.REGISTER_FOR_NOTIFICATION"] = notificationExtras;
broadcastExtras["SEND_RESULT"] = this.sendCommandResult;
DataWedgeIntents.sendBroadcastWithExtras({
    action: "com.symbol.datawedge.api.ACTION",
    extras: broadcastExtras});

I know that code probably is not great, I just copy / pasted one of my old examples and modified where needed but hopefully conveys the idea.

You will also need to modify https://github.com/darryncampbell/react-native-datawedge-intents/blob/master/android/src/main/java/com/darryncampbell/rndatawedgeintents/RNDataWedgeIntentsModule.java#L107 to add the filter for notifications, https://github.com/darryncampbell/DataWedge-Quick-Suspend-Scanner/blob/master/app/src/main/java/com/darryncampbell/datawedgequicksuspendscanner/MainActivity.java#L63

MartinMasek commented 2 years ago

Thanks @darryncampbell. I've used react-native-ble-manager in the end to listen to the bond event and got information from there. Here is a code sample if anyone had the similar goal.

const BleManagerModule = NativeModules.BleManager
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule)

...

useEffect(() => {
          bleManagerEmitter.addListener('BleManagerPeripheralDidBond', (args) => {
            console.log('>>> BleManagerPeripheralDidBond ')
            console.log(args)
        })
})