Pushwoosh / pushwoosh-phonegap-plugin

Pushwoosh PhoneGap Build Plugin
Other
109 stars 139 forks source link

Attempt to invoke virtual method 'void com.pushwoosh.PushManager.registerForPushNotifications()' on a null object reference #247

Closed jeweller1980stepanets closed 7 years ago

jeweller1980stepanets commented 7 years ago

Hi, i am using pushwoosh-cordova-plugin version 6.5.0. in ionic application

On the iPhone i had same problem: Response "200 no error": string: {"status_code":210,"status_message":"Application not found","response":null} I added "Pushwoosh_APPID" key to Info.plist and after that worked.

But when i testing on android: Attempt to invoke virtual method 'void com.pushwoosh.PushManager.registerForPushNotifications()' on a null object reference

` var PUSHWOOSH_APP_ID = 'XXXXXXXXX'; var GOOGLE_PROJECT_NUMBER = 'XXXXXXXXX';

    function initPushNotification() {

      var pushNotification = cordova.require("pushwoosh-cordova-plugin.PushNotification");

      document.addEventListener('push-notification', function (event) {
        //get the notification payload
        var notification = event.notification;

        //display alert to the user for example
        alert(notification.aps.alert);

        //clear the app badge
        pushNotification.setApplicationIconBadgeNumber(0);
      });

      if (ionic.Platform.isIOS()) {
        pushNotification.onDeviceReady({ pw_appid: PUSHWOOSH_APP_ID});
      }

      if (ionic.Platform.isAndroid()) {
        pushNotification.onDeviceReady({ projectid: GOOGLE_PROJECT_NUMBER, pw_appid: PUSHWOOSH_APP_ID });
      }
      //registerDevice();
      pushNotification.registerDevice(function (status) {
        $rootScope.deviceToken = status.pushToken || status.deviceToken;
      }, function (status) {
        var errorStr = JSON.stringify(status);
        // if the request is timeout then retry
        console.log(errorStr);
      });
    }`

Thanks in advance.

DimanAM commented 7 years ago

This can happen only if registerDevice() is invoked before onDeviceReady(). You can check it using the device logs. Every plugin method invocation is printed out, e.g:

D/Pushwoosh(862): [CordovaPlugin] Plugin Method Called: onDeviceReady

Given your code this can only occur if ionic.Platform.isAndroid() and ionic.Platform. isIOS() works inconsistently. The fact that you also had to add "Pushwoosh_APPID" key to Info.plist for iOS confirms this theory. Actually you do not need to check platform when using pushNotification.onDeviceReady() method. You can simply use

pushNotification.onDeviceReady({ projectid: GOOGLE_PROJECT_NUMBER, pw_appid: PUSHWOOSH_APP_ID });

for all platforms.

jeweller1980stepanets commented 7 years ago

No, i see in console:

03-30 02:48:32.761 2653-2731/com.mlmstart D/Pushwoosh: [CordovaPlugin] Plugin Method Called: onDeviceReady 03-30 02:48:32.762 2653-2731/com.mlmstart D/Pushwoosh: [CordovaPlugin] broadcastPush = true 03-30 02:48:32.765 2653-2731/com.mlmstart D/Pushwoosh: [CordovaPlugin] Plugin Method Called: registerDevice 03-30 02:48:32.769 2653-2653/com.mlmstart D/SystemWebChromeClient: file:///android_asset/www/vendor-b61de83544.js: Line 37 : ERROR_REGISTER 03-30 02:48:32.770 2653-2653/com.mlmstart D/SystemWebChromeClient: file:///android_asset/www/vendor-b61de83544.js: Line 37 : "Attempt to invoke virtual method 'void com.pushwoosh.PushManager.registerForPushNotifications()' on a null object reference"

I don't know why it's not working :(

DimanAM commented 7 years ago

That's super-weird. As I can see from your log the execution actually reaches this point https://github.com/Pushwoosh/pushwoosh-phonegap-plugin/blob/master/src/android/src/com/pushwoosh/plugin/pushnotifications/PushNotifications.java#L258. After that it will either initialize mPushManager variable and there will no such error (wich you actually experience) or you will see this error and the stack trace: https://github.com/Pushwoosh/pushwoosh-phonegap-plugin/blob/master/src/android/src/com/pushwoosh/plugin/pushnotifications/PushNotifications.java#L288

PWLog.error(TAG, "Missing pw_appid parameter. Did you follow the guide correctly?", e);

All plugin methods are executed consequentially on the main thread so there should be no race condiiton.

Are you sure there no other errors in the device log? You can also set a breakpoint in onDeviceReady method to see when and why it fails to initialize PushManager.

jeweller1980stepanets commented 7 years ago

Thank you. I removed the platform check and moved the registration to a separate method. Now it working...