How to handle firebase notification on background

2019-06-20 07:25发布

问题:

I want to handle firebase notification message on background as well as on foreground . I will send a message that will consist a youtube link from developer and when user tap on notification bar it must direct user to open the link. Does anyone knows how it is done?

 public void onMessageReceived(RemoteMessage remoteMessage) {
    // [START_EXCLUDE]
    // There are two types of messages data messages and notification messages. Data messages are handled
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
    // is in the foreground. When the app is in the background an automatically generated notification is displayed.
    // When the user taps on the notification they are returned to the app. Messages containing both notification
    // and data payloads are treated as notification messages. The Firebase console always sends notification

    // [END_EXCLUDE]

    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be:
    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.
}

what to do next to achieve my goal? Thanks in advance:)

回答1:

You should send data payload in your FCM message. Data payload gets received in on message method irrespective of your app being in foreground or background. Handle the action there. Like show notification by reading the data payload always, or if you want show an alert dialog when your app is open or in the foreground.

here is a sample payload:

{
  "to": "registration_id_or_topic",
  "data": {
        "message": "This is a Firebase Cloud Messaging Topic Message!",
        "youtubeURL": "https://youtu.be/A1SDBIViRtE"
   }
}

Then in your onMessageReceived:

public void onMessageReceived(RemoteMessage remoteMessage) {
   if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        Map<String, String> receivedMap = remoteMessage.getData();
        String youtubeURL = receivedMap.get("youtubeURL");
        showNotificationWithURLAction(youtubeURL);
   }
   .....
}

you can easily implement showNotificationWithURLAction(...) method by googling it out. One sample is here



回答2:

This is a possible duplicate of this question. Don't send a "notification" in your push message body.

So basically, I changed the body for the push notification request from:

{
   "data": {
       "type" : "mytype"
    },
    "notification": {
        "title": "My Title",
        "body": "My Notification Message"
    },
    "to": "/topics/all"
}

To:

{
   "data": {
       "type" : "mytype",
       "title": "My Title",
       "body": "My Notification Message"
    },
    "to": "/topics/all"
}

Now my app calls onMessageReceived() everytime even in background, and I just changed the methods to use the obtained notification title and message in the push data.



回答3:

The android system will always handle messages inside the notification field in the payload. If you want your app to handle the notification, you need to put your YouTube link inside that data field in your payload.

{ "data" : 
    { "link" : "www.youtube.com"}
}

This notification is received on the app as a RemoteMessage. Use remoteMessage.getData() to get you youtube link.

See https://firebase.google.com/docs/cloud-messaging/concept-options#messages-with-both-notification-and-data-payloads



回答4:

If you want to redirect user to content in your app than use deepLink, here is how to just open link when notification is clicked:

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setData(Uri.parse(remoteMessage.getData().getString("url")); 
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
         // Resources r = getResources();
          Notification notification = new NotificationCompat.Builder(context)
                  .setTicker("yortext")
                  .setSmallIcon(android.R.drawable.ic_menu_report_image)
                  .setContentTitle("title")
                  .setContentText("content")
                  .setContentIntent(pendingIntent)
                  .build();

          NotificationManager notificationManager =  (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
          notificationManager.notify(0, notification);

To send payload go into firebase console, create new message, here you will have advanced options, where you can put your data in key/value pairs, put url as on photo below.