1

Short version:

I want to create a notification that is uninterruptible by all other notifications, SMS messages, etc until the user clears it.

Long version:

I'm using Firebase Cloud Messaging to send alerts to my phone. Messages are handled based on the topic, and I need to make the "alarm" messages repeat continuously until the notification is cleared by the user. This is currently accomplished by setting the notification with FLAG_INSISTENT, which loops the sound.

The problem is the insistent "alarm" notification is permanently stopped when a different notification or SMS comes through.

I would like to ignore all new notifications, or restart the alarm after the new notification plays. I've searched for a couple hours and tried several flags and settings, but can't figure out how to do it.

Here is the code I'm using to handle FCM messages and set the notifications:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData() != null){
       String messageTopic= remoteMessage.getFrom();
       String messageTitle = remoteMessage.getData().get("title");
       String messageBody = remoteMessage.getData().get("message");

       if (messageTopic.equals("/topics/news")) {
         Intent intent = new Intent(this, MainScreenActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

         Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

         NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
              .setSmallIcon(R.drawable.ic_stat_news)
              .setContentTitle(messageTitle)
              .setContentText(messageBody)
              .setAutoCancel(true)
              .setSound(soundUri)
              .setContentIntent(pendingIntent)
              .setPriority(Notification.PRIORITY_MAX);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(30548, notificationBuilder.build());
    }
    else if (messageTopic.equals("/topics/alarm")) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.example.com/alarm.php"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_alarm)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent)
                .setPriority(Notification.PRIORITY_MAX);

        Notification mNotification = notificationBuilder.build();
        mNotification.flags |= Notification.FLAG_INSISTENT;

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(30549, mNotification);
    }
  }
}

I may be able to prevent my own notifications from interrupting the alarm by ignoring them when the alarm notification ID (30549) is active (or create the new notification and then create the alarm again). However, SMS and notifications from other programs will still interrupt, so that wouldn't be a perfect solution.

shinypenguin
  • 128
  • 5
  • Did you solve it somehow? – Pepa Zapletal Aug 15 '23 at 18:45
  • @PepaZapletal Looks like I ghosted this post. I ended up checking for an active alarm when sending a notification from my app and then re-triggered the alarm. I could only do this for new notifications from my app, so it still broke on notifications from other apps. Not ideal, but good enough for personal use. I have not actively worked on this for several years, so there are likely better solutions. – shinypenguin Aug 24 '23 at 19:17

1 Answers1

0

I don't think you'll have luck with this, unfortunately. Android will not let you have a notification that takes full priority from my understanding.

However, in the Firebase console when you go to the notifications section and click advanced you can set the priority of a notification to High and set a sound.

Since it looks like you're writing the notification programmatically and setting priority to Max this will achieve the same affect.

Setting priority to high, from Google's documentation:

When a high-priority notification arrives (see right), it is presented to users for a short period of time with an expanded layout exposing possible actions.

After this period of time, the notification retreats to the notification shade. If a notification's priority is flagged as High, Max, or full-screen, it gets a heads-up notification

Also check out this SO for someone who's notification wouldn't go away when they wanted it to here

Community
  • 1
  • 1
Debacle
  • 1,181
  • 1
  • 9
  • 12
  • Thanks for the response. I am sending the messages from a server specifying "priority" high, sending "to" the topic, and sending data fields only (not notification fields), to ensure the app handles the message even in the background. I'm pretty sure the messaging part of the app is working and it's just the notification part of it that needs to be changed somehow. I'm not sure how the other SO question relates to this one. The alarm notification persists in the task bar, but the sound stops once a new notification plays. – shinypenguin Jun 06 '16 at 00:16
  • Maybe try creating a service that then plays some audio, that would require a notification to be displayed, like when an app is playing music. – Arthur Thompson Jun 07 '16 at 20:51