Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse CorodvaPush Ionic: Android doesnt show notifications when app in background

I have an issue with the plugin. When I am in the app, and I send notification with Parse, I receive an alert with the message (this works as intended). However, when app is in the background, nothing displays on the phone. Here is how I use 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's 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 structure of the Parse notification 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 pulling my hair, I finally managed to to fix the issue. So, the problem is that it doesn't matter how you specify the JSON in Parse, its always send in the form presented above - meaning that what you specify is always in payload->data->'key':'value'. However, the GCMIntentService seeks for a notification, that has "message" in the first layer of the JSON. Meaning, it has to look like that:

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

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

Anyway, you to fix this, we need to change the handling of a notification, when app is not in 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, our code will take into consideration that our notification doesnt have 'message' in first layer of JSON - it will seek it at the data->alert (standard form of the notification from PARSE).

I hope that helps, because I've spend waaay to many days trying to figure it out :).

like image 741
uksz Avatar asked Jul 15 '15 13:07

uksz


People also ask

Can Android emulator receive push notifications?

The emulator must enable the Google API for Google Play services. To resolve this issue, the mobile developer must create an emulator that uses Google API as the target OS.


1 Answers

So, after two days of pulling my hair, I finally managed to to fix the issue. So, the problem is that it doesn't matter how you specify the JSON in Parse, its always send in the form presented above - meaning that what you specify is always in payload->data->'key':'value'. However, the GCMIntentService seeks for a notification, that has "message" in the first layer of the JSON. Meaning, it has to look like that:

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

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

Anyway, you to fix this, we need to change the handling of a notification, when app is not in 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, our code will take into consideration that our notification doesnt have 'message' in first layer of JSON - it will seek it at the data->alert (standard form of the notification from PARSE).

I hope that helps, because I've spend waaay to many days trying to figure it out :).

like image 58
uksz Avatar answered Oct 03 '22 06:10

uksz