Phonegap 从后台推送通知 OnResume,它会从托盘中删除我的所有通知
Posted
技术标签:
【中文标题】Phonegap 从后台推送通知 OnResume,它会从托盘中删除我的所有通知【英文标题】:Phonegap Push notification OnResume from Background, it deletes all my notification from Tray 【发布时间】:2015-04-02 08:11:26 【问题描述】:我想知道如何处理 Phonegap 推送通知的 onResume 函数。 当我通过单击图标恢复应用程序时,托盘中的推送通知全部被擦除,但没有事件回调,没有消息被附加,它们只是消失了。
前景
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();
简历
would i do
function onResume()
onNotificationGCM = function(e)
$("#app-status-ul").append('
--INLINE NOTIFICATION--' + '');
document.addEventListener("resume", onResume, true);
??
我的 javascript
if (e.foreground)
$("#app-status-ul").append(e.payload.title);
if(e.payload.message="works")
$("#app-status-ul").append('Is this working!!');
// 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
if(e.payload.message="works")
$("#app-status-ul").append('Is this working?!!');
全套Javascript
var pushNotification;
function onDeviceReady()
//alert("onDeviceReady");
document.addEventListener("backbutton", function(e)
if( $("#home").length > 0)
// call this to get a new token each time. don't call it to reuse existing token.
//pushNotification.unregister(successHandler, errorHandler);
e.preventDefault();
navigator.app.exitApp();
else
navigator.app.backHistory();
, false);
try
pushNotification = window.plugins.pushNotification;
if (device.platform == 'android' || device.platform == 'Android')
// ecb: event callback that gets called when your device receives a notification
pushNotification.register(successHandler, errorHandler, "senderID":"xxxxxxxxxxxx","ecb":"onNotificationGCM"); // required!
else
pushNotification.register(tokenHandler, errorHandler, "badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"); // required!
catch(err)
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
alert(txt);
// end of device ready
// handle APNS notifications for ios
onNotificationAPN = function(e)
if (e.alert)
$("#app-status-ul").append('<li>push-notification: ' + e.alert + '</li>');
navigator.notification.alert(e.alert);
if (e.sound)
var snd = new Media(e.sound);
snd.play();
if (e.badge)
pushNotification.setApplicationIconBadgeNumber(successHandler, e.badge);
// handle GCM notifications for Android
window.onNotificationGCM = function(e)
//function onNotificationGCM(e)
switch( e.event )
case 'registered':
if ( e.regid.length > 0 )
// 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.
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(e.payload.title);
if(e.payload.message="works")
$("#app-status-ul").append('This FINALLY IS WORKING!!');
// 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
if(e.payload.message="works")
$("#app-status-ul").append('This FINALLY IS WORKING!!');
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;
function tokenHandler (result)
// Your iOS push server needs to know the token before it can push to this device
// here is where you might want to send it the token for later use.
// - called when a plugin method returns without error
function successHandler (result)
function errorHandler (error)
document.addEventListener('deviceready', onDeviceReady, true);
配置.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.phonegap.helloworld" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" xmlns:android = "http://schemas.android.com/apk/res/android">
<name>HelloWorld</name>
<description>
Hello World sample application that responds to the deviceready event.
</description>
<author email="support@phonegap.com" href="http://phonegap.com">
PhoneGap Team
</author>
<gap:config-file platform="android" parent="/manifest/application">
<activity android:launchMode="singleTop" />
</gap:config-file>
<preference name="phonegap-version" value="3.7.0" />
<preference name="orientation" value="default" />
<preference name="target-device" value="universal" />
<preference name="fullscreen" value="true" />
<preference name="webviewbounce" value="true" />
<preference name="prerendered-icon" value="true" />
<preference name="stay-in-webview" value="false" />
<preference name="ios-statusbarstyle" value="black-opaque" />
<preference name="detect-data-types" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="show-splash-screen-spinner" value="true" />
<preference name="auto-hide-splash-screen" value="true" />
<preference name="disable-cursor" value="false" />
<preference name="android-minSdkVersion" value="7" />
<preference name="android-installLocation" value="auto" />
<gap:plugin name="org.apache.cordova.battery-status" />
<gap:plugin name="org.apache.cordova.camera" />
<gap:plugin name="org.apache.cordova.media-capture" />
<gap:plugin name="org.apache.cordova.console" />
<gap:plugin name="org.apache.cordova.contacts" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.device-motion" />
<gap:plugin name="org.apache.cordova.device-orientation" />
<gap:plugin name="org.apache.cordova.dialogs" />
<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.file-transfer" />
<gap:plugin name="org.apache.cordova.geolocation" />
<gap:plugin name="org.apache.cordova.globalization" />
<gap:plugin name="org.apache.cordova.inappbrowser" />
<gap:plugin name="org.apache.cordova.media" />
<gap:plugin name="org.apache.cordova.network-information" />
<gap:plugin name="org.apache.cordova.splashscreen" />
<gap:plugin name="org.apache.cordova.vibration" />
<gap:plugin name="com.simonmacdonald.telephonenumber" version="1.0.0" />
<gap:plugin name="hu.dpal.phonegap.plugins.uniquedeviceid" />
<gap:plugin name="com.phonegap.plugins.pushplugin" version="2.4.0" />
<icon src="icon.png" />
<icon gap:platform="android" gap:qualifier="ldpi" src="res/icon/android/icon-36-ldpi.png" />
<icon gap:platform="android" gap:qualifier="mdpi" src="res/icon/android/icon-48-mdpi.png" />
<icon gap:platform="android" gap:qualifier="hdpi" src="res/icon/android/icon-72-hdpi.png" />
<icon gap:platform="android" gap:qualifier="xhdpi" src="res/icon/android/icon-96-xhdpi.png" />
<icon gap:platform="blackberry" src="res/icon/blackberry/icon-80.png" />
<icon gap:platform="blackberry" gap:state="hover" src="res/icon/blackberry/icon-80.png" />
<icon gap:platform="ios" src="res/icon/ios/icon-57.png" />
<icon gap:platform="ios" src="res/icon/ios/icon-72.png" />
<icon gap:platform="ios" src="res/icon/ios/icon-57-2x.png" />
<icon gap:platform="ios" src="res/icon/ios/icon-72-2x.png" />
<icon gap:platform="webos" src="res/icon/webos/icon-64.png" />
<icon gap:platform="winphone" src="res/icon/windows-phone/icon-48.png" />
<icon gap:platform="winphone" gap:role="background" src="res/icon/windows-phone/icon-173.png" />
<gap:splash gap:platform="android" gap:qualifier="port-ldpi" src="res/screen/android/screen-ldpi-portrait.png" />
<gap:splash gap:platform="android" gap:qualifier="port-mdpi" src="res/screen/android/screen-mdpi-portrait.png" />
<gap:splash gap:platform="android" gap:qualifier="port-hdpi" src="res/screen/android/screen-hdpi-portrait.png" />
<gap:splash gap:platform="android" gap:qualifier="port-xhdpi" src="res/screen/android/screen-xhdpi-portrait.png" />
<gap:splash gap:platform="blackberry" src="res/screen/blackberry/screen-225.png" />
<gap:splash gap:platform="ios" src="res/screen/ios/screen-iphone-portrait.png" />
<gap:splash gap:platform="ios" src="res/screen/ios/screen-iphone-portrait-2x.png" />
<gap:splash gap:platform="ios" src="res/screen/ios/screen-iphone-portrait-568h-2x.png" />
<gap:splash gap:platform="ios" src="res/screen/ios/screen-ipad-portrait.png" />
<gap:splash gap:platform="ios" src="res/screen/ios/screen-ipad-landscape.png" />
<gap:splash gap:platform="winphone" src="res/screen/windows-phone/screen-portrait.jpg" />
<access origin="*" />
<access origin="mailto:*" launch-external="yes" />
</widget>
【问题讨论】:
所以基本上当您的应用程序处于前台时,您会收到一些可以在托盘中看到的推送通知。但是当您按下主页按钮时收到通知后,应用程序会进入后台并且托盘也会清除它的所有通知。但是您希望通知完好无损??? 对不起,我解释得不够清楚。当应用程序在前台时,它可以完美地工作,消息被推送到应用程序中,它在后台时也可以工作。唯一不起作用的是,当应用程序在后台时,手机收到推送通知它位于托盘中,我单击应用程序图标恢复到应用程序,然后没有 ECB 回调和托盘被清除(一旦应用程序进入前台,推送通知消失并且应用程序永远不会收到消息)。因此,当单击应用程序图标时应用程序恢复时,我需要 ECB 回调。 好的...然后我给出一个解决方案作为答案,检查后让我知道它是否有效。 @john:确认的一件事,你不想在点击推送后清除通知???或者只是想清除特定的通知??? 我真的不介意,只要将推送消息放入应用程序,我就可以清除它。要点是那些消息(托盘中的推送消息)刚刚消失,我无法将它们放入应用程序。因为有 3 种情况,如果是 e.foreground,如果是 e.coldstart,如果是 e.background。但是当我点击图标恢复时。没有涵盖这种情况。 【参考方案1】:首先打开com.plugin.gcm包下的GCMIntentService.java文件。在那里你会发现
public void createNotification(Context context, Bundle extras)
方法。有一行包含类似
mBuilder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(context.getApplicationInfo().icon)
.setWhen(System.currentTimeMillis())
.setContentTitle("Notification")
.setContentText(jsonObject.getString("alert"))
.setTicker(extras.getString("title"))
.setContentIntent(contentIntent);
在该对象的末尾添加以下属性
.setAutoCancel(true);
onNotificationGCM(e)函数中的javascript端,有一个块
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 )
else // otherwise we were launched because the user touched a notification in the notification tray.
//when app in background
navigator.notification.alert(e.payload.msg);
break;
这里的e.payload.msg是GCM返回的数据
【讨论】:
这只是删除通知托盘对吗?但是当我单击应用程序图标以恢复应用程序时仍然没有事件回调。 那很奇怪,我试过你的代码,我也在上面发布了我的代码,我已经有了 e.foreground,还有 e.coldstart 等。但是当我恢复应用程序时,ECB 不起作用通过单击应用程序图标。当您单击应用程序图标打开应用程序时,您的消息是否加载(ECB 触发?)? 是的,在我的代码中 ecb 着火了。我认为您的代码中有一些错误。您可以在点击通知时在控制台中检查它会引发什么错误吗? 当我点击托盘中的通知或当应用程序处于前台时它们会触发,只是当我通过单击应用程序图标恢复应用程序时不会触发。我可以提醒 console.log 看看吗?我已经有 $("#app-status-ul").append('ERROR -> MSG:' + e.msg + '');但它没有给出任何错误。我想知道它是否与我的 Config.xml 或 Androidmanifest 有关系 我也将我的整个 javascript 和我的 config.xml 一起发布在那里,我感觉它可能与我的配置有关。我需要一个 document.addEventListener("resume", onResume, true);当我点击应用程序恢复时,欧洲央行的一部分?以上是关于Phonegap 从后台推送通知 OnResume,它会从托盘中删除我的所有通知的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序在Phonegap(cordova)的后台状态下收到推送通知时增加徽章编号,
当应用程序打开或应用程序处于后台时,Phonegap Firebase 推送通知不会触发事件侦听器