1

I'm able to send a push notification to my App with FCM services. I'm doing that using POSTMAN/INSOMNIA.

Everything is working fine. I can see the notification. But I need some advice / point to the right direction when it comes to processing the incoming message.

I've set up everything using the FCM official documentation.

My server call looks like this:

{
    "to":"...",
    "data" : {
        "sound" : "default",
        "body" :  "Message body",
        "title" : "My APP",
        "content_available" : true,
        "priority" : "high",
        "my_message": "123456"
    }
}

I can access the data in the onMessageReceived() method:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData().size() > 0) {
        // get incoming data    
    }
}

So far nothing extraordinary. What I'd like to achieve is if my Activity is in foreground I don't need to display the taskbar notification, just to show the message which comes with my FCM message (the my_message key)) inside my Activity. Is that possible?

If the app is in background I'd like to open a Activity (that I'm able to do) and pass to the Intent my data (the message above) This is what I'm struggling.

If the 'App in Background' scenario happens, my notification is displayed this way:

private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    String channelId = "my_channel_id";
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_person)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "DG_Channel",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0, notificationBuilder.build());
}

So my 3 main questions are:

  • can I tell if my app is in foreground/background when the notification arrives?
  • If in foreground how can I access the data from activity
  • if in background, how can I pass data to the activity

Thank's for any advice

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dusan
  • 3,284
  • 6
  • 25
  • 46

1 Answers1

1

To let your app know about incoming data, register a BroadcastReceiver which is tied to your activity's lifecycle: Take a look Here

If your app's in background, you can get it stored in a Database / SharedPreferences depending on your use case, and check for it's presence everytime the app is opened.

About determining whether your app's in FG: Here

If it's in Foreground, send a Custom Broadcast that your Activity is bound to receive.

If it's in Background, store it, and work on it the next time app is opened.

Arvind
  • 724
  • 5
  • 6
  • I'm not sure how can I register a BroadcastReceiver for a incomming message sent from Firebase. I've been using a to do that. I just need to send data to the app form an API. If there's another way around, i don't need to use Firebase. – Dusan Oct 09 '20 at 08:03
  • You'd require to send a broadcast with your custom action string (say "your.package.ACTION") from the FCM Service. If your app is in Foreground and has registered a receiver for the same action, then it'd receive that broadcast. – Arvind Oct 09 '20 at 10:53
  • 1
    I followed your advice and it works like I wanted to. Thank yoy! – Dusan Oct 12 '20 at 13:21