Basically, I am wondering if it is possible to create two different notifications and how - one for Android Device and other for Android Wear?
For example: I want to have just setContentText, but on Android device I want setContentTitle and setContentText
There is currently no possibility to show notification just on Wear (like setLocalOnly with device only - look for more).
I think the Synchronized Notifications sample that comes with the Android Wear SDK may be useful to look at. It gives three simple types of notifications:
(1) A phone-only notification
(2) A watch-only notification
(3) A pair of synchronized phone and watch notifications where the content shown
on the watch notification is different from the one on the phone. They are
synchronized in the sense that dismissing one results in dismissal of the
other one; all based on the Data Layer apis.
I think the third use case is most relevant to you.
Officially it is not possible to create two different notifications for wear and the phone without writing your own Android Wear App extension. It is only possible to define a notification that is only shown on the phone with NotificationCompat.Builder.setLocalOnly(true)
To create a Notification that is only shown on a Wear Device however you can (at the moment) add the Notification to a group with NotificationCompat.Builder.setGroup(randomGroupKey) and omit the display of a group summary notification. If a notification belongs to a group it is not displayed on the phone because the phone will only show the summary notification. If there is no summary you get a notification for your watch only. Just generate a random group key for every watch only notification.
Officially it is only possible to create a notification that looks different on a smartwatch.
For this use a WearableExtender. For example this code snippet:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(getString(R.string.smaple_notification_title));
builder.setSmallIcon(R.drawable.ic_message);
builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, ActivateActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
extender.setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.notif_background));
extender.setContentIcon(R.drawable.ic_message);
extender.setHintHideIcon(true);
extender.extend(builder);
builder.setPriority(NotificationCompat.PRIORITY_LOW);
builder.setContentText(notificationText);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.large_icon));
notificationManager.notify(messageIndex, builder.build());
Sets a special background for the notification, hides the app icon that is normally displayed on the notification, and adds a new icon to the preview of your Notification in the "screen off" mode of the watch.
I don't know if there is a way to do exactly what you want but I try to use stack & summary to bypass this: an contentText only notification has been hidden by a summary notification with contentText and contentTitle. On the Android Wear however summary is not being displayed but all the stacked notification (in your term is the notification with only contentText) can be shown.
Yes, It is possible. Steps -
Intercept your Notifications on Handheld by implementing BroadcastReceiever
Generate Notification for Handheld using NotificationBuilder - use setLocalOnly to duplicating it with Wearable
Send Notification data in Message to Wearable - using MessageApi
Extract receieved data & generate Notification for Wearable
Related
I am using nativescript-plugin-firebase for the APP and I get the notifications when the app is closed.
However, if the app is open I don't get any notifications. Is there any way to force showing push notifications even when the app is open or do I have to create a custom notification with sound within the app?
Another problem that I have is the way the push notifications are displayed. I use Laravel-FCM to send push notifications from the server-side, I have the priority set to 'high' and everything else set to default but when I receive the notification to the android device there is no floating notification, only a badge icon and a notification in the notifications center.
In the app notifications settings, there is Miscellaneous Notification Category with Importance set to Default, Vibration set to OFF and LED Light is also OFF but if I manually change those settings then firebase notifications appear with floating notification and vibration.
Any way to actually make these notifications with high priority by default?
P.S. The device I am testing on is Xiaomi Redmi Note 8 Pro
I have the same problem when using Nativescript-firebase as yours. When the app is open, the push notification is delivered but not shown (see onMessageReceived callback) then you can use Nativescript-local-notification to show the content of the push notification received when app is open.
In my delphi code app, Is possible I change the settings of my pending intent used to handle push notifications when It arrives? I´d like to change your behaviour. Instead I have many push icons notifiactions on status bar when each new push messages arrive, I´d like to have only one with a counter increasing when new push messsages arrive. I´d like something as https://developer.android.com/training/wearables/notifications/stacks.html
Luiz
You can implement the logic you want.
When you receive a push, it carries various data.
If a notification is already displaying such data, you can cancel the existing notification and create a new one.
In any way, the notification manager of android will be able to stack notifications by itself for you.
So i suggest, when you receive data from a push:
Store it to sqlite
Calculate the notification ID you need to display that notification (can be a hashCode of the notification type ?)
Fetch all notif for that type from sqlite
Cancel the notification with this ID (don't worry, cancelling a non existing notification won't make your app crash)
Create a shiny new notification with all the data you fetched, if you have more than one, you might want to display "+X other".
EDIT: With my answer, i assumed you can do as much as thing in "delphi to android" than in native android. Hence, i can't provide code for you, but the idea does not depend of the language you are using.
I have a notification on the handheld with a big text style:
notif.setStyle(new NotificationCompat.BigTextStyle()
.bigText(bigLine).setBigContentTitle(titleLine));
This causes the corresponding Wear notification to have this extended text on the first page.
Is there any way to change what is shown on the first page for the wearable only, while leaving the handheld notification unchanged?
In the end, I'd like to split out the bigText value into pages for the wearable.
If you want to split the text from your notifications into few pages - it seems like you have few "items" in one notification and want show them separately on wearable device. If you really have such scenario you should consider using the grouping functionality
This feature is used for example in Gmail app. You basically have one group notification (but with InboxStyle instead of BigTextStyle) on your mobile and multiple notifications on wearable device within one group.
Group notification
You need to set the same groupKey for all these notifications by using method setGroup (String groupKey)
and then use method setGroupSummary (boolean isGroupSummary) with true value to ndicate which one is a summary notification and should be displayed only on mobile. A non-summary notifications will show up only on wearable device.
Two grouping approaches
Using this approach you can submit one notification (group summary notification) on your phone and multiple notifications on the wearable that will be displayed as a one group. So you will end up with something like
MOBILE: 1 group summary notification
WEAR: X notifications within a group
But if you really want to have one notification with X pages instead of X notifications you can work it around in following configuration:
MOBILE: 1 group summary notification
WEAR: 1 notification "within a group" with X pages
My little app sends some notifications. We get a callback via a Pendingintent when the notification is clicked on. However, when a notification is simply removed without being clicked on, I don't get any kind of notification and thus wouldn't know if a notification has been removed by the user.
My ultimate goal is to limit the number of active notifications sent by my app to no more than 3. But I haven't been able to find a way to enumerate or simply get the count of active notifications sent by my app. The number of methods available in NotificationManager is rather limited.
Any help will be appreciated.
You can set a PendingIntent with setDeleteIntent() which will be called when the notification is removed from the notification tray (such as when the user swipes to dismiss it).
Do note that the notification design guidelines state:
If a notification of a certain type is already pending when your app tries to send a new notification of the same type, combine them into a single summary notification for the app. Do not create a new object.
A summary notification builds a summary description and allows the user to understand how many notifications of a particular kind are pending.
I.e., don't do this:
Do this (this example uses an InboxStyle notification as is recommended):
Make sure you are not posting multiple notifications of the same type.
the method "Notification.deleteIntent" you can use to set a PendingIntent which the notification was removed by system will be called .And then you can do something you want .
From reading about wearable-notifications documentation, it doesn't seem possible to programmatically stop my app's notifications from appearing on the connected wearable device. I can add an my app to 'muted' apps' list using the Android Wear app on the handheld; however, I would like to do this using code. Please let me know if you've figured this out.
Additionally, is it possible to show a completely different notification on the phone and on the wearable, instead of just having a different set of notification actions on the wearable?
Thank you for your responses!
Using setLocalOnly(true), it is possible to display the notification only on phone. This, in effect, programmatically mutes your app - your app's notifications do not appear on the connected devices.
To create completely different notifications for phone and wearable, we can write a companion wearable app that displays the custom notification. The phone notification is then stopped from appearing on wearable using setLocalOnly(). I haven't tried the 'stacking' mentioned by Maciej Ciemięga yet.
(Added this as an answer for the benefit of those who might miss the comments on the accepted answer.)
First question:
I'm afraid muting apps from code is not possible.
Second question:
It is possible to show different notifications on phone and watch.
You can do it by implementing a wearable application and show local (setLocalOnly()) notifications separately on watch and phone (+ sync them with the phone using DataApi).
Alternatively 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 (summary) notification on your phone and second notification only on wear.
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")
.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")
.setGroup("GROUP")
.setGroupSummary(false);
notificationManager.notify(0, phoneNotificationBuilder.build());
notificationManager.notify(1, wearableNotificationBuilder.build());
This way you can create "stack" with one notification only (+ summary notification of course). The stack with one notification will appear only on watch and the summary notification will appear only on phone - so this is what you want to achieve:)
Please read more about grouping (stacking) notifications here:
https://developer.android.com/training/wearables/notifications/stacks.html