1

I'm using Cordova 3.5.0-0.2.6 to cerate an Android application and using PushPlugin 2.2.1 to push notifications.

Everything works fine and get the notifications in my droid notifications bar when the app is in background, but when it's in foreground nothing happens when it get the notification. Wanted to make it play a sound or even show a popup alert to let the user know he has a new notification. Copied the example code from the plugin's github page (https://github.com/phonegap-build/PushPlugin) but it isn't working. The corresponding part of the code is this:

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)
        {
            // on Android soundname is outside the payload.
            // On Amazon FireOS all custom attributes are contained within payload
            var soundfile = e.soundname || e.payload.sound;
            // if the notification contains a soundname, play it.
            // playing a sound also requires the org.apache.cordova.media plugin
            var my_media = new Media("/android_asset/www/"+ soundfile);
            my_media.play();
        }
        else
        {   // otherwise we were launched because the user touched a notification in the notification tray.
            window.location = 'iframe.html?url=' + e.payload.url;
            if (e.coldstart) {
                //$("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
            } else{
                //$("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>');
            }
        }

Not sure where the beep.wav file should be placed, I placed it into the www folder, but I'm 100% sure that's the place to put it.

Any one had this problem before? Any idea what I might be missing here?

Thanks,

Luciano

AngelGris
  • 813
  • 1
  • 9
  • 25

2 Answers2

2

For android please add property "forceShow": "true" and it should work..

example code is below:

var push = PushNotification.init({
                        "android": {
                            "senderID": "xxxxxxxxx",
                            "forceShow": "true",
                            "icon": "icon",
                            "sound": true,
                            "vibration": true,
                            "badge": true
                        },
                        "browser": {
                        },
                        "ios": {
                            "senderID": "xxxxxxxxx",
                            "icon": "icon",
                            "gcmSandbox": true,
                            "sound": true,
                            "vibration": true,
                            "badge": true
                        },
                        "windows": {}
                    });
Nimesh khatri
  • 763
  • 12
  • 29
1

but when it's in foreground nothing happens when it get the notification

This is the default behavior of PushPlugin. It creates a notification and the associated sound only when the application is in the background or closed.


Wanted to make it play a sound or even show a popup alert to let the user know he has a new notification.

The easiest solution would be to use Cordova Plugin Dialogs. All you would have to do is:

switch( e.event )
{
    case 'message':
        if (e.foreground) {
            navigator.notification.beep(1);
        }
.
.
}

Note that this beep will be the message tone configured by the user on his/her device.


Not sure where the beep.wav file should be placed, I placed it into the www folder

Not sure either, but I think it is the www directory too.

See also: Android won't play push sound while app not running

Community
  • 1
  • 1
Ajoy
  • 1,838
  • 3
  • 30
  • 57