Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle firebase notification on background as well as foreground?

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

like image 228
vishal gupta Avatar asked Mar 16 '17 07:03

vishal gupta


People also ask

How do I handle the Firebase notification when an app is in foreground?

Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.

How do you handle background notification in react-native Firebase?

Handling background message notifications To add the handler, make the following changes on App. js before App() : // Register background handler // Get the notification messaging(). setBackgroundMessageHandler(async remoteMessage => { // Extract the body let message_body = remoteMessage.

What are the two types of notifications in Firebase?

Message types. With FCM, you can send two types of messages to clients: Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically. Data messages, which are handled by the client app.

How do I show notification when an app is in foreground Android flutter?

To handle messages while your application is in the foreground, listen to the onMessage stream. FirebaseMessaging. onMessage. listen((RemoteMessage message) { print('Got a message whilst in the foreground!


2 Answers

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

like image 144
drulabs Avatar answered Nov 15 '22 04:11

drulabs


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.

like image 43
Alvin Rusli Avatar answered Nov 15 '22 04:11

Alvin Rusli