【发布时间】: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" height="57" src="res/icon/ios/icon-57.png" width="57" />
<icon gap:platform="ios" height="72" src="res/icon/ios/icon-72.png" width="72" />
<icon gap:platform="ios" height="114" src="res/icon/ios/icon-57-2x.png" width="114" />
<icon gap:platform="ios" height="144" src="res/icon/ios/icon-72-2x.png" width="144" />
<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" height="480" src="res/screen/ios/screen-iphone-portrait.png" width="320" />
<gap:splash gap:platform="ios" height="960" src="res/screen/ios/screen-iphone-portrait-2x.png" width="640" />
<gap:splash gap:platform="ios" height="1136" src="res/screen/ios/screen-iphone-portrait-568h-2x.png" width="640" />
<gap:splash gap:platform="ios" height="1024" src="res/screen/ios/screen-ipad-portrait.png" width="768" />
<gap:splash gap:platform="ios" height="768" src="res/screen/ios/screen-ipad-landscape.png" width="1024" />
<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。但是当我点击图标恢复时。没有涵盖这种情况。
标签: cordova phonegap-plugins phonegap-pushplugin