Different text for the wear device and the mobile device - android

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

Related

clicking on group of notification is not redirecting to specific screen, instead of that app is restarted in oreo

I have implemented firebasse notification functionality in my chat app and everything is running fine but now while testing in Android os 8.0 and 8.1 when app is in background and if user is getting 4 or more than 4 notification then its combined in group and when user click on group then not getting intent and app is restarted.
If user tap on single notification then I am able to send him in specific screen.
I want Notification data or chat id so I can send him in specific screen but not getting any data in intent.
I have searched similar kind of question in stackoverflow but still not getting proper result.
Android: Clicking Grouped Notifications Restarts App
How to open non-launcher activity on notification group click
You can use setContentIntent to set the value of Intent like below
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.your_notification_icon)
.setContentTitle("Notification Title")
.setContentText("Notification ")
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
It will open SecondActivity (Replace the SecondActivity with your own activity)
I am having the same issue causing app restart in Oreo, after many tries i noticed that if when creating the notification i call .setGroup(string) the system no longer groups the notifications.I am creating the notifications from a service. This is not a solution i know but not grouping is a lesser evil than app restart.

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.

Sending immediate notification with options

So I have a timer using AlarmManager and once it hits 0 I want it to send a notification with various attributes depending on what the user chooses (vibrate or not, play sound or not, show text or not, show... rectangle thingy in the drop-down tray whatever it's called, etc).
I am not sure what the expected class to use here is since it's immediate, i.e. it's after the alarm manager has already hit 0 and I am now in the onReceive() method of my broadcast receiver, so I don't think I need to use a PendingIntent at this point.
Do I use NotificationCompat.Builder? Will it do everything I need? And if so do I use the support v4 or v7 version?
Trying this:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(c)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(c.getResources(), R.mipmap.ic_launcher))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText("This text should be in notification drawer!");
NotificationManager notificationManager = (NotificationManager) c.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
Toast.makeText(c, "Toast text!", Toast.LENGTH_LONG).show();
As per discussion in the comments -
Yes NotificationCompat.Builder will do all of the above things you need.
.build is enough to send notification instantly. And if you want to set a specific time you can use .setWhen as well. You can find all methods related to action that you need to do here - Notification Developer Guide

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

Android Wear Notification is not displayed if FLAG_NO_CLEAR is used

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);

Categories

Resources