5

大家好我正在開發一個cordova Hybrid應用程序,它需要Android和iOS的推送通知服務才能工作,所以我已經安裝了cordova插件「PushPlugin」。Cordova推送插件:onNotificationGMC未被解僱,無法獲得regID

這裏是我的代碼

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 ..我的所有工作都依賴於這個

我在銀河S4迷你測試這個程序與Android 4.4.2。

回答

3

FIXED

我感動onNotificationGCM在這樣一個空的腳本標籤:

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

現在它給你的RegID :)

+0

我不得不將我的回調函數,如'onNotificationGCM'和'onNotificationAPNS'附加到'window'。似乎是某種範圍的問題。 – cloudrave 2015-01-14 20:09:10

+0

我得到了regID,但是當我提交消息時,它會顯示「InvalidRegistration」,我檢查了項目編號(發件人ID),並且還激活了GCM API ..並審查了服務器api密鑰..所有沒關係..但它說。無效註冊錯誤..任何幫助嗎? – idleMind 2015-06-14 16:42:57

0

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

使用onDeviceReady函數創建工廠後,創建onNotificationGCM函數。是這樣的:

app.factory( 'PushProcessingService',函數(){ ..

});

功能onNotificationGCM(E){}

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

+0

我面臨同樣的問題,並且提供了您的建議。不幸的是,它沒有按照我的預期工作。我在哪裏必須調用PushProcessingService?我把它放在這裏app.run(函數($ ionicPlatform,PushProcessingService){PushProcessingService.initialize(); ...} – asubanovsky 2015-02-12 02:45:16

0

在離子的框架,你有一個現成的插件: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); 
}); 

此代碼只能在真實設備(不是模擬器)

+0

我得到了regID,但是當我提交消息時,它說「InvalidRegistration」,,我檢查了項目號碼(發件人ID),也激活GCM API ..和審查服務器API密鑰..所有好的..但它說..無效註冊錯誤..任何幫助嗎? – idleMind 2015-06-14 16:43:31

+0

我也通過這段代碼得到了「OK」。 – 2015-09-03 19:19:07