1

I'm using new Firebase Cloud Messaging and sending messages from server including both notification and dat. I have following questions about that:

  1. onMessageReceived is not called when app is in background and notification created so how i can detect that notification and get its data without waiting for user to click and get data from intent.

  2. I have set "click_action" in notification to launch activity when notification called but it will recreate activity even if it is already running how avoid that?

William
  • 225
  • 4
  • 21
  • Same problems for me. I'am unable to get notification data without waiting for user to click(if activity is open). – Jitesh Prajapati Aug 06 '16 at 09:35
  • If an app is in the background and received a *notification* message, that message is not delivered to the app. Instead Firebase displays a message in the notification tray, which starts the app when clicked. See http://stackoverflow.com/questions/37809885/firebase-push-notifications-update-db/37843845#37843845 – Frank van Puffelen Aug 06 '16 at 15:30

2 Answers2

0

You can use intent flags to get rid of that second problem, you mentioned. Refer to this link for the available flags: https://developer.android.com/reference/android/content/Intent.html

Then, you can use those flags like this,

Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

when starting an activity.

And for the third problem you listed, you can assign pending intents to the notification which will get fired after clicked.

Intent intent = new Intent(this, FNotiReceiver.class);
intent.setAction("com.nepal.application.pendingIntent");
intent.putExtra("Name", "Data/Value");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notification_ID,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

and to remove the notification after clicking it, you can use:

NotificationManager notifManager= (NotificationManager)  context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancel(notification_ID);

Sorry I don't have the exact answer to your 1st issue, but I think creating a background service will help.

Karun Shrestha
  • 731
  • 7
  • 16
-1

Firebase notification handle by,

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
            Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

For more details https://firebase.google.com/docs/notifications/android/console-device

Shanto George
  • 994
  • 13
  • 26