Cordova Push Notification when app is executing (in foreground)

Comments

5 comments

  • Avatar
    Gayview Mahat

    Hello,

    The instruction below is for Native Android, on Cordova you need to do the same in PushNotifications.java file.

    Here is the code from the MainActivity of our Sample App:

    //Push message receiver
    private BroadcastReceiver mReceiver = new BasePushMessageReceiver()
    {
       @Override
       protected void onMessageReceive(Intent intent)
       {
          //JSON_DATA_KEY contains JSON payload of push notification.
          doOnMessageReceive(intent.getExtras().getString(JSON_DATA_KEY));
       }
    };
    

    What you need to do is to override the receiver like this:

    //Push message receiver
    private BroadcastReceiver mReceiver = new BasePushMessageReceiver()
    {
    
       @Override
       public void onReceive(Context context, Intent intent)
       {
          int id = PreferenceUtils.getMessageId(context);
          PreferenceUtils.setMessageId(context,-1);
          super.onReceive(context,intent);
          PreferenceUtils.setMessageId(context,id);
       }
       @Override
       protected void onMessageReceive(Intent intent)
       {
          //JSON_DATA_KEY contains JSON payload of push notification.
          doOnMessageReceive(intent.getExtras().getString(JSON_DATA_KEY));
       }
    };
    

    This way none of your pushes will be cancelled when the app is in foreground.

  • Avatar
    DailyCaller

    Hi,

    thanks for the answer, i have been doing some tests based on the code you posted and now arrives the notification and the user can see it, because it keeps on the header. But the notification is still processed because the super.onReveice call.

    Can i override the onReceive() method with no code inside? implications?

    thanks!

  • Avatar
    DailyCaller

    Looking at the super.onReceive declaration i need to do:

    • don't cancel the messageId via notification manager
    • send the pushStat
    • don't do the onMessageReceive

    how can avoid to call the super.onReceive but still sending the pushStat?

  • Avatar
    DailyCaller

    This topic is still not solved.

    The initial solution is not correct because the notification is still processed when the notification arrives when app is in background state. If i override method OnReceive (from BroadcastReceiver), calling super.onReceive(context,intent); the notification keeps at the status bar, but internally the notification is consumed and i don't want that behavior.

  • Avatar
    Gayview Mahat

    Sorry for such a delay. Instead of the solution suggested above, could you possibly try setting the following to "false"?

    < meta-data android:name="PW_BROADCAST_PUSH" android:value="true" />
    

    This way your notifications will be accessible in the Notification Bar even if the app was in the foreground.

Please sign in to leave a comment.