Cordova Push Plugin: onNotificationGMC is not fired and cannot obtain regID

我是研究僧i 提交于 2019-12-30 09:34:21

问题


Hello everyone I'm developing a cordova Hybrid app that requires the Push Notification Service of Android and iOS to work and so I've installed the cordova plugin "PushPlugin".

Here's my code

document.addEventListener("deviceready", deviceready, false);

function deviceready() {
    try {
        pushNotification = window.plugins.pushNotification;
        pushNotification.unregister(successHandler, errorHandler);
        pushNotification.register(
            successHandler,
            errorHandler, {
                "senderID": "7645XXXXXXXX",
                "ecb": "onNotificationGCM"
            });

        function successHandler(data) {
            alert("SUCCESS: " + JSON.stringify(data));
        };

        function errorHandler(e) {
            alert("ERROR" + e);
        }


        function onNotificationGCM(e) {
            alert("Done")
        }

    } catch (e) {
        alert(e);
    }
}

When I run my application I expect to have two alert: the succesHandler one and the onNotificationGCM one but it only fires the succesHandler one saying: "OK"... With this problem I can't even access the regID parameter that will be stored in my server...

Can someone please explain me how to get the regID.. All my work depend on this

I'm testing this app on a Galaxy S4 Mini with Android 4.4.2.


回答1:


FIXED

I moved the onNotificationGCM in an empty script tag like this:

<script>
function onNotificationGCM(result) {
    alert(result.regid);
}
</script>

And now it give you the regID :)




回答2:


I had the same problem. If you are using AngularJS + IonicFramework you do not have to do this:

After you create your factory with your onDeviceReady function, creates onNotificationGCM function. Something like this:

app.factory('PushProcessingService', function () { ..

});

function onNotificationGCM(e) { }

I was creating onNotificationGCM inside my factory. This solve my problem. I hope it can helps you.




回答3:


In the ionic-framework you have a ready plugin: http://ngcordova.com/docs/plugins/pushNotifications/

here is an example for a working code for android devices:

module.run(function($cordovaPush) {

var androidConfig = {
    "senderID": "replace_with_sender_id",
    "ecb": "replace_with_the_name_of_function that will return you the regid"
};

document.addEventListener("deviceready", function(){
$cordovaPush.register(config).then(function(result) {
  // Success
}, function(err) {
  // Error
})

window.function replace_with_ecb(notification) { //notification.regid
  switch(notification.event) {
    case 'registered':
      if (notification.regid.length > 0 ) {
        alert('registration ID = ' + notification.regid);
      }
      break;

    case 'message':
      // this is the actual push notification. its format depends on the data model from the push server
      alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
      break;

    case 'error':
      alert('GCM error = ' + notification.msg);
      break;

    default:
      alert('An unknown GCM event has occurred');
      break;
  }
};

}, false);
});

this code only works a real device(not an emulator)



来源:https://stackoverflow.com/questions/25664951/cordova-push-plugin-onnotificationgmc-is-not-fired-and-cannot-obtain-regid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!