Enable all notification settings by default in my android app - android

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

Related

No notification sound on the emulator

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.

FCM topic notifications being received on emulator but no other device

I'm having an issue where Firebase Cloud Messaging will send notifications to the emulator (Android 7.0, with play services installed) but not to any device (Google Pixel 2, Samsung s9+ , Huawei MATE 10) running Android 8+. I initially thought it might have been a power saving issue closing off the listener but even when I remove optimisation from the app, it still doesn't come through.
I know the downstream request is being sent successfully and I'm receiving status code 200 from the response (and of course because the notification is being received on the emulator). When debugging on the devices however, the onMessageReceived in my FCMService class is not being called at all, whether in the foreground or background.
It's definitely registered in the manifest and each device is using the same version of the application:
<service android:name=".services.FCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
If there is any more information required, let me know. Thanks in advance.
Why was this happening?
I realised when I entered the version numbers into the question that it would have to do with Android Oreo. I was searching for issues with FCM and not recent changes the how notifications work. In Android 26+, notification channels have been introduced and are required if your app is targeting 26 and above (which I was). Because I updated the target SDK without thinking, I of course got hit with no notifications.
How to fix it
The easiest way is to just target API 25 but that's not ideal.
What you can do is register notification channels. I've done this where I handle my notifications (so onMessageReceived) as it doesn't register the same channel more than once.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Set the default notification channel settings to be that of alert
int importance = NotificationManager.IMPORTANCE_HIGH;
String channel_id = ALERT_CHANNEL_ID;
CharSequence channel_name = ALERT_CHANNEL_NAME;
String channel_desc = ALERT_CHANNEL_DESC;
// Logic to change the channel id, name and desc if required
// Build and modify channel settings
NotificationChannel channel = new NotificationChannel(channel_id, channel_name, importance);
channel.setDescription(channel_desc);
channel.enableLights(true);
channel.enableVibration(true);
// Create the notification channel
notificationManager.createNotificationChannel(channel);
}
Then you want to change the notification builder:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel_id)

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

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)

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

Vibrate an Android Wear device on notification

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.

Categories

Resources