【发布时间】:2014-03-20 01:00:49
【问题描述】:
我正在 Eclipse 中开发一个 android 应用程序,并且我已经成功安装了 Phonegap PushPlugin,一切正常,在状态栏中接收并启动通知,并且在触摸时打开我的应用程序。
问题出现在我的应用程序关闭(即冷态)时,应用程序已成功启动,但没有运行“if (e.coldstart)”中的操作
我根据插件官方文档中发布的以下示例编写了我的代码:
function onNotificationGCM(e) {
$("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');
switch (e.event) {
case 'registered':
if (e.regid.length > 0) {
$("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
// Your GCM push server needs to know the regID before it can push to this device
// here is where you might want to send it the regID for later use.
console.log("regID = " + e.regid);
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if (e.foreground) {
$("#app-status-ul").append('<li>--INLINE NOTIFICATION--' + '</li>');
// if the notification contains a soundname, play it.
var my_media = new Media("/android_asset/www/" + e.soundname);
my_media.play();
} else { // otherwise we were launched because the user touched a notification in the notification tray.
if (e.coldstart) {
$("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
} else {
$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
}
}
$("#app-status-ul").append('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
$("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>');
break;
case 'error':
$("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
break;
default:
$("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
break;
}
}
此代码位于我的应用程序的主页上,当您点击通知并成功显示标签“INLINE NOTIFICATION”和“BACKGROUND NOTIFICATION”(以适用者为准)时打开
不应该这样放在
if (e.coldstart) {
$("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
}
如果应用来自被关闭的情况下应该执行吗?
非常感谢您的合作,因为当通知到达时,我的应用程序应该打开一个与 index.html 不同的页面,并且该页面保留在 index.html 中
在这里我将编辑我的问题,在original plugin documentation 中放置一个文本片段,引用变量“coldstart”:
最后,您是否应该通过点击后退按钮完全退出应用程序 从主页,您可能仍会收到通知。接触 通知托盘中的该通知将重新启动您的应用程序并 允许您处理通知 (COLDSTART)。在这种情况下 将在传入事件上设置冷启动标志。
另外添加我自己的代码,在收到推送通知时打开一个新页面:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent();
},
// Update DOM on a Received Event
receivedEvent: function() {
var pushNotification = window.plugins.pushNotification;
pushNotification.register(app.successHandler, app.errorHandler,{"senderID":"My sender ID","ecb":"app.onNotificationGCM"});
},
// result contains any message sent from the plugin call
successHandler: function(result) {
alert('Callback Success! Result = '+result)
},
errorHandler:function(error) {
alert(error);
},
onNotificationGCM: function(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
console.log("Regid " + e.regid);
window.localStorage.setItem("gcmid", e.regid);
}
break;
case 'message':
// if this flag is set, this notification happened while we were in the foreground.
// you might want to play a sound to get the user's attention, throw up a dialog, etc.
if ( e.foreground )
{
alert('foreground message = '+e.message+' msgcnt = '+e.msgcnt+' room= '+e.payload.room_msg+' lat = '+lat_r+' lng = '+lng_r);
top.location.href="chat.html?idu=notapply&room_snd=notapply&roomname_snd=" + e.payload.room_msg + "&lat_snd=" + lat_r + "&lng_snd=" + lng_r + "&msg_snd=" + e.message;
}
else
{ // otherwise we were launched because the user touched a notification in the notification tray.
if ( e.coldstart )
{
alert('foreground message = '+e.message+' msgcnt = '+e.msgcnt+' room= '+e.payload.room_msg+' lat = '+lat_r+' lng = '+lng_r);
top.location.href="chat.html?idu=notapply&room_snd=notapply&roomname_snd=" + e.payload.room_msg + "&lat_snd=" + lat_r + "&lng_snd=" + lng_r + "&msg_snd=" + e.message;
}
else
{
alert('foreground message = '+e.message+' msgcnt = '+e.msgcnt+' room= '+e.payload.room_msg+' lat = '+lat_r+' lng = '+lng_r);
top.location.href="chat.html?idu=notapply&room_snd=notapply&roomname_snd=" + e.payload.room_msg + "&lat_snd=" + lat_r + "&lng_snd=" + lng_r + "&msg_snd=" + e.message;
}
}
break;
case 'error':
alert('GCM error = '+e.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
}
};
暂时解决我的问题,添加一个额外的有效负载,指示我的活动应该打开一个与 index.html 不同的页面,尽管该解决方案不会将“e.coldstart”作为“true”传递,但我将其添加到函数中在 PushHandlerAtivity.java 中:
之后:
private void forceMainActivityReload()
{
PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());
startActivity(launchIntent);
}
之前:
private void forceMainActivityReload()
{
PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());
Bundle extras = getIntent().getExtras();
launchIntent.putExtra("room_msg", extras.getString("room_msg"));
startActivity(launchIntent);
}
当然在生成通知的函数中使用 addDataWithKeyValue 函数添加额外的有效负载,然后我的主要活动添加以下内容:
Bundle extras = getIntent().getExtras();
String message = extras.getString("room_msg");
if(message != null){
super.loadUrl("file:///android_asset/www/chat.html?", 10000);
}else{
super.loadUrl("file:///android_asset/www/index.html", 10000);
}
【问题讨论】:
-
当应用程序在后台时,JS 不工作(停止)。这样它就不会执行
-
当后台运行完美@HanhLe,问题是当完全关闭时,我假设pushplugin启动应用程序然后运行JS函数“onNotificationGCM(e)”
-
哦,当您的应用程序完全关闭时,如果您点击启动应用程序的通知,e.coldstart 将无法工作。因为,您的应用程序关闭,这意味着所有资源都将无法正常工作(除了服务背景 Android,它会收到消息和通知)。
-
好的@HanhLe,然后出现另一个问题,适用于“e.coldstart”?
-
"e.coldstart" 表示你的应用启动和推送插件未创建,你能发布你的代码,我会找到准确的点
标签: android cordova notifications push-notification