Android notification icon issue

2019-02-19 11:46发布

问题:

I have a strange issue. I have two way to send notifications in my Android app; one from the Android service and the other through FCM.

The scenarios are as follows:

  1. Regardless of whether the app is running or not, the icon of the notification sent from the Android service appears correctly.
  2. When the app is running, the notification icon appears still appears correctly if I send the notification via FCM.
  3. But if the app isn't running and I send the notification via FCM, a white square is displayed instead of the notification icon.

My code in FCMService:

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("Android App")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());

回答1:

Most likely your problem is the difference between notification-messages and data-messages.

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

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

Currently the FCM Web Console only sends notification-messages

So all the messages sent via Web Console (or via API with a notification payload) will be have in this way:

  1. if the app is closed or in background: FCM will display the notification. if you want to customize it you can, but you need to provide specific configuration (in the manifest or in the send API call) see https://firebase.google.com/docs/cloud-messaging/android/client#manifest
  2. if the app is in foreground: FCM will call onMessageReceived()

.

If the behavior that you want is that onMessageReceived() is always called:
then you need to use a data-only (no notification) message



回答2:

This is a FMC bug detailed in github fcm page.

https://github.com/firebase/quickstart-android/issues/4