簡體   English   中英

Cordova Push插件:onNotificationGMC未被觸發且無法獲取regID

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

大家好我正在開發一個Cordova Hybrid應用程序,它需要Android和iOS的推送通知服務才能工作,因此我安裝了cordova插件“ PushPlugin ”。

這是我的代碼

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);
    }
}

當我運行我的應用程序時,我希望有兩個警告:succesHandler一個和onNotificationGCM一個,但它只觸發succesHandler一個說:“OK”...有了這個問題,我甚至無法訪問將被存儲的regID參數在我的服務器......

有人可以解釋我如何獲得regID ..我的所有工作都依賴於此

我正在使用Android 4.4.2在Galaxy S4 Mini上測試這個應用程序。

固定

我將onNotificationGCM移動到一個空的腳本標記中,如下所示:

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

現在它給你regID :)

我有同樣的問題。 如果您使用的是AngularJS + IonicFramework,則不必執行此操作:

使用onDeviceReady函數創建工廠后,創建onNotificationGCM函數。 像這樣的東西:

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

});

功能onNotificationGCM(e){}

我在工廠內創建了onNotificationGCM。 這解決了我的問題。 我希望它可以幫助你。

在離子框架中,您有一個現成的插件: http//ngcordova.com/docs/plugins/pushNotifications/

這是一個Android設備工作代碼的示例:

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);
});

此代碼僅適用於真實設備(不是模擬器)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM