How to Disable my App from Appearing on Android Wear Devices? - android

How to Disable my App from Appearing on Android Wear Devices?
It seems that my app integrates automatically with Android Wear (notifications mirroring) but I want to disable it...

To keep your app's [phone] notification from being mirrored to Android Wear, call setLocalOnly(true) on the Notification.Builder (or NotificationCompat.Builder, if you're targeting SDK < 20) that you use to create the notification.
For example:
NotificationCompat.Builder bob = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setLocalOnly(true);
(the last line is the important one)
Documented here: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLocalOnly(boolean)

Related

startForegroundService - Specify a custom 'background app running..' notification?

I have a foreground service that I launch via startForegroundService.
All works great.
The only thing I am unable to figure out is how to / if its possible to customize the "...is running in the background' notification.
The notification I am sending over to startForeground looks like this:
Notification notification = notificationBuilder.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_notif_icon)
.setContentTitle("My title")
.setContentText("My content")
.build();
startForeground(1, notification);
I am then presented by android with the standard "...running in background" notification, with my custom one no where to be seen.
Am I doing something wrong?
Not looking to hide it or anything nefarious, but would like to use it more as a status of what the background service is actually doing... Which sounds like an ideal use case for this sort of thing.
For what its worth, I am running Android 8 and targeting SDK 26 as that is the latest available for my handset.
Thanks!
As per the Create and Manage Notification Channels guide:
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel.
Your notification is not appearing because you do not set a notification channel as per the note on that same page:
Caution: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error.
Note: You can turn on a new setting in Android 8.0 (API level 26) to display an on-screen warning that appears as a toast when an app targeting Android 8.0 (API level 26) attempts to post without a notification channel. To turn on the setting for a development device running Android 8.0 (API level 26), navigate to Settings > Developer options and enable Show notification channel warnings.

Enable all notification settings by default in my android app

Is there any way I can enable all notification settings by default when my app gets installed ?
Users are receiving notifications but sound is disabled by default and we need to manually enable it on the device. Not all users can do this manually. It would be great to know if there is any way we can check all these things when our app gets installed like WhatsApp or Telegram (they have everything checked by default)
Try using with this below permission in AndroidManifest file
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
and set notification priority for both below and above Oreo versions IMPORTANCE_HIGH for Oreo and above, and PRIORITY_HIGH or PRIORITY_MAX for below Oreo versions
Reference link for priority note
Priority for version below Oreo
mBuilder.setSmallIcon(R.drawable.app_logo)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setStyle(bigPictureStyle)
.setSound(soundUri)
.setPriority(NotificationCompat.PRIORITY_HIGH) // prirority here for version below Oreo
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.app_logo))
.setContentText(message)
.build();
Priority for Oreo and above
Refer this link
WhatsApp, Telegram, Snapchat, and so on. These are all white-listed apps which means the package names are hardcoded at OS level to allow some of the permissions enabled by default. Our apps are not so. Hence, we need the user to enable those permissions manually.
You can check this by yourself. Create a new android application give the package name of the Telegram application(org.telegram.messenger) and just run it. Don't do any code at all, and no need to open the app too. Simply go to the notification settings of the newly created application, where you find all the permissions enabled by default.
Hope you got the answer.
Android 8 or higher you need to use NotificationChannel to enable sound, vibration, sound etc.
Uri notification_sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
notificationChannel.setSound(notification_sound, attributes);//for enable sound
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
}
but in Redmi note 5 pro (MIUI 10.2.1.0) still notification sound is disabled. I think there is a bug in MIUI. Run this same code in mi A1(Android one mobile) everything fine. It works.
refer this link to know more about Notification Channel

Heads-up Notification - Android Lollipop

I'm trying to show a notification-type heads-up but I could not. What I tried
final Notification.Builder notif = new Builder(getApplicationContext())
.setContentTitle(getString(R.string.title))
.setContentText(getString(R.string.text))
// .setTicker(getString(R.string.tick)) removed, seems to not show at all
// .setWhen(System.currentTimeMillis()) removed, match default
// .setContentIntent(contentIntent) removed, I don't neet it
.setColor(Color.parseColor(getString(R.color.yellow))) //ok
.setSmallIcon(R.drawable.ic_small) //ok
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
// .setCategory(Notification.CATEGORY_CALL) does not seem to make a difference
.setPriority(Notification.PRIORITY_MAX); //does not seem to make a difference
// .setVisibility(Notification.VISIBILITY_PRIVATE); //does not seem to make a difference
mNotificationManager.notify(Constants.NOTIFICATION_ID, notif.build());
The notification is shown only as an icon in the bar.
I'm using API 21 on API 21 emulator (not L preview)
I have tried:
android:Theme.Holo.NoActionBar,
android:Theme.Holo.NoActionBar.Fullscreen
and NotificationCompat.Builder
SDK examples are not available. does anyone know how to do it?
I made it working by adding:
.setDefaults(Notification.DEFAULT_VIBRATE)
is this the best way?
According to Notifications, you are required to set a vibrate or ringtone to make Heads-up work. However, here's a quick hack that doesn't require VIBRATE permission to produce a head-up notification:
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);
EDIT:
Don't abuse heads-up notification. See here for when to use heads-up notification:
MAX: For critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.
HIGH: Primarily for important communication, such as messages or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.
According to Google:
https://developer.android.com/design/patterns/notifications.html
If a notification's priority is flagged as High, Max, or full-screen, it gets a heads-up notification.
So the following code should generate an heads-up notification:
.setPriority(Notification.PRIORITY_MAX)
Should be enough. But apparently the .setDefaults(Notification.DEFAULT_VIBRATE) has to be set also. Hopefully Google will fix this in their final release of Android 5.0.
Not sure if bug or feature...
All my apps doesn´t show the Notification, for example i have a Nexus 6 with Android 5.1.1, but i think this is an issuse since Android 5.0, i had to set:
.setPriority(Notification.PRIORITY_HIGH)
Correctly set and manage notification priority
Android supports a priority flag for notifications. This flag allows you to influence where your notification appears, relative to other notifications, and helps ensure that users always see their most important notifications first. You can choose from the following priority levels when posting a notification:
MAX Use for critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before
they can continue with a particular task.
HIGH Use primarily for important communication, such as message or chat events with content that is particularly interesting for the
user. High-priority notifications trigger the heads-up notification
display.
DEFAULT Use for all notifications that don't fall into any of the other priorities described here and if the application does not
prioritize its own notifications
LOW Use for notifications that you want the user to be informed about, but that are less urgent. Low-priority notifications tend to
show up at the bottom of the list, which makes them a good choice for
things like public or undirected social updates: The user has asked to
be notified about them, but these notifications should never take
precedence over urgent or direct communication.
MIN Use for contextual or background information such as weather information or contextual location information. Minimum-priority
notifications do not appear in the status bar. The user discovers them
on expanding the notification shade.
To set the priority, use the setPriority function (introduced in API 16) alongwith setDefaults (added in API 11) of Notification Builder. Choose the priority DEFAULT, HIGH, LOW, MAX, MIN as per the requirement of your app. Defaults can also be chosen here.
A small snippet:
notification = NotificationBuilder(service)
notification.setPriority(Notification.PRIORITY_MAX)
notification.setDefaults(Notification.DEFAULT_ALL)
Please check that your phone is not in “silent” or “do not disturb” mode. I spent day before I found it. I just leave this comment for those who get the same problem and found this question.
Should set high priority and use ringtones or vibrations.
notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
Ref: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up
Heads-up Notifications
With Android 5.0 (API level 21), notifications can appear in a small
floating window (also called a heads-up notification) when the device
is active (that is, the device is unlocked and its screen is on).
These notifications appear similar to the compact form of your
notification, except that the heads-up notification also shows action
buttons. Users can act on, or dismiss, a heads-up notification without
leaving the current app.
Examples of conditions that may trigger heads-up notifications
include:
The user's activity is in fullscreen mode (the app uses fullScreenIntent), or
The notification has high priority and uses ringtones or vibrations
For devices running Android 8.0 (API level 26) and higher the notification channel requires high importance
new NotificationChannel("ID", "Channel Name", NotificationManager.IMPORTANCE_HIGH);
Add this line in your code to display heads up notification it's only working for Lollipop version
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
You don't need to set vibrate. You only need to set sound. It's less intrusive. I don't get any sound on mine, but the notification displays on top. Make sure you use PRIORITY_HIGH and DEFAULT_SOUND.
NotificationChannel channel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = new NotificationChannel("my_channel_01",
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
}
Notification notification =
new NotificationCompat.Builder(this, "notify_001")
.setSmallIcon(R.drawable.ic_check)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setChannelId("my_channel_01").build();
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notification);

Does NotificationCompat and NotificationManagerCompat have to be used for notifications to appear on a wearable?

I'm a bit confused by the documentation for wearable notifications.
If I have an existing app that is using Notification.Builder and NotificationManager.notify() will the notifications automatically also display on a paired wearable or does the app need to change to use NotificationCompat.Builder and NotificationManagerCompat.notify()?
If its not necessary to use the XXXCompat classes for notifications to appear on the wearable then is there any benefit/use in using them instead of the non Compat variants?
NotificationManagerCompat is required for all Wear specific functionality (anything added via a WearableExtender, a part of NotificationCompat.Builder) per delivering notifications:
If you use the framework's NotificationManager, some features from NotificationCompat.WearableExtender do not work, so make sure to use NotificationCompat.
However, basic notifications sent via NotificationManager will indeed be mirrored over the Android Wear devices without issues.
As CommonsWare alluded to in his comment, it is much, much easier to write Notifications that look good on all API versions by using NotificationCompat. This will become even more important when Android L comes out (where there are numerous Android L notification changes) which, unless you are targeting Android L+ devices only, you'll need to use NotificationCompat to access.

Android Wear: Programmatically stop my app's notifications from appearing on watch

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

Categories

Resources