Firebase notifications when the application is closed

I have implemented a Firebase notification in my Android app. When my application is running, a notification is displayed with my custom layout, but when the application is not running, a notification is displayed with the default layout. How to change the notification layout to my layout when the application is not running. In addition, I keep the general settings so that the user can switch notifications. But when the application does not start, a notification is displayed anyway. How can this be achieved?

@Override public void onMessageReceived(RemoteMessage remoteMessage) { if(SettingsFragment.getReceiceNotification()){ //if user wants to receive notification NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.push_notification_layout); remoteViews.setImageViewResource(R.id.push_notif_icon,R.mipmap.ic_bird_black); Intent intent = new Intent(this,MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); notificationBuilder.setContent(remoteViews); notificationBuilder.setContentTitle("Radyo Türkkuşu"); notificationBuilder.setContentText(remoteMessage.getNotification().getBody()); notificationBuilder.setAutoCancel(true); notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); notificationBuilder.setContentIntent(pendingIntent); remoteViews.setTextViewText(R.id.push_title, "Radyo Türkkuşu"); remoteViews.setTextViewText(R.id.push_context, remoteMessage.getNotification().getBody()); //notificationBuilder.setLights (ContextCompat.getColor(MainActivity.context, R.color.pushColor), 5000, 5000); notificationManager.notify(0,notificationBuilder.build()); } } 
+7
android firebase firebase-cloud-messaging firebase-notifications
source share
3 answers

Your problem is that you are using it with a notification tray.

See link

Messages with a payload of notifications and data, both in the background and in the foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in addition to the purpose of your launch activity.

If you use {data:"something"}(data-message) with {notification:"something"}(display-message) while your application is in the background, the data payload will be delivered in addition to the intention, but not the onMessageReceived() method. I assume that you are implementing your code to display a notification, so when your application is in the foreground onMessageReceived() is the trigger and it displays the desired notification, but if it is not onMessageReceived() , do not start the trigger, and the Android system will handle it with the payload of your notification. You simply remove {notification:"something"}(display-message/notification tray) from your server-side code to always guarantee onMessageReceived() .

For those who continue to mention onMessageReceived() , will always be triggered regardless of whether it is foreground or background, please visit the link

+16
source share

There are two types of FCM messages.

  • Notification message
  • Data message

Messages sent from the Firebase console, Notification . To receive a message in onMessageReceived (), use a Data Message . Use the server side code below to send a message notification message

 { "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data" : { "Nick" : "Mario", "body" : "great match!", "Room" : "PortugalVSDenmark" }, } 

Link https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

+7
source share

Your code will never run when your application is closed or running in the background. See this: Firebase Notice

What you need to do is check how the application starts, click a notification, or click the start icon. To do this, some data must be added to the notification, and then retrieved at the first run in your application. If you can receive them successfully, it means that your application is launched by clicking on the notification, then you can do what you want.

0
source share

All Articles