Android notifications when the app is in the background

I am sending a push notification from google firebase for my Android application with the goal of Android 5.0:

My push notification code:

@Override public void onMessageReceived(RemoteMessage remoteMessage) { String badge = "0"; Uri uri = Uri.parse( getString(R.string.app_host_name) ); Map<String, String> data = remoteMessage.getData(); if (data.size() > 0) { try { uri = Uri.parse( data.get("link") ); badge = data.get("badge"); } catch (NullPointerException e) { // } } if (remoteMessage.getNotification() != null) { RemoteMessage.Notification notification = remoteMessage.getNotification(); sendNotification(notification.getTitle(), notification.getBody(), uri.toString(), badge); } } private void sendNotification(String title, String body, String url, String badge) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); if (Patterns.WEB_URL.matcher(url).matches()) { intent.putExtra("link", url); } PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT ); Resources resources = getApplicationContext().getResources(); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "default") .setColor( resources.getColor(R.color.colorPrimaryDark) ) .setSmallIcon( R.drawable.ic_stat_icon ) .setContentTitle(title) .setContentText(body) .setAutoCancel(true) .setNumber(Integer.parseInt(badge)) .setLargeIcon( BitmapFactory.decodeResource( resources, R.mipmap.ic_launcher ) ) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= 26) { NotificationChannel notificationChannel = new NotificationChannel( "default", "Main notification channel", NotificationManager.IMPORTANCE_HIGH ); notificationManager.createNotificationChannel( notificationChannel ); } notificationManager.notify( 1, notificationBuilder.build() ); } 

And everything is super perfect when the application is active / open / not in the background, but when it is not, notifications are not grouped, there is no number displayed, and there is no reaction to all these settings at all, that I was able to change - this is just a small icon and circle color using manifest settings

  <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_stat_icon" /> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorPrimaryDark" /> 

but why? For example, when the application is in the background, notifications do not use the settings from the activity code, but only use some kind of "default" from AndroidManifest.

+2
source share
1 answer

As you said in the comments:

when the application is in the background, the application does not accept setNumber, setAutoCancel, setSmallIcon, setLargeIcon options

This is because you are using the payload to send a notification that only runs in the foreground.

Therefore, when your application is in the background, it does not introduce this method.

to solve this problem you can only use the data payload:

 "data": { "titles": "New Title", "bodys": "body here" } 

as the data payload will go into onMessageReceived() when your application is in the foreground / background.

then in fcm you can do this:

  if (remoteMessage.getData().size() > 0) { title = remoteMessage.getData().get("titles"); body = remoteMessage.getData().get("bodys"); } 
+2
source

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


All Articles