Welcome to last part of Android Push Notifications using Firebase tutorial. In previous articles, we have discussed following:

If you have not yet completed Android app or app server integration with FCM, I strongly advise to follow above articles before proceeding with this.

Handling Messages in Android App

Firebase notifications may act differently depending on the foreground/background state of the receiving app. To receive FCM push notification messages in foregrounded apps, use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived callback, which is provided for most message types, with the following exceptions:

  • Notifications delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
  • Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

In nutshell, we have following:

App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.

Edit the app manifest

To use FirebaseMessagingService, you need to add the following in your app manifest:

<service
 android:name=".MyFirebaseMessagingService">
 <intent-filter>
 <action android:name="com.google.firebase.MESSAGING_EVENT"/>
 </intent-filter>
</service>

Add these lines inside the application tag to set the custom default icon and custom color:

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
 See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
 android:name="com.google.firebase.messaging.default_notification_icon"
 android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
 notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
 android:name="com.google.firebase.messaging.default_notification_color"
 android:resource="@color/colorAccent" />

Android displays the custom default icon for

  • All notification messages sent from the Notifications console.
  • Any notification message that does not explicitly set the icon in the notification payload.

Android uses the custom default color for

  • All notification messages sent from the Notifications console.
  • Any notification message that does not explicitly set the color in the notification payload.

If no custom default icon is set and no icon is set in the notification payload, Android displays the application icon rendered in white.

Override onMessageReceived

By overriding the method FirebaseMessagingService.onMessageReceived, you can perform actions based on the received RemoteMessage object and get the message data:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...

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

Handle notification messages in a backgrounded app

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

Need further help? If you’re confused or facing any issue while completing first step, let’s know in comments.


Like it? Share with your friends!

attacomsian

A passionate programmer, domainer, writer and photographer! Love writing about web development, latest trends in technology and domain name industry. Working hard to make the world a better place! Follow me at Twitter @attacomsian or visit my website www.attacomsian.com

175 Comments

Your email address will not be published.

  1. Pingback: generic plaquenil
  2. Pingback: tab ivermectil 6
  3. Pingback: stromectol kidney
  4. Pingback: rayos 250 capsule
  5. Pingback: cialis symptoms
  6. Pingback: generic tadalafil
  7. Pingback: ivermectin ireland
  8. Pingback: Anonymous
  9. Pingback: Anonymous
  10. Pingback: Anonymous
  11. Pingback: ivermectin buy
  12. Pingback: order ivermectin
  13. Pingback: goodrx sildenafil
  14. Pingback: tadalafil prix
  15. Pingback: order cialis
  16. Pingback: ivermectine kopen
  17. Pingback: cialis online
  18. Pingback: molnupiravir cost
  19. Pingback: ivermectin india
  20. Pingback: generic cialis
  21. Pingback: buy viagra online
  22. Pingback: iver mectin
  23. Pingback: ivermectin generic
  24. Pingback: generic
  25. Pingback: ivermectin 4 mg
  26. Pingback: cheap stromectol
  27. Pingback: cost of stromectol
  28. Pingback: ivermectin japan
  29. Pingback: ivermectin 1%cream
  30. Pingback: lucky slots
  31. Pingback: dengue ivermectin
  32. Blogger/Blog*Spot feeds shows the most recent (recently updated as the default setting) 25 headlines or what others call as post titles. My question is how can I increase/decrease the number of those headlines because I think they’re too many..

  33. Pingback: mylan tadalafil
  34. Pingback: india ivermectin
  35. Pingback: iver mectin
  36. Pingback: 2powered
  37. Pingback: cialis walmart
  38. Pingback: confeitofilm
  39. Pingback: A片
  40. Pingback: film
  41. Pingback: madridbet
  42. Pingback: madridbet
  43. Pingback: madridbet
  44. Pingback: elexusbet
  45. Pingback: meritking
  46. Pingback: madridbet
  47. Pingback: meritking
  48. Pingback: madridbet
  49. Pingback: porn
  50. Pingback: meritking giriş
  51. Pingback: child porn
  52. Pingback: porn
  53. Pingback: meritking
  54. Pingback: izmir escort
  55. Pingback: porn
  56. Pingback: child ponr
  57. Pingback: porn
  58. Pingback: sex
  59. Pingback: çeşme transfer
  60. Pingback: izmir travesti
  61. Pingback: hd porno izle
  62. Pingback: fuck google
  63. Pingback: yasam ayavefe
  64. Pingback: travesti.site
  65. Pingback: porna
  66. Pingback: casinno porna
  67. Pingback: betgross giriş
  68. Pingback: lugabet giriş
  69. Pingback: casino porna
  70. Pingback: jojobet
  71. Pingback: jojobet twitter
  72. Pingback: sex
  73. Pingback: animal porn
  74. Pingback: porno
  75. Pingback: bursa travesti
  76. Pingback: Escort bayan
  77. Pingback: fuck google