0

I have successfully implemented PushPlugin and it is triggering push notifications in all states except when the app is in the foreground. Here is the code I am using:

function onNotification(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>');
            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.

            //navigator.notification.alert('--INLINE NOTIFICATION--');

            alert("You have a new alert!: " + e.payload.message);

            var my_media = new Media (../homebound.wav);
            my_media.play();
            } else {
            //otherwise we were launched because the user touched the notification in the notification tray.
            if (e.coldstart) 
                {
                    navigator.notification.alert('--COLDSTART NOTIFICATION--');
                }
                    else
                    {
                        navigator.notification.alert('--BACKGROUND NOTIFICATION--')
                    }
                }

            $("#app-status-ul").append('<li>MESSAGE -> MSG ' + e.payload.message + '</li>');

            $("#app-status-ul").append('<li>MESSAGE -> MSGCNT: ' + e.payload.msgcnt + '</li>')

            $status.append('<li>MESSAGE -> TIME: ' + e.payload.timeStamp + '</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;
    }

}

I have looked around and not found anything for this issue, the fix seems like it should be simple but is alluding me because I lack experience with this plugin. Has anyone had this issue and can let me know where the issue lies, thanks in advance.

EDIT: I have been debugging the app through usb debug, and I have found that the app is receiving the alert but it is not triggering anything, I have added an alert to the "if(e.foreground){alert("You have received a new notification: " + e.payload.message);" but it still isn't triggering, the issue is obviously lying around the if statement, but I cannot figure it out.

EDIT 2: I have updated to the question to respond to the comment made by user Hana Le, I am getting a warning error: "processMessage failed: Error: ReferenceError: onNotification is not defined"

Where would this need to be defined in order to make it work and why does the app receive and trigger a notification when running in background and when not running at all?

geolaw
  • 412
  • 1
  • 12
  • 26

2 Answers2

1

The problem cannot be solved inside your Cordovawebview because it is idle. Yo have to include some native code that launches local push. Perhaps it is already included in your plugin.

Martín Alcubierre
  • 4,341
  • 1
  • 27
  • 27
  • is there any examples of this you can link at all? – geolaw Aug 27 '14 at 15:37
  • Use Webintent Plugin to launch your cordova webview from native: https://build.phonegap.com/plugins/43 In the call (intent) you can pass a parameter to your webview so webintent.js can call your js notification. – Martín Alcubierre Aug 27 '14 at 16:04
  • i have read about what this is but i am not too sure how it helps me considering the push notifications are occurring when the app is in the background or inactive. – geolaw Aug 27 '14 at 18:13
  • Webintent plugin has been created to call idle or non instanciated activities (it reloads webview) – Martín Alcubierre Aug 27 '14 at 18:19
  • you were right about something being included in the plugin, it wasn't being called correctly, I have since managed to find the errors and put them straight. Thank you for your answers – geolaw Aug 29 '14 at 17:57
0
 var my_media = new Media (../homebound.wav)
 // ../homebound.wav is not String (Error from here)
 // And path media file incorrect. In Android path is: file:///android_asset/www
 var my_media = new Media ('file:///android_asset/www/homebound.wav')
Hanh Le
  • 894
  • 6
  • 14
  • this has not fixed the issue. The problem appears to be with something to do with the INLINE NOTIFICATION flag or the "if(e.foreground);" because the debug is displaying the message, and coldstart and background notifications are occuring. It is only the forefront notes that do nothing. – geolaw Aug 27 '14 at 03:27
  • You said "the app is receiving the alert ", where alert trigger? I not see anything. Make sure onNotificationGCM must have scope in window – Hanh Le Aug 27 '14 at 09:44
  • I had the alert trigger commented out "navigator.notification.alert('--INLINE NOTIFICATION--');" this is what is triggering the alert in the other two states (coldstart and background) but even when i use that, nothing happens when the app is in foreground. – geolaw Aug 27 '14 at 13:03
  • I don't known what you handled. You must test code line by line to find where issue appear or message not receive. It simple and you can solve – Hanh Le Aug 28 '14 at 01:17
  • I have updated the question after testing the code on a device an android device and debugging through usb – geolaw Aug 28 '14 at 20:44
  • Error mean onNotification not available in window object(window.onNotification is not defined). When GCM send message to device then native code will receive and fired message to the webview. In webview must have a function onNotification() to receive and do anything - onNotification must have scope in window object(window.onNotification = function(){...}) – Hanh Le Aug 29 '14 at 01:18
  • That explanation was perfect, I made some minor tweaks to my code (don't know exactly which part fixed it but it now works) Thank you so much! – geolaw Aug 29 '14 at 17:05