[英]Push notifications in ngCordova/Ionic, getting just “OK”
I'm trying for the first time to create an hybrid app using this example code for Android . 我第一次尝试使用此示例代码为Android创建混合应用程序。 I'm testing to see if I can get notifications to work and I'm getting two errors: 我正在测试以查看通知是否可以正常工作,并且出现两个错误:
The result
object you should get it doesn't contain anything but just OK 您应该得到的result
对象不包含任何内容,只是可以
I'm getting $rootScope is not defined
, where should I define it? 我得到$rootScope is not defined
,我应该在哪里定义?
My code is 我的代码是
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);
});
Include $rootScope
in .run() like this 像这样在.run()中包含$rootScope
app.run(function($cordovaPush,$rootScope) {
And result
in register
response is not of any use to you.In case of success of registration you will receive an event registered
, what you need is registration id, which you will receive here as notification.regid
. 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.