【发布时间】:2015-07-10 00:01:34
【问题描述】:
我正在为 Android/IOS 做推送通知。
我使用了一个cordova push-plugin https://github.com/phonegap-build/PushPlugin,它似乎工作得很好。
信息:我在一个 AngularJS 项目中。
在我的 NotificationHelper 工厂中,我有这个初始化方法:
helper.init = function() {
// Instanciate push plugin notification
var pushNotification = window.plugins.pushNotification;
var errorHandler = function(error) {
logger.debug('errorHandler = ' + error);
};
if ($rootScope.isAndroid()) {
var senderId = CONFIG.NOTIFICATION_ANDROID_SENDER_ID;
pushNotification.register(function(result) {
logger.debug('successHandler = ' + result);
}, errorHandler, {
'senderID' : senderId,
'ecb' : 'onNotificationGCM'
});
}
};
我还在 mains.js 上定义了这些方法:
var onNotificationGCM = function(event) {
// call back to web service in Angular.
var elem = angular.element(document.querySelector('[ng-app]'));
var injector = elem.injector();
var service = injector.get('NotificationHelper');
service.onNotificationGCM(event);
};
从主javascript调用angularJS工厂是一个技巧。
'onNotificationGCM' 调用 'NotificationHelper.onNotificationGCM' 方法:
helper.onNotificationGCM = function(e) {
switch (e.event) {
case 'message':
// Notification happened while app was in the foreground
if (e.foreground) {
logger.debug('[notification] [message] Foreground : ' + JSON.stringify(e));
} else { // Notification touched in the notification tray
logger.debug('[notification] [message] Background : ' + JSON.stringify(e));
if (e.coldstart) {
// App was not running and user clicked on notification
} else {
// App was running and user clicked on notification
}
}
decriptPayloadNotification(e.payload);
break;
case 'registered':
logger.debug('[notification] [registered] : ' + JSON.stringify(e));
if (e.regid.length > 0) {
registerUser(e.regid, 'gcm');
}
break;
case 'error':
logger.debug('[notification] [error] : ' + JSON.stringify(e));
break;
default:
logger.debug('[notification] [default] : Unknown, an event was received and we do not know what it is : ' + JSON.stringify(e));
break;
}
};
在第一次使用时,一切正常:
- 收到“已注册”事件
- 收到通知
- 当我在前台时,我会收到“消息”事件:
NotificationHelper: [notification] [message] 前台: {"event":"message","from":"847593779913","message":"Agenêts 23/03 10h\r\n","collapse_key": "do_not_collapse","foreground":true,"payload":{"lt":"school","lv":"E1154","notId":"35429","title":"Agenêts le 23/03/ 2015","message":"Agenêts 23/03 10h\r\n"}}
- 当我在后台时,如果我收到通知并在通知托盘上触摸它,我会收到“消息”事件:
NotificationHelper: [notification] [message] 背景: {"event":"message","from":"847593779913","message":"la piscine sera fermée 1 journée pour raison technology","coldstart": false,"collapse_key":"do_not_collapse","foreground":false,"payload":{"lt":"swimming pool","lv":"E114","notId":"29869","title": "23/04/2015 fermeture de la piscine","message":"la piscine sera fermée 1 journée pour raison technology"}}
但是,如果我杀死我的应用程序,一切都会停止工作。
如果我重新启动应用程序,我将不会再收到“消息”事件并且不会调用“onNotificationGCM”。
我写了一些文章谈论这个问题,但没有成功:
Stackoverflow : Phonegap PushNotification to open a specific app page
有人知道这个问题吗?
【问题讨论】:
标签: javascript angularjs cordova push-notification phonegap-pushplugin