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.
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
Related
I have an online JSON file that shows the latest country earthquakes, the listView adapter in my app receives data from this JSON file and the refresh is manually by user`s swipe, but now I am adding an automatic and timely basis refresh mode to my list, the questions is:
How can I create a system notification after a new quake is detected?
Write this below code for sending System notification in your app while your data is loaded from network request.
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("App Name / Title Message")
.setContentText("Description Message hat data is updated")
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_notification_icon)
.setColor(getResources().getColor(R.color.icon_color))
.setPriority(Notification.PRIORITY_HIGH) // will show notification even you are in app
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_SHOW_LIGHTS;
NotificationManager notificationManager = (NotificationManager)
getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
//you can use unique identification int number for notificationID instead of 1
putting this code block will show notification.
Just build a Notification like this
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
And then just show it
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, mBuilder.build());
And for anything other extras check the official documentation
In order for this to work you somehow should push when a new earthquake happens to your phone. If you have backend, you can send a push notification through the cloud messaging.
If you don't have backend just create a background service and pull data every now and then and when data says new earthquake jsut follow the notification creation from above.
hey all i got a tough problem and need advice. I have constructed a notification manually after recieving a FCM data payload. This is how the notification gets created both in foreground and background since its a data payload:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
String msg = remoteMessage.getData().get("message");
sendNotification(msg);
}
private PendingIntent createIntent(String msg) {
Intent intent = new Intent(this, SportsActivity.class);
Bundle b = new Bundle();
b.putString(Constants.KEY_GO_TO_TAB, Constants.KEY_DASHBOARD_HOCKEY_SCORE_TAB);
intent.putExtras(b);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
return pendingIntent;
}
private void sendNotification( String messageBody) {
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody))
.setContentText(messageBody)
.setColor(ContextCompat.getColor(this, R.color.hockey_brand))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(createIntent(messageBody));
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
it seems to function fine. The issue im having is i want the notification to NOT SHOW in when the app is in the foreground. is there not a way to do this without scanning the activity task ? as this requires a permission
i've read this article but this how to know you've went from foreground to background. also android.permission.GET_TASKS is deprecated and REAL_GET_TASKS permission is not for third party either. I simply want to know at any given time that the user is either in foreground or background so i know if i should show a notification or not. I wonder if firebase itself has something. When you send a "Notification payload" from the firebase console if the app is not in the foreground is does not show in the notification panel so there should be a way to do this.
Alternatively you can choose not to show your notification when your app is in foreground by implementing Application.ActivityLifecycleCallbacks
in your Application.
Then you can check if your app is in foreground or background.
Source
I wrote the below method for showing FCM notification on my android 5.1 device. When I run the code inside a FirebaseMessagingService it is just giving single line notification , where if I run the same code inside my Activity its giving expandable notifications.
I basically need my long FCM text notifications to get expanded on notifications rather than showing partial notification text.
Any leads?
private void showNotif(String messageBody){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
// Constructs the Builder object.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
.setAutoCancel(true)
.setContentIntent(pendingIntent)
/*
* Sets the big view "big text" style and supplies the
* text (the user's reminder message) that will be displayed
* in the detail area of the expanded notification.
* These calls are ignored by the support library for
* pre-4.1 devices.
*/
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody));
// android.support.v4.app.NotificationManagerCompat mNotifManager = (NotificationManagerCompat) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
This should work .
Big Text notifcations are shown in normal mode when there are other notifications on top . Try to expand it while testing.
You need to creare notification using this method https://fcm.googleapis.com/fcm/send and not using firebase console and all would be ok
I have a service that shows a notification PendingIntent each time it receives a new GCM message. The problem is that the GCM messages can be of different kinds. And if many notifications leave unread, I want not to show them separately but in groups like:
you have 3 unread messages of type A
you have 2 unread messages of type B
you have 4 unread messages of type C
As far as I understand, to get this effect I need to have an access to unread/unseen notifications. Each time when I new notification comes I can check, if there is another unread message of this type, and then decide, whether I create a new notification or update an old one.
My question is: is there a way to see, which notifications are unseen and get access to them?
For any case this is my method to create a message; if an argument notificationId is 0 a new notification should be created. Else - updated.
private int sendNotification(String msg, Integer notificationId) {
Log.d(TAG, "sending message with text: "+msg);
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Random random = new Random();
int notification_id = notificationId==0?random.nextInt(9999 - 1000) + 1000:notificationId;
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.notification);
Intent intent = new Intent(this, MainActivity.class);
// Send data to NotificationView Class
intent.putExtra("text", msg);
PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("escos")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(pending);
mBuilder.setContent(remoteViews);
remoteViews.setTextViewText(R.id.notiftext, msg);
remoteViews.setImageViewResource(R.id.notifim, R.drawable.ic_launcher);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager.notify(notification_id, notification);
return notification_id;
}
For different Notification strip (A, B, C etc.) in your status bar, use different NOTIFICATION_ID for building the Notification on basis of your defined type or collapse_key received from GCM.
For determining unread and read messages, use a local variable (counter) in Shared Preferences and increment it each time a specific type of Notification comes (on basis of defined type or collapse_key).
Then generate the Notification with that particular NOTIFICATION_ID as Notification with particular NOTIFICATION_ID can override each other. So You can override the previous Notification with Iterative Numbered text in New Notification.
As soon as user click on any Notification or particular Notification, clear the notification and reset the value of (counter) in Shared Preferences.
Edit1 : When you click on Notification with particular Pending Intent, then in that Activity use this code for removing all the Notifications generated from your app :
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try {
nMgr.cancelAll();
} catch (Exception e) {
e.printStackTrace();
}
Note : Do remember to add Try-Catch before you call cancelAll() as cancelAll() may not be supported by the device model and will generate
java.lang.SecurityException: Permission Denial
error.
Edit 2:
You can also use nMgr.cancel(NOTIFICATION_ID); to clear a specific notification, pass NOTIFICATION_ID to particular intent via extras and get the extras in that activity to cancel a particular notification.
And as you click on any notification it will be cleared from status bar unless you have not set .setAutoCancel(false) in your Notification Builder.
On lollipop, If the user has a pin set then some notifications aren't able to be swiped away. they act like a persistent notification when attempting to be dismissed.
Some apps notifications are able to be swiped away on the lockscreen without unlocking.
I have only tested this with hide sensitive notification content, does a flag set change this ability?
How do I achieve this?
This flag makes your notification stick:
Notification.FLAG_ONGOING_EVENT;
If you leave it out you can remove it. If you put it in, it sticks.
FYI: How I make my notifications:
//These are parameters for setting up the tag in the tray
private static final String NOTIFICATION_ID_TAG="notificationID";
private static final int NOTIFICATION_ID=123456;
public void createNotification() {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, MainActivity_Host.class);
intent.putExtra(NOTIFICATION_ID_TAG, NOTIFICATION_ID);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
Notification noti = new Notification.Builder(this)
.setContentTitle("Service Running")
.setContentText("The service is running").setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//THIS FLAG MAKES THE NOTIFICATION STICK = YOU CAN'T SWIPE IT AWAY... IF YOU LEAVE IT OUT YOU CAN REMOVE THE NOTIFICATION
noti.flags |= Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(NOTIFICATION_ID, noti);
}
This is how you can remove the notification from within the code:
//Erase the notification that we set up when the service started
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
As you see, you can get the notification from the NotificationManager by the NOTIFICATION_ID you used to make it. This is just a number i made up.