不调用 Cordova PushNotification onNotificationGCM
Cordova PushNotification onNotificationGCM is not called
我正在为两者做推送通知 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);
};
从 main 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;
}
};
第一次使用时一切正常:
- 'Registered' 收到事件
- 收到通知
- 当我在前台时,我收到 'message' 事件:
NotificationHelper: [notification] [message] Foreground : {"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"}}
- 当我在后台时,如果我收到通知并在通知托盘上触摸它,我会收到 'message' 事件:
NotificationHelper: [notification] [message] Background : {"event":"message","from":"847593779913","message":"la piscine sera fermée 1 journée pour raison technique","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 technique"}}
但是,如果我终止我的应用程序,一切都会停止工作。
如果我重新启动应用程序,我将不会再收到 'message'事件并且不会调用 'onNotificationGCM'。
我写了一些文章讨论这个问题,但没有成功:
Whosebug : Phonegap PushNotification to open a specific app page
有人知道这个问题吗?
如果您使用 node-gcm 作为推送通知服务器端,我希望这对您的问题有所帮助:
要检查的事情:
- 从控制台使用 adb logcat 并检查您的设备日志以查看您的设备是否收到任何通知(如果收到,您应该会看到来自 gcmservices 的一些日志)。我相信你应该看到一些日志,否则当应用程序在前台时你可能不会收到任何通知。
- 检查您是否在消息数据上添加了 "title" 和 "message" 键(这让我很抓狂)
- 检查 delayWhileIdle 参数是否为 false
我正在使用的示例消息变量和方法如下:
var pushAndroidSender = new gcm.Sender('Your GoogleApi Server Key Here');
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: false,
timeToLive: 3,
data: {
"message":"What's up honey?",
"title":"Come on"
}
});
pushAndroidSender.send(message, registrationIds, function(err, result) {
if (err) {
console.error("this is an error: " + err);
} else {
console.log("this is a successful result:" + result);
}
});
我在问自己为什么它第一次能正常工作,而后几次却没有...我有这个想法:
- 可能回调函数是第一次订阅到插件,但下一次没有。
这正是我问题的根源。我只调用 "pushNotification.register" 一次来初始化插件并获取 ID...
但是下次启动应用程序时,我没有再次实例化插件,所以pushplugin不知道在回调请求中调用哪个方法。
所以答案是:在每次启动应用程序时调用 "pushNotification.register"!
在我的代码中,我必须在每个 "deviceready" 事件上调用下面的方法:
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'
});
}
};
我正在为两者做推送通知 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);
};
从 main 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;
}
};
第一次使用时一切正常:
- 'Registered' 收到事件
- 收到通知
- 当我在前台时,我收到 'message' 事件:
NotificationHelper: [notification] [message] Foreground : {"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"}}
- 当我在后台时,如果我收到通知并在通知托盘上触摸它,我会收到 'message' 事件:
NotificationHelper: [notification] [message] Background : {"event":"message","from":"847593779913","message":"la piscine sera fermée 1 journée pour raison technique","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 technique"}}
但是,如果我终止我的应用程序,一切都会停止工作。
如果我重新启动应用程序,我将不会再收到 'message'事件并且不会调用 'onNotificationGCM'。
我写了一些文章讨论这个问题,但没有成功:
Whosebug : Phonegap PushNotification to open a specific app page
有人知道这个问题吗?
如果您使用 node-gcm 作为推送通知服务器端,我希望这对您的问题有所帮助: 要检查的事情:
- 从控制台使用 adb logcat 并检查您的设备日志以查看您的设备是否收到任何通知(如果收到,您应该会看到来自 gcmservices 的一些日志)。我相信你应该看到一些日志,否则当应用程序在前台时你可能不会收到任何通知。
- 检查您是否在消息数据上添加了 "title" 和 "message" 键(这让我很抓狂)
- 检查 delayWhileIdle 参数是否为 false
我正在使用的示例消息变量和方法如下:
var pushAndroidSender = new gcm.Sender('Your GoogleApi Server Key Here');
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: false,
timeToLive: 3,
data: {
"message":"What's up honey?",
"title":"Come on"
}
});
pushAndroidSender.send(message, registrationIds, function(err, result) {
if (err) {
console.error("this is an error: " + err);
} else {
console.log("this is a successful result:" + result);
}
});
我在问自己为什么它第一次能正常工作,而后几次却没有...我有这个想法:
- 可能回调函数是第一次订阅到插件,但下一次没有。
这正是我问题的根源。我只调用 "pushNotification.register" 一次来初始化插件并获取 ID... 但是下次启动应用程序时,我没有再次实例化插件,所以pushplugin不知道在回调请求中调用哪个方法。
所以答案是:在每次启动应用程序时调用 "pushNotification.register"!
在我的代码中,我必须在每个 "deviceready" 事件上调用下面的方法:
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'
});
}
};