Android Notification on Wear device. `localOnly` flag - android

I have a few notifications running on my app built with NotificationCompat
Depending on the type, they show background bitmaps, or list of text, also most of them have 1 or 2 actions using the code:
builder.addAction(R.drawable.ic_notification, text, pendingIntent)
some of those actions makes sense for a watch (for example: "like" or "reply") and some doesn't (for example: "view album").
I thought I could use the setLocalOnly(boolean) method for it, but I found out that it is applied to the whole notification, not just to individual actions.
I've also been checking on NotificationCompat.Action and NotificationCompat.Action.WearableExtender but couldn't find anything that would be relevant.
So the question:
is there a way to make the notification show on the watch, but only with some of the actions but not others?

Please see docs in the paragraph "Specify wearable-only actions".

Related

How to create NotificationManager Policy correctly?

I am facing a problem with the NotificationManager Policy.
Let's say I just want to be interrupted by alarms. Then I specify that as the Category (NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS) but what values should I pass to create the Policy? I am talking about the priorityCallSenders, and the priorityMessageSenders. No calls and no messages are allowed, only alarms, so why should I have to specify a value for them?
The available values are:
PRIORITY_SENDERS_ANY
PRIORITY_SENDERS_CONTACTS
PRIORITY_SENDERS_STARRED
Which one should I use? The code results messy and confusing if I specify I want only alarms BUT then use the constant PRIORITY_SENDERS_ANY.
For you to remember, here I paste the call to create the Policy:
myPolicy = new NotificationManager.Policy(NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS, iCallsPrioritySenders, iMessagesPrioritySenders);
myNotificationManager.setNotificationPolicy(myPolicy);
On the other had, and I suspect that the issue might come from here, I am not able to set this values correctly in Xiaomi.
I have tried in Huawei and Android Emulator setting a value like this (even when it is not allowed by Android documentation):
NotificationManager.Policy(NotificationManager.Policy.PRIORITY_CATEGORY_ALARMS, -1, -1);
and it works well with both. But the problem is with Xiaomi. For some reason, calls/messages are always accepted and I am not able to disable calls. It looks like must be Any, Contacts or Starred.
Any light in this issue and the right values to pass?

How to remove pinned shortcuts?

Background
Starting from Android O, it's possible to create pinned shortcuts, which (on supported launchers) show a dialog to confirm the creation of them:
ShortcutInfoCompat pinShortcutInfo = new ShortcutInfoCompat.Builder(context, uniqueShortcutId)
.setShortLabel(label)
.setIntent(shortcutIntent)
.setLongLabel(label)
.setIcon(IconCompat.createWithBitmap(bitmap))
.build();
ShortcutManagerCompat.requestPinShortcut(this, pinShortcutInfo , null);
Docs:
https://developer.android.com/reference/android/content/pm/ShortcutManager.html
https://developer.android.com/guide/topics/ui/shortcuts.html
The problem
Sometimes, the pinned shortcut is not relevant anymore. For example, it points to something that doesn't exist anymore.
In this case, I want to be able to remove it.
What I've tried
I thought this is possible by the next code, but it doesn't, because it is probably about dynamic shortcuts, which is something else :
ShortcutManager shortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);
final List<ShortcutInfo> pinnedShortcuts = shortcutManager.getPinnedShortcuts();
final ArrayList<String> shortcutIdsToRemove = new ArrayList<>();
for (ShortcutInfo pinnedShortcut : pinnedShortcuts) {
final Intent pinnedShortcutIntent = pinnedShortcut.getIntent();
shortcutIdsToRemove.add(pinnedShortcut.getId());
}
shortcutManager.removeDynamicShortcuts(shortcutIdsToRemove);
// this also doesn't work : shortcutManager.disableShortcuts(shortcutIdsToRemove);
The question
How can I remove pinned shortcuts? Is it even possible?
Update: seems it isn't possible, as Google mentioned here.
So far, this API seems very restricting to me, as it has so many disadvantages:
The created icon image cannot be created from a resource ID.
On the launcher, the created icon has a tiny icon of the app that created it.
The icon cannot be removed via the API (only disabled), so if you created one that points to another app, and this app was removed, you won't be able to remove it.
The creation cannot be in the background, as it requires a confirmation dialog. It also cannot be created in a batch. Only one after another.
All pinned shortcuts your app has created will be removed if your app was removed.
So, my question now is:
Is there any way to create and remove a shortcut using the previous API, using an adb command, with root if needed ?
Is it possible to remove specific pinned shortcuts using adb command , with root if needed?
disableShortcuts() is the best you can do. It will show the user a custom error message when they try to select the disabled shortcut.
From the official documentation:
Shortcut limitations
Although you can publish up to five shortcuts (static and dynamic shortcuts combined) at a time for your app, most launchers can only display four.
However, there is no limit to the number of pinned shortcuts to your app that users can create. Even though your app cannot remove pinned shortcuts, it can still disable them.
Another section:
Because your app and its users can pin shortcuts to the device's launcher, it's possible that these pinned shortcuts could direct users to actions within your app that are out of date or no longer exist. To manage this situation, you can disable the shortcuts that you don't want users to select by calling disableShortcuts(), which removes the specified shortcuts from the static and dynamic shortcuts list and disables any pinned copies of these shortcuts. You can also use an overloaded version of this method, which accepts a CharSequence as a custom error message. That error message then appears when users attempt to launch any disabled shortcut.
Note: If you remove some of your app's static shortcuts when you update your app, the system disables these shortcuts automatically.

Android notification dismissed callback?

my app searches for new articles and sends a notification like "5 new articles". However when i send another one, i want to have it update the text to lets say there were 3 new so something like "8 new articles" BUT "3 new articles" if the user has dismissed that previous notification. I hope you get it.
Is there a way to know that notification was dismissed so i can reset the count?
Thanks !
This is a rather belated answer but I was looking to find out how to do this myself so perhaps it'll be useful to others. In API level 18 the following service was introduced which should make this straightfoward as you can now get a callback whenever a notification is added or removed:
https://developer.android.com/reference/android/service/notification/NotificationListenerService.html
In particular for the original question see the onNotificationRemoved method
The other option mentioned by jpop above is to use the builder's setDeleteIntent method when creating the notification, which will give a callback when the notification is deleted. This would then require you to maintain the state of the existing raised notifications somewhere else as it only tells you when something is added, not removed.
If your app has a database you can do this: Create a table that has some fields like id as int, article name as string, and isdismissed as boolean. So, each time you want to send a notification, you should count the records that the isdismissed field equals false.
In the other hand, each time user select a notification, the related isdismissed field must be equal true.
In addition, this sample from developer.android.com maybe can help you:
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
the listener don't work since android Nougat, a new access rule have been added
on android device : parameters -> applications -> gear menu -> spécial access -> notifications access
use : catch on swipe to dismiss event

android embed newline in a notification

I need to embed a newline in a notification. I have two lines of data that are different and I need to embed a newline to distinguish between the two.
I trued a backslash-n, but that does not work.
Is there a way to do this?
You do not need to use RemoteViews or a custom layout to show multiple lines, despite what others have said here!
Multiline texts are possible, but only when the notification is expanded.
To do this, you can use NotificationCompat.BigTextStyle. And if you read the dev guides here then you'll notice that they mention this tip:
Tip: To add formatting in your text (bold, italic, line breaks, and so on), you can add styling with HTML markup.
So, in other words:
val title = "My Title"
val body = "Line 1<br>Line 2<br><i>Italic Line 3</i>"
val formattedBody = SpannableString(
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) Html.fromHtml(body)
else Html.fromHtml(body, Html.FROM_HTML_MODE_LEGACY)
)
NotificationCompat.Builder(context, channelId)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setContentTitle(title)
.setContentText(formattedBody)
.setStyle(NotificationCompat.BigTextStyle().bigText(formattedBody).setBigContentTitle(title))
.setSmallIcon(R.drawable.ic_small_notification_icon)
.build()
Things to note about how this works:
The title can have no formatting (i.e. don't try to use HTML here)
The body can have as much formatting as we want, but we must note that the line breaks won’t display when the notification is collapsed. All other HTML formatting works fine in both collapsed/expanded states. You can bold, italicize, etc. as much as you want (I think)
If you don't want to send HTML formatting in your push notification, then another option is to use markdown formatting of some sort and manually handle this before your notification building by using SpannableStringBuilder
As far as I know there is no way to do this other than creating a custom Notification layout. The Notification documentation has a step-by-step process on doing this and is quite thorough. Here is the link to that.
Creating the two lines as you require would just mean putting in an extra TextView in the layout XML. Then you would just set each TextView's text to your two lines of data, as the example shows.
You need to make custom notification builder in your app. more detail here : https://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView, It takes me so long to find out the way to make firebase to call onMessageReceived in all cases.
app in foreground
app in background
app was killed.
basically your message to firebase via api must not have "notification" key in your json request to firebase server and only use "data" key as show below, firebase service will call your onMessageReceived in all cases listed above.
{
"to": "/topics/your_topic",
"data": {
"key1":"data_key1",
"title":"",
"line1":"data1",
"line2":"data2"
}
}

Android: How to update application shortcut icon with a new items count programmatically

I wanted to show the numbers in a application shortcut icon(without developing the widget), i would like to know how that number can be updated programmatically whenever i receive a service notification saying "n" new items added to the user. I am able to show the notification message whenever i get new items, I want to add this items count to the application shortcut icon(some thing similar to showing the new message count in the messages shortcut).
Thanks
Praneeth
Use Notification.Builder class and set the number of the notifications using setNumber(int number) member function.
I know you don't want to use Widgets but I'm pretty sure the existing message count is based on a Widget.
Here's an example of how to get one up and running - Widget Tutorial

Categories

Resources