[英]Scheduling local notifications from the background (Ionic + ngCordova)
[英]Push notifications in ngCordova/Ionic, getting just “OK”
我第一次嘗試使用此示例代碼為Android創建混合應用程序。 我正在測試以查看通知是否可以正常工作,並且出現兩個錯誤:
您應該得到的result
對象不包含任何內容,只是可以
我得到$rootScope is not defined
,我應該在哪里定義?
我的代碼是
var app = angular.module('starter', ['ionic', 'ngCordova']);
app.run(function($cordovaPush) {
var androidConfig = {
"senderID": "84xxxxxxxx",
};
document.addEventListener("deviceready", function(){
$cordovaPush.register(androidConfig).then(function(result) {
// Success
console.log("OK, result is " + result);
}, function(err) {
// Error
console.log("NOT OK");
})
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
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;
}
});
// WARNING: dangerous to unregister (results in loss of tokenID)
$cordovaPush.unregister(options).then(function(result) {
// Success!
}, function(err) {
// Error
})
}, false);
});
像這樣在.run()中包含$rootScope
app.run(function($cordovaPush,$rootScope) {
result
register
響應的result
對您沒有任何幫助。如果注冊成功,您將收到一個registered
事件,您需要的是注冊ID,您將在這里收到此事件作為notification.regid
。
case 'registered':
if (notification.regid.length > 0 ) {
alert('registration ID = ' + notification.regid);
}
break;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.