0

So I got push-notifications working with Google's Firebase Cloud Messaging. The only problem now is that the notification doesn't show any alert, only if I pull down the notification drawer that I see it's there.

I've got this part of the code where I think that "popup" feature is added

public void displayNotification(String title, String body){
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext, Constants.CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(body);

        Intent intent = new Intent(mContext, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        if(mNotificationManager != null) {
            mNotificationManager.notify(1, mBuilder.build());
        }

    }

Other problem I have is that when I click the notification, it opens the activity but doesn't delete the notification.

Tiago Vitorino
  • 108
  • 1
  • 15

1 Answers1

2

Foreground pop-up

Under the builder, you are required to set high or max priority as well as the default notification vibration / sound so that you can see the 'pop-up' window

.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL);

Background pop-up

To achieve background pop-up, you need to fine-tune your FCM payload. If you have both data and notification in your payload, the pop up cannot be handled by your displayNotification method. You will need a data only payload.

Google has placed this behavior in the documentation. enter image description here Reference - FCM for android: popup system notification when app is in background

AutoCancel

For your second issue, add the setAutoCancel in your builder

.setAutoCancel(true)

Extra note

For some devices like Xiaomi and Redmi, you need to go to Settings to enable floating notification

enter image description here

jackycflau
  • 1,101
  • 1
  • 13
  • 33