4

I am working chat application. I want to show custom push Notification when message receives, when app is open and closed (not destroyed or killed) notification are received, below code is working fine, but when app is killed or destroyed from background custom push notification is not received in oreo and above

Here is code I Used

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    try {
       context = getApplicationContext();
        Map<String, String> data = remoteMessage.getData();
       if ("message".equals(data.get("pushNotificationAction"))){
        System.out.println("message");


        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),
                0, intent, 0);
        String mediatype = data.get("mediaType");
        String mesageType= data.get("messageType");     

            String channel_id = createNotificationChannel(getApplicationContext());
            mBuilder = new NotificationCompat.Builder(getApplicationContext(),channel_id)
                    .setSmallIcon(R.drawable.kisan_smoll_logo1)
                    .setCustomBigContentView(contentViewBig)
                    .setCustomContentView(contentViewBig)
                    .setContentIntent(contentIntent)
                    .setContentTitle(data.get("messageText"))
                    .setWhen(when)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(NotificationManager.IMPORTANCE_MAX)
                    .setAutoCancel(true);


            NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            mNotificationManager.notify(1, mBuilder.build());

      }catch(Exception e){
            e.printStackTrace();
           }

Sending JSON from server is below:

     {
  "message":{
   "to": "FCMID", 
"data" : {
     "body" : "This is a Message!",
     "title" : "Notifiation Message"
 "mediaType":"IMAGE",
 "messageType":"chat",
      }
  }
}

By sending data from server custom Fcm push notification is received in android when app is open, but when app is swipe from recent notifications are not received

James Z
  • 12,209
  • 10
  • 24
  • 44
Snehangshu Kar
  • 445
  • 2
  • 13
  • onMessageReceived Add logs for remoteMessagedata.getNotification().getBody() and check app open and close logs for same – Ganesh Pokale Apr 26 '19 at 11:10
  • 1
    Possible duplicate of [handle push notification (FCM) while app is in background / kill - Android](https://stackoverflow.com/questions/51177628/handle-push-notification-fcm-while-app-is-in-background-kill-android) – oemel09 Apr 26 '19 at 11:38
  • @GaneshPokale when app is open log , i can see the log but when app is close(distroyed) not able to see logs – Snehangshu Kar Apr 26 '19 at 11:46
  • @omel i am asking about custom Notification not from firebase console, which is not received oreo and above devices in backgroud or when app is killed, this not duplicate – Snehangshu Kar Apr 26 '19 at 11:49
  • Add this logs for remoteMessagedata.getNotification().getBody() at before Map data = remoteMessage.getData(); – Ganesh Pokale Apr 26 '19 at 11:54
  • As you asked i have added log like bellow, when app is open, i can see the log but when app is close(destroyed) not able to see logs mLogger.log("onMessageReceived" + remoteMessage.getNotification().getBody()); Map data = remoteMessage.getData(); – Snehangshu Kar Apr 26 '19 at 12:16

0 Answers0