I have a foreground service with a notification, when updating this notification on a Wear device if the user is on the watch face the notification is always brought to the front, having more than one notification updating then they are constantly fighting for the screen.
Happens on Android Wear OS by Google - Version 1.2; Android OS - Version 8.0.0
On Wear OS - Version 2.12.0; Android OS - Version 7.1.1 is working correctly
I'm updating this notifications using the
NotificationManagerCompat.notify() and not the NotificationManager.notify() like the documents say and I'm keeping the NotificationCompat.Builder reference instantiated on the createNotification method and using it on the updateNotification method
Using RemoteViews instead of updating the contentText I have the same result, the only thing that works is using the .setUsesChronometer(true)but this uses RemoteViews, so there must be a way to update the notification without making it pop-up
Create notification method:
protected void showNotification(String name, String activityId) {
Intent notificationIntent = activityIntent(activityId);
notificationIntent.setAction(MAIN_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
notificationBuilder = buildBaseNotification(name, NOTIFICATION_CHANNEL_ID)
.extend(extender)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setLocalOnly(true)
.addAction(wearableNotificationPauseAction());
startForeground(NOTIFICATION_ID, notificationBuilder.build());
}
Update notification method:
private void updateNotificationTimer(DataModel data) {
notificationBuilder.setContentText(data.getSomethingRelevant());
notificationManagerCompat.notify(NOTIFICATION_ID, notificationBuilder.build());
}
Issue in GIF format
Related
Unfortunately another question about my startForegroundService notification... I searched, really, I did:
I have a foreground service that is running perfectly. I would like to add a couple of actions to this notification. For one, make it so when the user clicks the notification they are sent to MainActivity as well as adding a "Quit" addAction.
Here is the snippet I am using to create the notification:
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String channelId = getNotificationChannel(notificationManager);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_notif_icon)
.setContentTitle("My app")
.setContentText("Background service is running...")
.setContentIntent(pendingIntent)
.build();
startForeground(13365, notification);
Using the above a notification shows up just fine, but click on it results in nothing. I also tried using addAction, also nothing. I am aware the syntax is a little bit different (....Action.Builder) when adding an addAction.
I am creating my notification in the onCreate handler of the foreground service. Running on SDK 26.
Can startForeground notifications have setContentIntent / addAction attached to them?
Thanks!
Solved : I had the notification replaced elsewhere in my application and was not adding the intents there.
Doh!
This question already has answers here:
Notification Icon with the new Firebase Cloud Messaging system
(10 answers)
Closed 6 years ago.
I'm using Firebase Notification for my app's push notifications.
All working well but notification icon shows white circle when app is not running.
I'm targeting SDK version 23, also I'm using Roman Nurik's notification icon generator to generate white-on-transparent icons.
Notification icon is showing correctly when app is on foreground and running. img
But icon gets replaced with generic white circle when app is in background or killed. img
Here's my notification builder method:
private void sendNotification(String messageTitle, 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);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notification_icon)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification.build());
}
I've already found an answer on similar issue: Notification Icon with the new Firebase Cloud Messaging system
Unfortunately this is a limitation of Firebase Notifications in SDK
9.0.0. When the app is in the background the launcher icon is use from the manifest (with the requisite Android tinting) for messages sent
from the console.
If the app is in the foreground (or a data message is sent) you should
be able to use your own logic to customise, and you should be able to
customise the icon if sending the message from the HTTP/XMPP APIs.
Right now with messages sent from the console you'll get this
behavior.
It seems to be that the best way to avoid this Firebase Notifications bug is to change your targetSdkVersion in build.gradle to 19. The notification icon will be then colored. Firebase after a while will fix this issue.
Hope it will help
try this code:
Intent intent = new Intent(this, TabActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(getNotificationIcon())
.setContentTitle("IDS DMS Support")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
I think that you need to add a silhouette icon to the app and use it if the device is running Android Lollipop.
Use below code to get small icon for notification.
protected int getNotificationIcon() {
boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.drawable.ic_silhouette : R.drawable.ic_launcher;
}
You can see full solution Here.
I did same thing and works fine.
How do smart watches receive text messages from mobile phone? I am looking to make a similar application and I am not sure where to start. I want to make an application for my tablet that will show text message notification.
To create a notification that is transmitted to connected Android Wear devices, you need to use the NotificationCompat class located in the Support v4-Package.
The detailed documentation how to do this can be found here.
The code essentially breaks down to this code snippet (copied from the documentation):
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(this, ViewEventActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
This creates a notification on the handheld as well as connected Android Wear devices, without even creating a Wear-App. The pairing and transmitting process is handled by the OS.
As a general hint, the Training section of Android Developers is always a good place to start researching.
on some devices (seems to be api level independend, in this case Samsung S5 and Samsung Galaxy Tab 3 8" with Stock ROMs) the notifications are only shown when app is in foreground but not when it is in background.
I am sure that the service is running because I can see that in the android settings and it also works on other devices without problems.
public void generateProgressNotification(String title, String message) {
long[] vibrate = new long[]{300};
Intent intent = new Intent(mApplicationContext, MainActivity.class);
intent.putExtra(NotificationHelper.START_SHOW_PROGRESS, true);
PendingIntent contentIntent = PendingIntent.getActivity(mApplicationContext, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mApplicationContext)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setVibrate(vibrate)
.setContentIntent(contentIntent)
.setLights(mApplicationContext.getResources().getColor(R.color.primary), 200, 200)
.setContentText(message);
NotificationManager mNotificationManager = (NotificationManager) mApplicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(PROGRESS_NOTIFICATION_ID, mBuilder.build());
}
Has anyone an idea why the notifications does not work on all devices?
Best regards,
Moritz
In newer versions of android devices. The notification is only pushed if the user using the application. If application is not running then notification will be pushed on next run. I also had the same problem and then I searched for the issue and even used wakeful broadcast receiver but the problem was not solved.
I have made an app that sets notifications in the drop-down status bar of Android phones. However, there is a bug in my code (sometimes the notifications are set, sometimes they are not). I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).
How can I do this? (Thanks in advance).
Sample code is greatly appreciated.
I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see
the notification in the status bar?).
How can I do this?
You can't, sorry. Update: Now possible with Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
However, you can always simply cancel() it -- canceling a Notification that is not on-screen is perfectly fine. Conversely, you can always safely call notify() again for the same Notification, and it too will not cause a problem if the Notification is already on-screen.
EDIT:
NotificationManager.getActiveNotifications() was added in API 23 if you don't want to use the NotificationListenerService
Just to put all together. This is how it works
To build a notification,
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSmallIcon(R.drawable.myicon).build();
To make a notification sound call setSound() of Notification,
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setSmallIcon(R.drawable.myicon).build();
To cancel the notification after user selected and launched the receiver Intent, call setAutoCancel(),
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setAutoCancel(true)
.setSmallIcon(R.drawable.myicon).build();
To make sound/vibrate only once for a particular notification use Notification.FLAG_ONLY_ALERT_ONCE. With this flag, your notification will make sound only once till it gets cancelled and you can call notify() as many times as you want with the notification id. Note that if you call cancel() or if user cancelled the notification or auto cancelled, notify() call will make the notification sound again.
n.flags |= Notification.FLAG_ONLY_ALERT_ONCE; // Dont vibrate or make notification sound
Finally to put the notification on notification panel,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notification_id, n);
Note that notification_id here is important if you want to use the notification effectively.( to keep single sound/vibration for a notification or to cancel a specific notification).
To cancel a particular notification,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
You can cancel() a notification even if it doesn't exist or you can call notify() as many times as you want with the same id. Note that calling notify with different id will create new notifications.
So, regardless of whether the notification exist or not, if you call notify() again with the correct notification_id with the Notification.FLAG_ONLY_ALERT_ONCE flag set, you can keep your notification alive without disturbing the user with repeated sounds.
You need to set an id for each notification you make.
so you make a notification ..
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent);
Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis());
NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent);
manger.notify(notId, notification);
to clear it..
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0);
pendingIntent.cancel();
and to check if active..( existAlarm returns null if no pending intent available)
public PendingIntent existAlarm(int id) {
Intent intent = new Intent(this, alarmreceiver.class);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent test = PendingIntent.getBroadcast(this, id + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE);
return test;
}
So everything comes down to initialize an ID for each notification and how you make it unique.
A new method is introduced to the NotificationManager class in API 23:
public StatusBarNotification[] getActiveNotifications()
There exists a flag for that.
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
FLAG_ONLY_ALERT_ONCE:
...should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.
Although, the notification will blink when it is sent again, but there won't be any sound or vibration.
It's possible now to check notifications outstanding in android 4.3 upwards
See here:
http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
It seems that from Android M (API 23) it is possible to get your process like that, without using NotificationListenerService nor requiring additional permissions:
notificationManager.getActiveNotifications()
As of Android Marshmallow (API 23), you can recover a list of active notifications posted by your app. This NotificationManager method is getActiveNotifications(). More info here: https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()