Heads up notification do not work sometimes
Asked Answered
L

2

3

I make a head up notification this way:

Notification.Builder nb = new Notification.Builder(context)
        .setSmallIcon(icon)
        .setContentTitle("Title")
        .setContentText("Content")
        .setDeleteIntent(delete)
        .setPriority(Notification.PRIORITY_HIGH)
        .setVibrate(new long[0]);

notificationManager.notify(1, nb.build()); // TODO hardcode

First time I install application on a testing device, notifications are heads up, but if I expand notification area (while heads up is still running) and dismiss the notification from there, next time notifications are not heads up. After reinstalling the app notifications are heads up again. Are there any reasons why heads up behaviour can not be constant?

Lapham answered 30/12, 2015 at 21:4 Comment(0)
G
10

Heads up notifications have a built in rate limiting - if the user swipes your heads up notification up (putting it back into the notification tray) or to the side (dismissing it), then that signals the system to prevent further heads up notifications for some period of time (~a minute by default).

Goldbrick answered 30/12, 2015 at 21:49 Comment(2)
@FlashspeedIfe - feel free to file a documentation bugGoldbrick
Are you sure that this will happen ? I am facing the same issue in android 5.0 device alone. Have any documentation link for this. Any updates would be helpfullVirgievirgil
B
0

I hope the below code will help you most to understand which properties ate most important to display heads-up notifications and also by using this code I didn't find the issue of not working sometimes.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification(remoteMessage.getNotification().getTitle(),     
    remoteMessage.getNotification().getBody(),remoteMessage.getData());
}

private void sendNotification(String messageTitle, String messageBody, 
Map<String, String> data) {

Intent intent = HomeActivity.getHomeActivityIntent(this,data.get(Constants.PUSH_URL));

   PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
           PendingIntent.FLAG_ONE_SHOT);

   NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1")
           .setSmallIcon(R.drawable.icon_notification)
           .setContentTitle(messageTitle)
           .setContentText(messageBody)
           .setContentIntent(pendingIntent)
           .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE) //Important for heads-up notification
           .setPriority(Notification.PRIORITY_MAX); //Important for heads-up notification

   Notification buildNotification = mBuilder.build();
   int notifyId = (int) System.currentTimeMillis(); //For each push the older one will not be replaced for this unique id

   // Since android Oreo notification channel is needed.
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
       String name = getString(R.string.channel_name);
       String description = getString(R.string.channel_description);
       int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
       NotificationChannel channel = new NotificationChannel(getResources().getString(R.string.default_notification_channel_id),
               name,
               importance);
       channel.setDescription(description);
       channel.setShowBadge(true);
       channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
       NotificationManager notificationManager = getSystemService(NotificationManager.class);

       if (notificationManager != null) {
           notificationManager.createNotificationChannel(channel);
           notificationManager.notify(notifyId, buildNotification);
       }
   }else{

       NotificationManager mNotifyMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
       if (mNotifyMgr != null) {
           mNotifyMgr.notify(notifyId, buildNotification);
       }
   }
}

To display heads-up, copy and paste the whole code and also fix errors based on strings and imports. After getting successful heads-up, remove or add anything as your requirement.

You may also follow my post on medium at this link for more detail answer.

Blintz answered 26/2, 2019 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.