Parse CorodvaPush Ionic: Android does not show notifications when the application is in the background

I have a problem with the plugin. When I am in the application and I send a notification with Parse, I get a message with a message (this works as intended). However, when the application is in the background, nothing is displayed on the phone. Here's how I use the plugin, and how I handle notifications:

var gmcId = "xxxxxxx"; var androidConfig = { senderID: gmcId }; document.addEventListener("deviceready", function(){ $cordovaPush.register(androidConfig).then(function(result) { console.log("result: " + result); }, function(err) { // Error }) $rootScope.$on('$cordovaPush:notificationReceived', function(event, 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. 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 attention, throw up a dialog, etc. if ( e.foreground ) { // if the notification contains a soundname, play it. navigator.notification.beep(1); alert(e.payload.data.alert) } else { // otherwise we were launched because the user touched a notification in the notification tray. if ( e.coldstart ) { } else { navigator.notification.beep(1); alert(e.payload.data.alert) } } 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; } }); }, false); 

Thanks!!

Btw, this is the Parse notification structure that I receive:

 {"collapse_key":"do_not_collapse", "event":"message", "foreground":true, "from":"xxxxxxxxxx", "payload":{ "data": {"alert":"asdasdas","push_hash":"bff149a0b87f5b0e00d9dd364e9ddaa0"},"push_id":"2iFhVp2R4u","time":"2015-07-21T12:24:09.905Z"}} 

Answer:

So, after two days of hair pulling, I managed to solve the problem. Thus, the problem is that it does not matter how you specify JSON in Parse, you always send it in the form presented above - this means that you always indicate in the payload -> data -> "key": "value" . However, the GCMIntentService is looking for a notification that has a “message” in the first JSON layer. So it should look like this:

  {"collapse_key":"do_not_collapse", "event":"message", "foreground":true, "from":"xxxxxxxxxx", "message":"ssadasdasdsa" ......} 

You can see that it is listed in GCMIntentService.java below the lines that run "protected void onMessage" (I think its line is 53).

In any case, you must fix this, we need to change the notification processing when the application is not in the foreground. We need to change it to:

  @Override protected void onMessage(Context context, Intent intent) { Log.d(TAG, "onMessage - context: " + context); // Extract the payload from the message Bundle extras = intent.getExtras(); if (extras != null) { // if we are in the foreground, just surface the payload, else post it to the statusbar if (PushPlugin.isInForeground()) { extras.putBoolean("foreground", true); PushPlugin.sendExtras(extras); } else { try { JSONObject object_example = new JSONObject( extras.getString("data")); String message = object_example.getString("alert"); extras.putString("message", message); } catch (JSONException e) { //some exception handler code. } createNotification(context, extras); } } } 

Here in our code it will be taken into account that our notification does not have a “message” in the first JSON layer - it will look for it in the data-> notification (standard notification form from PARSE).

I hope this helps because I spent waaay for many days trying to figure it out :).

+7
android ionic-framework ionic
source share
2 answers

So, after two days of hair pulling, I managed to solve the problem. Thus, the problem is that it does not matter how you specify JSON in Parse, you always send it in the form presented above - this means that you always indicate in the payload -> data -> "key": "value" . However, the GCMIntentService is looking for a notification that has a “message” in the first JSON layer. So it should look like this:

 {"collapse_key":"do_not_collapse", "event":"message", "foreground":true, "from":"xxxxxxxxxx", "message":"ssadasdasdsa" ......} 

You can see that it is listed in GCMIntentService.java below the lines that run "protected void onMessage" (I think its line is 53).

In any case, you must fix this, we need to change the notification processing when the application is not in the foreground. We need to change it to:

  @Override protected void onMessage(Context context, Intent intent) { Log.d(TAG, "onMessage - context: " + context); // Extract the payload from the message Bundle extras = intent.getExtras(); if (extras != null) { // if we are in the foreground, just surface the payload, else post it to the statusbar if (PushPlugin.isInForeground()) { extras.putBoolean("foreground", true); PushPlugin.sendExtras(extras); } else { try { JSONObject object_example = new JSONObject( extras.getString("data")); String message = object_example.getString("alert"); extras.putString("message", message); } catch (JSONException e) { //some exception handler code. } createNotification(context, extras); } } } 

Here in our code it will be taken into account that our notification does not have a “message” in the first JSON layer - it will look for it in the data-> notification (standard notification form from PARSE).

I hope this helps because I spent waaay for many days trying to figure it out :).

+1
source share

I used a plugin that was specially created for Parse.com alerts, this is how I registered for notifications, and it works in all scenarios.

 registerCallback: function (_pushCallback) { var deferred = $q.defer(); $window.parsePlugin.registerCallback('onNotification', function () { $window.onNotification = function (pnObj) { _pushCallback && _pushCallback(pnObj); alert('We received this push notification: ' + JSON.stringify(pnObj)); if (pnObj.receivedInForeground === false) { // TODO: route the user to the uri in pnObj } }; deferred.resolve(true); }, function (error) { deferred.reject(error); }); return deferred.promise; } 

you can see the full source code here: https://github.com/aaronksaunders/dcww/blob/master/www/js/parseService.js

This parse plugin is used - https://github.com/grrrian/phonegap-parse-plugin

0
source share

All Articles