当应用暂停时,我尝试将推送通知显示在Android的顶部栏中。
应用程序处于前台时通知处理正常,但只要应用程序暂停或未运行,它们就永远不会被抓住。
这里是通知的原始JSON:
{"event":"message","from":"XXXXXXXXXX","collapse_key":"do_not_collapse","foreground":true,"payload":{"alert":"Hello World!","badge":"7","sound":"sound.caf"}}
这是捕获通知的应用代码:
window.onNotification = function (e) {
switch (e.event) {
case 'registered':
if (e.regid.length > 0) {
console.log("Your regID is : " + e.regid);
var userId = window.localStorage.getItem("UserIdToken").replace(/"/g, "");
var data = new Object();
data.UserId = userId;
data.PushNotificationRegId = e.regid;
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
console.log('message = ' + JSON.stringify(e));
var elem = angular.element(document.querySelector('[ng-app]'));
var rootScope = elem.injector().get('$rootScope');
rootScope.$broadcast('pushNotificationReceived', e);
break;
case 'error':
//console.log('GCM error = ' + e.msg);
alert('GCM error = ' + e.msg);
break;
default:
//console.log('An unknown GCM event has occurred');
alert('An unknown GCM event has occurred');
break;
}
};