How to handle a fire base notice in the background as well as in the foreground?

I want to process a benefit notification notification in the background as well as in the foreground. I will send a message that will contain a link to YouTube from the developer, and when the user clicks on the notification panel, he must direct the user to open the link. Does anyone know how to do this?

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:)

+5
source share
4 answers

You must send a data payload in your FCM message. The data payload is accepted in the messaging method, regardless of whether your application is in the foreground or in the background. Process action there. Like notification of impressions, always reading the data payload, or if you want to show a warning dialog when your application is open or in the foreground.

here is an example 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 the showNotificationWithURLAction (...) method by doing it. One sample here

+4
source

This is a possible duplicate of this question . Do not send a โ€œnotificationโ€ to the body of your message.

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

 { "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 application calls onMessageReceived () every time even in the background, and I just changed the methods of using the received notification header and message in the push data.

+2
source

If you want to redirect the user to content in the application than using deepLink, here's how to simply open the link when you click on the notification:

 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 the payload, go to the firebase console, create a new message, here you will have additional parameters, where you can put your data in key / value pairs, put the url, as in the photo below.

Adding a payload to a Firebase post

0
source

The Android system will always process messages inside the notification field in the payload. If you want your application to handle the notification, you need to put your YouTube link in this data field in your payload.

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

This notification was received in the application as RemoteMessage. Use remoteMessage.getData () to get the youtube link.

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

0
source

Source: https://habr.com/ru/post/1265532/


All Articles