Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Notification white circle issue [duplicate]

I've a problem with Firebase Notification, If I send notification when app is running on screen the notification show correctly like this:

enter image description here

enter image description here

Than if I send notification when app in running on background, the notification appears like this:

enter image description here

enter image description here

This is my FirebaseMessagingService class

    public class AppFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification(remoteMessage);
    }

    private void sendNotification(RemoteMessage remoteMessage) {
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        int icon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? R.drawable.ic_video_label_white_24dp: R.drawable.ic_launcher;
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setWhen(System.currentTimeMillis());
        notificationBuilder.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
        notificationBuilder.setSmallIcon(icon);
        notificationBuilder.setContentTitle(remoteMessage.getNotification().getTitle());
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(defaultSoundUri);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            Intent resultIntent = new Intent(this, MainActivity.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(MainActivity.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
            notificationBuilder.setContentIntent(pendingIntent);
            notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
        }

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

Why this happens?

like image 687
Michele Lacorte Avatar asked Jun 24 '16 07:06

Michele Lacorte


People also ask

Is there a limit for Firebase notifications?

You can send up to 240 messages/minute and 5,000 messages/hour to a single device.

What is Senderid in FCM?

The Sender ID is used in the client side application to register to FCM from the device. Copy the Server Key. You must provide the Server Key in the Engagement server while configuring an application to send push messages.

Can FCM notification on Android overwrite previous one?

It's possible. Two approaches. First is you make use of the collapse_key parameter to set the message as a collapsible message.

Can I send push notifications without FCM?

Without FCM, you'd need to build more than three notification systems. You'd need one each for iOS and Android, but then you'd also need to accommodate all the different types of browsers for the web application to deliver push notifications without a hitch.


2 Answers

This is bug in Firebase which is not resolved yet. Link here: https://stackoverflow.com/a/37332514/1507602

Another alternative is to not to use Firebase console to send notification, instead use POST API, that way your notification will be delivered directly to onMessageReceived() where you can create your own Notification.

To send a data payload message you have to make a curl request:

HTTP POST Request

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

You can get the server key (AIzaSyZ-1u...0GBYzPu7Udno5aA), from firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key

like image 119
Hisham Muneer Avatar answered Sep 28 '22 21:09

Hisham Muneer


The reason, why it is happening is way how Firebase Notifications are working, and different behavior, when app is in foreground and different when in background:

  1. Foreground - here is compatibility, with this, what was in past and GCM - you are fully controlling and your code from AppFirebaseMessagingService is executed
  2. Background - in this case system/library is responsible for displaying message, and is using for this main icon of app. If you have any data in data node, then it is passed as intent to main activity of your app (after user will press notification)

Due to this - I decided in my app not using it as: 1. I cannot control it 2. Is used default icon of app (that in my case was also looking like yours - rounded 3. I cannot make additional things with this (in my case I was also showing image + adding action, and with Firebase notifications - I cannot make this, when app is in background)

like image 42
Mateusz Pryczkowski Avatar answered Sep 28 '22 23:09

Mateusz Pryczkowski