Android Wear Notification is not displayed if FLAG_NO_CLEAR is used - android

If flag "FLAG_NO_CLEAR" is used the notification gets not displayed on the Android Wear.
Does anyone know why or any workaround? I didn't find any information in the documentation.
I need the flag "FLAG_NO_CLEAR" on my notifications and have Action button for "dismiss", "snooze" etc.!

Notification flag FLAG_NO_CLEAR basically makes your notification "ongoing". Ongoing notifications posted from phone will NOT be displayed on the wearable device.
You have two solutions to your problem - both of them have advantages and disadvantages. Please read text below and decide which solution will solve your situation better:)
Solution 1 - use group:
You can make use of group feature of Android Wear framework. It's basically created to post many (grouped) notifications on wearable device and one summary notification on phone. But using this mechanism you can also post one ongoing notification on your phone and second notification only on wear. You will end up with one ongoing notification on your phone and one normal notification on your wearable device.
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// This notification will be shown only on phone
final NotificationCompat.Builder phoneNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title phone")
.setContentText("Text phone")
.setOngoing(true)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(true);
// This notification will be shown only on watch
final NotificationCompat.Builder wearableNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title wearable")
.setContentText("Text wearable")
.setOngoing(false)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(false);
notificationManager.notify(0, phoneNotificationBuilder.build());
notificationManager.notify(1, wearableNotificationBuilder.build());
This method will allow you to post an "alternative" notification for watch only, keeping your ongoing notification on phone. But (as mentioned earlier) the notification on watch cannot be ongoing - it has to be a normal notification. If you really want to have a true ongoing notification on watch you'll need to go for second solution.
Please read more about grouping (stacking) notifications here:
https://developer.android.com/training/wearables/notifications/stacks.html
Solution 2 - create wearable app:
Ongoing notifications from phone won't be shown on watch, but you can create wearable part of your Android application and post your notification directly on Android Wear. You can easily post an ongoing notification from there, but it won't be the same notification as is on phone. You will need to sync them between both devices.
Please read more about DataApi here:
https://developer.android.com/training/wearables/data-layer/index.html
https://developer.android.com/training/wearables/data-layer/data-items.html
You can also take a look at my post where I've posted a code demonstrating how to use a DataApi in practise:
https://stackoverflow.com/a/24896043/3827276

Honestly something really weird changed in wear 1.5
so a solution would be to set an alarm
Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent displayIntent = new Intent(this, WearNotif.class);
//displayIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent displayPendingIntent = PendingIntent.getActivity(this,
0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("WeirdShit")
.setContentText("some random crap")
.extend(new Notification.WearableExtender()
.setDisplayIntent(displayPendingIntent))
.setSound(alarmSound)
.build();
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(25454, notification);

Related

Android push disappears after a few seconds

My test phone is a Samsung Galaxy S6 using Android 7.0 Nougat.
In other apps, Android push notifications stay in the status bar, but in my app the push notification disappears after the notification is received.
Problem is DISAPPEAR NOTIFICATION FROM STATUS BAR.
Not about NOTIFICATION ALARM.
Push notification is well done working.
After ringing, there is no push in status bar.
Does anyone know about this situation?
Here is the code:
NotificationCompat.Builder builder = new
NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.icon_noti)
.setContentTitle('My App')
.setContentText('test')
.setDefaults(Notification.DEFAULT_SOUND)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,
0, new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
If someone knows about this issue, please answer.
Thanks for reading.
For me it works perfectly (except for the single quotes here .setContentTitle('My App') and here .setContentText('test'))!
Maybe there are some system issues - or maybe this behavior is caused by some other part of your app.
By the way, note that NotificationCompat.Builder is deprecated; now it requires an additional String channelId.
I had this issue and the problem was that I had two different notifications with the same id.
One was tied to a service so when I unbound the service it killed the notification but since both had the same ID it killed both notifications.
Changing the notification id when calling the notify method fixed the issue:
notificationManager?.Notify(2, notification); //Previously I had "1" which matched another notification that had 1 as ID
Took me like 4 hours to find out but at the end in my scenario it had nothing to do with the notification settings.

Notifications in Android when the phone is sleeping

I have been using Notifications in an app I am building. The way to build notification has changed a lot since older versions it seems, but in the end I managed to display a notification. (*)
I have two problems though:
1) The notification makes no sound. It is not a matter of increasing the volume of notifications in the tablet because notifications from gmail for example do produce sound. How do I make a notification that produce sound and vibration? (I am trying this in Android 6)
2) Even more important, how do I make a notification that "notifies" me even when the phone is sleeping? So far my notifications do happen when the phone is sleeping but I have to turn on the phone to see them. Ideally these should appear even when the screen is black
EDIT: just to be clear, the notifications are happening when the phone is sleeping. It is just that I don't see them unless I turn on the screen, then they are. I would like for the notification to "turn on the screen"
(*) My current code is
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notification= new Notification.Builder(context)
.setContentTitle(message)
.setContentText("Proximity Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pi).build(); //getNotification();
//notification.flags= Notification.FLAG_AUTO_CANCEL;
//notification.setLatestEventInfo(context, "GAST", "Proximity Alert", pi); //this is deprecated
notificationManager.notify(NOTIFICATION_ID, notification);
Yes, you can add sound while sending notification with this way.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICAT‌​ION);
builder.setSound(defaultSoundUri);
And to do it while sleeping phone you will have to use service.
Documentation : Services

How to make notification stay on top?

I have a notification which I always want to stay on top.
I have already given it a priority of 2
Mostly it stays on top but, suppose there is a open WiFi network, then my notification goes down and the notification of open WiFi network comes on the top position.
But when I looked at the notification of vlc(for Android) paying a song, the notification of vlc stay on top and the notification of open WiFi network goes down.
I notification is ongoing and with priority 2.
What should I do. I am a beginner so please bear with me.
And thanks in advance.
have you tried this
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notify_icon)
.setContentTitle("Notification")
.setContentText("ON Top")
.setPriority(Notification.PRIORITY_MAX);
Priority Levels --> PRIORITY_MAX / PRIORITY_HIGH /PRIORITY_DEFAULT /PRIORITY_LOW /PRIORITY_MIN
read
https://material.google.com/patterns/notifications.html#correctly_set_and_manage_notification_priority
https://developer.android.com/reference/android/app/Notification.html#priority
and as StenSoft said
Android: How to create an "Ongoing" notification?
I think you can refer to this code:
Intent push = new Intent();
push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
push.setClass(this, NotificationDemo.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification updateNotification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_face_black_24dp)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
.setContentTitle("Priority Max Notification Title")
.setContentText("Priority Max Notification Message")
.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_SOUND)
.setPriority(Notification.PRIORITY_MAX)
.setFullScreenIntent(pi,true)
.build();
nm.notify("priorityMaxTest",2,updateNotification);

Notification using setFullScreenIntent() for BigTextStyle opening Activity automatically

I have a very strange issue, I am working on Push Notification and it was successfully implemented but when i have used BigTextStyle in Notification to show a long message in notification area with setFullScreenIntent() method then the issue coming up the Notification opening the Activity automatically which is set in PendingIntent.
If I don't use setFullScreenIntent() then notification won't opening Activity automatically the user has to tap or click on Notification to open the Activity set in PendingIntent.
So there are two codes
Without setFullScreenIntent() working fine and not opening Activity automatically:
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
With setFullScreenIntent() also working fine but opening Activity automatically:-
notification = new NotificationCompat.Builder(context)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setFullScreenIntent(resultPendingIntent, true) //Whether true or false same result
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification.build());
public NotificationCompat.Builder setFullScreenIntent (PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status
bar. Only for use with extremely high-priority notifications demanding
the user's immediate attention, such as an incoming phone call or
alarm clock that the user has explicitly set to a particular time. If
this facility is used for something else, please give the user an
option to turn it off and use a normal notification, as this can be
extremely disruptive.
On some platforms, the system UI may choose to display a heads-up
notification, instead of launching this intent, while the user is
using the device.
Parameters
intent: The pending intent to launch.
highPriority: Passing
true will cause this notification to be sent even if other
notifications are suppressed.
Found here. As you can see it immediately launches the intent. I don't really know in what case you wanted to use setFullScreenIntent()?
A notification won't automatically expand when a static notification is displayed on top (could be custom bar with wifi, bluetooth and sound control)
pass setFullScreenIntent and setContentIntent with different pending intents.
Worked for me. click on Notif will work and autolaunch will stop

Different text for the wear device and the mobile device

Is it possible to have a notification that shows a different text (content title and content text) in the Android wear device and in the mobile device?
Not at the moment. However, you can achieve this effect in the following way:
post a notification on the phone with setLocalOnly(true)
post a DataItem using a DataAPI that describes the notification and changed text
when the wearable receives the DataItem, post the notification with different text, again setting setLocalOnly(true)
on each notification alse call setDeleteIntent so you know, when there are dismissed
when on of the notifications gets dismissed, delte the DataItem from point 2.
when the DataItem gets deleted, you will receive a callback; delete the remaining notification
There might be some corner cases here I don't see immediately, but the general approach should allow you to achieve what you want.
Yes, it's possible now with little tricky and bug on Android wear which help's us. More Over this trick doesn't need Android wear API, just a normal Notification with RemoteViews is enough.
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentText("This msg won't display in your phone, only on wear you can see.")
.setContentTitle("Hello")
.setContentIntent(
PendingIntent.getActivity(context, 10,intent,PendingIntent.FLAG_ONE_SHOT));
NotificationManager mNM = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.notification_layout);
contentView.setTextViewText(R.id.noti_text,"This message won't display in your wear device, only on phone you can see.");
contentView.setImageViewResource(R.id.noti_image,R.drawable.ic_launcher);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(50, notification);
Now run the app on your device and check the notification on both watch and phone, the content inside the RemoteViews won't display in your watch but on phone it will display and if you remove the .setContentTitle() & .setContentText(), then the RemoteViews content will be displayed on both watch and phone too.
Actually you can achieve this using
.setGroup(GROUP_KEY)
.setGroupSummary(true)
On your phone notification. Then create a notification with the data that you want to show on the watch and set
.setGroup(GROUP_KEY)
to the same group that your phone notification. This is used to show stack notifications but if you use it with only one then it does the trick.
Complete documentation: Stacking Notifications

Categories

Resources