ibm-bluemix-mobile-services / bms-clientsdk-cordova-plugin-push

Cordova Push notifications Plugin for IBM Cloud Mobile Services
Apache License 2.0
9 stars 9 forks source link

android, Error in Success callbackId: BMSPushClient973699422 : SyntaxError: Unexpected token o in JSON at position 1 #92

Closed andaqui closed 6 years ago

andaqui commented 6 years ago

Im trying to get this working with android device. Its seems to register the device but when i send a notification through ibm cloud and the device recieves the push i get the following error log in android studio. So the push notification never gets to the js event handler.

"Error in Success callbackId: BMSPushClient973699422 : SyntaxError: Unexpected token o in JSON at position 1", source: file:///android_asset/www/cordova.js (309)"

Help appreciated.

AnanthaKrish commented 6 years ago

@kumpanen Can you please share your JS file ?

andaqui commented 6 years ago
    /**
     * Set up constants and require the bms cordova plugins
     */
    const bmsAppGuid = constants.bmsAppGuid;
    const bmsClientSecret = constants.bmsClientSecret;

    /**
     * Initialize the core plugin & Initialize the Push plugin
     * Should be called onDeviceReady
     */
    function init(){
      const bmsClient = cordova.require('bms-core.BMSClient');
      const bmsPush = cordova.require('bms-push.BMSPush');
      bmsClient.initialize(bmsClient.REGION_GERMANY);
      const options = {};
      bmsPush.initialize(bmsAppGuid, bmsClientSecret, options);
    }

    /**
     * Register the device for bms push
     * If success save device token to backend and set handle notification listener
     */
    function registerDevice(){
      const bmsPush = cordova.require('bms-push.BMSPush');
      const success = function(response) {
        console.log('Success: ' + response);
        _registerNotification();
        $localStorage.bmsDeviceId = JSON.parse(response).deviceId;
        $localStorage.bmsDeviceToken = JSON.parse(response).token;
        userDevice.save(JSON.parse(response).token, null, JSON.parse(response).deviceId).then((data) => {
          //saved ibm token on user succesfully
        }).catch((error) => {
          messageService.setError(error);
        });
      };
      const failure = function(response) {
        console.log('Error: ' + response);
        messageService.setSuccess('registerDevice Error: ' + response);
      };
      const options = {};
      bmsPush.registerDevice(options, success, failure);
    }

    /**
     * Register notification
     */
    function _registerNotification(){
      const bmsPush = cordova.require('bms-push.BMSPush');
      const handleNotificationCallback = function(notification) {
        messageService.setSuccess('notification Success: ' + JSON.parse(notification).payload);
      };
      bmsPush.registerNotificationsCallback(handleNotificationCallback);
    }
andaqui commented 6 years ago

I found the error, was the JSON.parse(notification) causing the error. So it wasnt an issue on the cordova plugin.