0

I am working with FCM and sending Push Messages to Android and when the APP is open and push message is received the URL intent works fine sent via the TAG String for now. When the APP is closed and the push message is received it loads the default page.

The main point of using the notification: area is so I can utilize the channel_id. If I sent this information in the data: JSON it no longer listens to the channel_id but the push messages both open the correct URL whether the app is open or closed.

Hopefully we have some pro's in this area with FCM and can see my pitfall. Thanks!

My Firebase Messaging Service

    public class MyMessagingService extends FirebaseMessagingService {

        protected String title;
        protected String body;
        protected String tag;

    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        if (remoteMessage.getData().isEmpty()) {
            showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTag());
        } else
            showNotification(remoteMessage.getData());

    }

    private void showNotification(Map<String, String> data) {

        String title = data.get("title");
        String body = data.get("body");
        String tag = data.get("tag");

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        if (tag != null) {
            intent.putExtra("url", tag);
        }

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID_1 = "1";
        String NOTIFICATION_CHANNEL_ID_2 = "2";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel1 = new NotificationChannel(NOTIFICATION_CHANNEL_ID_1,"Critical Alerts",NotificationManager.IMPORTANCE_HIGH);
            notificationChannel1.setDescription("Critical Warnings from your Tubs");
            notificationChannel1.enableLights(true);
            notificationChannel1.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationManager.createNotificationChannel(notificationChannel1);

            NotificationChannel notificationChannel2 = new NotificationChannel(NOTIFICATION_CHANNEL_ID_2,"Status Updates",NotificationManager.IMPORTANCE_LOW);
            notificationChannel2.setDescription("Status and Changes");
            notificationChannel2.enableLights(true);
            notificationChannel2.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationManager.createNotificationChannel(notificationChannel2);
        }
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID_1);
            notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(body);
            notificationBuilder.setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(body));
            notificationBuilder.setContentIntent(pendingIntent);
            notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
    }

    //App Closed
    public void showNotification(String title2, String body2, String tag2) {

        this.title = title2;
        this.body = body2;
        this.tag = tag2;

        Intent intent2 = new Intent(this, MainActivity.class);
        intent2.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        if (tag2 != null) {
            intent2.putExtra("url", tag2);
        }

        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationManager notificationManager2 = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID_1 = "1";
        String NOTIFICATION_CHANNEL_ID_2 = "2";

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel3 = new NotificationChannel(NOTIFICATION_CHANNEL_ID_1,"Critical Alerts",NotificationManager.IMPORTANCE_HIGH);
            notificationChannel3.setDescription("Critical Warnings from your Tubs");
            notificationChannel3.enableLights(true);
            notificationChannel3.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationManager2.createNotificationChannel(notificationChannel3);

            NotificationChannel notificationChannel4 = new NotificationChannel(NOTIFICATION_CHANNEL_ID_2,"Status Updates",NotificationManager.IMPORTANCE_LOW);
            notificationChannel4.setDescription("Status and Changes");
            notificationChannel4.enableLights(true);
            notificationChannel4.setVibrationPattern(new long[]{0,1000,500,1000});
            notificationManager2.createNotificationChannel(notificationChannel4);
        }
        NotificationCompat.Builder notificationBuilder2 = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID_1);
        notificationBuilder2.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title2)
                .setContentText(body2);
        notificationBuilder2.setStyle(new NotificationCompat.BigTextStyle()
                .bigText(body2));
        notificationBuilder2.setContentIntent(pendingIntent2);
        notificationManager2.notify(new Random().nextInt(),notificationBuilder2.build());
    }
}

My JSON sent to FCM HTTP v1

{
  "message": {
    "token": "dWdgiBqeZCM:APA91bEcufQdXdntHM8uN7PiqTEN2n_CxBF-lbqtC3yFJyF1p8c5qUzf5AfAa33XtQ30bOZxUD1423ia9i071rYvKh8MMGhEhG_DMeQ7EYTIryHoFILlGoTPOuLjdLy-8hqbYdE-MvgF",
    "android": {
      "notification": {
        "title": "Test Title!",
        "body": "Test Body!",
        "channel_id": "1",
        "tag": "https://example.com/"
      }
    }
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Zeb
  • 356
  • 2
  • 8
  • 1
    Does this answer your question? [How to handle notification when app in background in Firebase](https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) – Ashwini Saini Jan 23 '20 at 07:43
  • Thanks for the references I did have it working and so I needed to be more clear that I have it working when using the data tag so "The main point of using the notification: area is so I can utilize the channel_id" because it doesn't use the channel_id when placed in the data tag and so it uses my default high priority channel. I am looking to send a URL push in the data field and have it hit my channel_id without using the notification tag. Thanks everyone! – Zeb Jan 24 '20 at 16:46

2 Answers2

0

You need to change in json;

 {
   "message": {
"token": "dWdgiBqeZCM:APA91bEcufQdXdntHM8uN7PiqTEN2n_CxBF-lbqtC3yFJyF1p8c5qUzf5AfAa33XtQ30bOZxUD1423ia9i071rYvKh8MMGhEhG_DMeQ7EYTIryHoFILlGoTPOuLjdLy-8hqbYdE-MvgF",
"android": {
  "notification": {     //Here you need to change here, 'notification' to 'data'
    "title": "Test Title!",      
    "body": "Test Body!",       
    "channel_id": "1",            
    "tag": "https://example.com/" 
    }
    }
  }
}
ashok
  • 431
  • 5
  • 8
  • Thanks @ashok that is working but my main question which I should have put in the subject DOH! is how to pass channel_id without using the notification tag. It does work open or closed and passes the URL correctly but doesn't listen to my channel_id unless using notification and if I use notification it doesn't listen to my URL. Basically looking for everything above using the data tag but having it listen to my channel_id and not ignore it – Zeb Jan 24 '20 at 17:01
0

You FCM Json contain notification tag that's why onMessageReceived in not call when app is closed. So remove this notification tag

 "notification": {
    "title": "Test Title!",
    "body": "Test Body!",
    "channel_id": "1",
    "tag": "https://example.com/"
  }

Use data tag instead of notification just like this

 "data": {
    "title": "Test Title!",
    "body": "Test Body!",
    "channel_id": "1",
    "tag": "https://example.com/"
   }

And get data field by this remoteMessage.getData()

Rahul sharma
  • 1,492
  • 12
  • 26
  • Thanks @frankenstein. You are correct because when I was testing before it works great if the app is open or closed as long as I just use the data: tag however when I specify channel_id in the data: tag it is ignored and FCM shows channel_id is only supported in the notification: area of the JSON. I am wanting to send everything in body but the second I use that tag it behaves different and uses the default channel. Maybe I need to add something to my service to find channel_id in the data tag but don't know what that would be. – Zeb Jan 24 '20 at 16:44