I'm trying to develop an app for android and Android Wear (Wear OS). I create a Notification from the mobile to the watch.
notification = new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000, 1000})
.setContentTitle(title)
.setContentText(content)
.setStyle(bigStyle.bigText(content))
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), background))
.build();
In the manifest of the watch I set:
<uses-permission android:name="android.permission.VIBRATE"/>
Unfortunately, there is no vibration. How can I fix it?
Thanks.
.setDefaults(Notification.DEFAULT_ALL)
This line of code will make the wear wake up and vibrate.
I would like to add 1 more updated answer for SDK 26 and above. I spent many hours trying to determine why my notifications would not vibrate on the Android Wear watch.
I eventually noticed that someone had set the priority on the notification channel to IMPORTANCE_MIN.
if (Build.VERSION.SDK_INT >= 26) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(Config.NOTIFICATION_CHANNEL_ID, Config.NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_MIN);
notificationManager.createNotificationChannel(notificationChannel);
}
To fix the issue, I did the following:
Uninstalled the app from the phone
Changed the priority to HIGH [Though I suspect NORMAL will work also]
Reinstalled the app
Updated Code:
NotificationChannel(Config.NOTIFICATION_CHANNEL_ID, Config.NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
Note: You also need to ensure that your defaults are correct as 'Android enthusiast' pointed out in his earlier answer.
I hope it saves someone from an investigation.
Related
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
I am going nuts with a problem with notifications on Android: While I was developing my project, suddenly the emulator plays no notification sounds anymore for API 26 and higher,
e.g. the API which require a channel.
Of course I have set up a channel and it has worked great before! I have reinstalled the app, deleted the channel, even set up another AVD with a API 27, same result: no sound ! (the notification does pop up)
Obviously I have checked that notification sounds are enabled, also for this specific channel, all seems OK, just no sounds.
If I play a test using:
RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)).play();
it works as it should, so no hardware problem.
On lower APIs pre 26 where you don't need a channel, the sound does play.
Anybody had the same problem?
//make the channel
//The Config class is imported and the constants resolved, not the problem
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(
Config.CHANNEL_1_ID,
Config.CHANNEL_1_NAME,
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(Config.CHANNEL_1_DESC);
channel.enableLights(true);
channel.enableVibration(true);
channel.setShowBadge(true);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
// send notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context, Config.CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(false)
.setColor(context.getResources().getColor(R.color.colorPrimary))
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat mNotificationMgr = NotificationManagerCompat.from(context);
mNotificationMgr.notify(1, mBuilder.build());
Seems I found the answer: I had to go through the "Finish setting up your Android SDK" wizard on the emulator. Clicked "skip" for everything, now it seems to work again.
Weirdly enough, I didn't do that initially and still the notifications worked as expected... duh !
On Pie 9.0 (api 28), you need to complete setup process. After doing it, the sound of notification channels will run well. It is same for both Emulator and physical devices.
If you have not done it, all notification on device will be run on silent, regardless of importance level.
You may need to check Tools->SDK Manager->Appearance & Bahaviour->Notifications "Play sound" checkbox for the "Android emulator" and "Android" to get notifications sounds (i.e. when other sounds work, but there's not notification sound).
For me the problem was the ring volume on the emulator. Apparently it was on zero by default and there was no indication for it on the top bar. Also when pressing the volume keys it would open the media volume and not the ring volume! I had to increase the ring volume via android settings -> sound.
What backwards compatibility does
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
provide that I don't get from the normal
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
They both have a notify method that worked on my tested devices (down to API level 19).
The Google sample here doesn't even use the Compat version once:
https://github.com/googlesamples/android-NotificationChannels/blob/master/Application/src/main/java/com/example/android/notificationchannels/NotificationHelper.java
From looking at the source code for NotificationManagerCompat, the bulk of the benefits are with regards to canceling notifications and determining if notifications are enabled on API 19 and below. Specifically with regard to notify(), NotificationManagerCompat can leverage side channel notifications on older Android API levels. This allows you to register a NotificationCompatSideChannelService that you can use to provide more robust grouping functionality on these older Android APIs.
Maximum of 8 bundled notifications?
I'm writing an app that shows notifications in the status bar (and in the notifications window) at specific times, according to alarms the user sets up.
Whenever there's 3 or more notifications issued, the OS bundles them together. So far, so good.
But it seems there's a maximum limit of 8 notifications for my app in the notifications window at all times. If I issue a ninth notification, the 'oldest' notification disapears from the group and the ninth appears.
I've tested the app on 2 Motorola phones with (what seems to be) vanilla Android 6.0 and 7.0. The issue happens with the 7.0 one (when Bundled Notifications were introduced). This is the code that generates the notifications:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntentPopup)
.setSmallIcon(R.drawable.q_statusbar_icon)
.setCustomContentView(contentView) //my notifications have a custom layout
.setSound(notificationSoundUri)
.setVibrate(vibrate_pattern);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notification);
Unfortunately, there will be times when more than 8 notifications should be 'active' in the notifcations window (bundled together). Is there a workaround to this? Am I missing something?
Is it possible for an iOS or android app to launch a system notification. And have the sound played by the notification change depending on some criteria?
If so can both platforms do so?
Both platforms can do so.
In iOS, as mentioned in the comment, you can send a push notification payload with a sound filename.
For example, the following payload would display an alert and play sound-file-name if this file is bundled with your application.
{"aps":{"alert":"my message","sound":"sound-file-name"}}
In Android, when you build a notification, you can set a sound to the notification using setSound(uri).
public NotificationCompat.Builder setSound (Uri sound)
Set the sound to play. It will play on the default stream.
Or you can use setDefaults(Notification.DEFAULT_SOUND) for playing the default sound.
For example :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_SOUND)
.setTicker (text)
.setSmallIcon(R.drawable.icon)
.setContentText (text)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(text))
.setAutoCancel(true).setNumber (4)
.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());