0

I am wondering how do i handle onResume functions for Phonegap Push notification. When i resume the app by either clicking on the Icon, the push notifications from the tray, all get wiped, but there are no event call back, none of the message get appeneded, they just disappeared.

For the foreground

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();
            }

For Resume

would i do 
function onResume() {
onNotificationGCM = function(e) {
$("#app-status-ul").append('

--INLINE NOTIFICATION--' + '');
}
}
document.addEventListener("resume", onResume, true);

??

My 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?!!');

                     }                }

Whole set of 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);

Config.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>
John
  • 983
  • 1
  • 13
  • 31
  • So basically when your app is in foreground you get some push notifications which can be seen in tray.But after got the notifications when you press the home button then the app goes in background and also the tray clears all its notifications. But you want the notifications intact right??? – Banik Apr 02 '15 at 11:14
  • Sorry i didn't explain it clear enough. When the App is in the foreground, it works perfectly, the message get pushed into the App, it also works when it is in the background. The only time that it doesnt work is, when the App is in the background, and phone receives push notification it sits in the tray, and i click the App Icon to resume to the App, then there is no ECB call back and the tray got cleared(soon as the App moves into the foreground,push notifications disappeared and the App never receive the messages). So i need a ECB call back when the app resume when the App icon is clicked. – John Apr 02 '15 at 11:17
  • okay... then i am giving a solution as answer and after checking it let me know it works or not. – Banik Apr 02 '15 at 11:22
  • @ john : one thing for confirmation, you dont want to clear notification after tapping the push ??? or just want to clear the particular notification??? – Banik Apr 02 '15 at 11:30
  • I dont really mind, i can have it cleared as long as the push messaged is dropped into the App. The main point is that Those messages(push message in the tray) just disappeared and i can't get them into the App. Cos there are 3 situation right, if e.foreground and if e.coldstart and if e.background. But when i click the icon to resume. That situation isnt covered. – John Apr 02 '15 at 11:36
  • okay... then setAutoCancel(true); I ve just updated the answer,check it works or not... – Banik Apr 02 '15 at 11:37
  • Just a quick questions, did you have to edit the Java inorder to load the Push messages in from the tray when you click Icon to resume? Or if it can be done through in the Javascript? because i see there are the e.foreground and e.coldstart, is there an event for resume? – John Apr 02 '15 at 11:42
  • without using native java code you cant clear the tray because it depends on the NotificationManager class which occurs in android.jar file. So its impossible to do in javascript. – Banik Apr 02 '15 at 11:48
  • Cool, no problem, i was just wondering if that alteration for the Java is to drop the messages into the App from the Push onResume as well, because my App isnt even doing that at the moment onResume when icon is clicked(Only when the app is already in foreground or in background(clicked by push tray notification). Sorry for the continuous questions. – John Apr 02 '15 at 11:58
  • I was just wondering about it, because on Github there was a comment said version 2.4.0 the onResume ECB is a standard behavior... – John Apr 03 '15 at 05:02

1 Answers1

1

First open GCMIntentService.java file under com.plugin.gcm package.There you will find

public void createNotification(Context context, Bundle extras)

method.There is a line containing something like

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);

At the end of that object add the follwing property

.setAutoCancel(true);

For javascript side in onNotificationGCM(e) function,there is a block

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;

Here e.payload.msg is the data which is returned by the GCM

Banik
  • 911
  • 6
  • 10
  • This just deletes the Notification tray right?But still no event call back tho when i click the App icon to resume the App. – John Apr 04 '15 at 06:09
  • Thats weird, i tried your code, and i also posted mine above, i already have the e.foreground in place, also e.coldstart etc. But the ECB is not working when i resume the App by clicking the App icon. Does yours load in the message(ECB fires?) when you click the App icon to open the App? – John Apr 04 '15 at 08:59
  • yes, in my code ecb fires. I think there is some error in your code.can you check it in console whenever the you tap on the notification what error it throws? – Banik Apr 04 '15 at 09:06
  • When i click on the notification from the tray or when App is in foreground they fires tho, just not firing when i resume the App by clicking on the App icon. Can i alert the console.log to see? I already have $("#app-status-ul").append('
  • ERROR -> MSG:' + e.msg + '
  • '); But it gives no error. I am wondering if it has something to do with my Config.xml or Androidmanifest – John Apr 04 '15 at 09:15
  • I posted my whole javascript up there also with my config.xml as well, i am having a feeling that it might have something to do with my config. Do i need a document.addEventListener("resume", onResume, true); part for the ECB to fire when i click the App to resume? – John Apr 04 '15 at 09:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74475/discussion-between-banik-and-john). – Banik Apr 04 '15 at 09:34