This question already has answers here:
NotificationCompat.Builder deprecated in Android O
(10 answers)
Closed 5 years ago.
Notification.Builder(context) has been deprecated recently with the venue of Notification Channels in Android O.
PROBLEM:
After using Notification.Builder(context, StringID) instead of Notification.Builder(context)I did receive a notification to my Android O device.
However, after trying that on an Android 23 (M), I did not receive a notification. I debugged my code and it just stopped executing once the debugger hit the line post Notification.Builder(context, StringID) on Android 23 (M).
FIX:
To Fix this issue, I used if/else condition to segregate between Android O devices and the rest of other devices.
I have the following code snippet:
Notification.Builder notificationBuilder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = new Notification.Builder(mContext,
mContext.getResources().getString(R.string.notification_id_channel));
} else {
notificationBuilder = new Notification.Builder(mContext);
}
Lint in Android Studio is displaying the following deprecation line:
QUESTION:
Is there a way to get rid off that deprecation warning line?
Your solution is to use NotificationCompat.Builder(Context context, String channelId). If you use this you don't have to check the API level, the Builder ignores the channel ID on pre-Oreo devices.
I have tested it on API 15, 22, 23, and 26 and it works perfectly.
You have to define a unique channelId (for example "MyChannelId_01") and call NotificationCompat.Builder (ctx, "MyChannelId_01"). The constructed Notification will be posted on this NotificationChannel "MyChannelId_01".
This alow you to define importance of the notification (this controls how interruptive notifications posted to this channel are. Value is IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE, IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT or IMPORTANCE_HIGH).
You can find an example here : Creating a notification channel
I had the same issue and since I am targeting android 22 and 24 I just did this:
NotificationCompat.Builder notification = new NotificationCompat.Builder(MainActivity.this, "")
I am sure someone will say this is a hack but it gets rid of the warning and I have no issues.
Seems passing an empty string works for < android 26.
Maybe someone else can state if this causes issues for 26.
Thanks
Related
My Xamarin.Android app's push notification only works on Android 11 (Pixel 3 XL). Currently my app targets Android 11, however it also runs on Android 12 (Pixel 6 Pro). The only thing that is not working is Firebase push notifications. Below is the code that I am using. For the past week I have been researching the issue and saw posts about a specific issue with Android 12 (Pixel 6) not recieving push notifications. I performed changes to the phone configurations that others suggested and another app notification began to work, yet mine still has not. Any ideas would help.Thanks.
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
var name = "NameOfChannel";
var description = "Notification Channel";
var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.Max)
{
Description = description
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
What I was missed is
PendingIntent.FLAG_IMMUTABLE
If Notification has pendingIntent then set mutable/immutable Flag to it
Found the solution. Which was to add android:exported="false" on the Firebase service tag within the AndroidManifest.
PendingIntent pendingIntent;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.S)
{
pendingIntent=PendingIntent.getActivity(context,notificationId,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_MUTABLE);
}
else
{
pendingIntent=PendingIntent.getActivity(context,notificationId,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
}
builder.setContentIntent(pendingIntent)
Im also facing the similar issue (Android/Kotlin), though i have modified Pending Intent changes and set "exported=false" still , app is not receiving the push notification.
below is firebase version
implementation platform('com.google.firebase:firebase-bom:31.0.3')
implementation 'androidx.work:work-runtime-ktx'
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-iid'
The notification it self is working good, but not as I want. It vibrates and shows the icon defined but not as a Watsapp Notification and in the setCategory I put CATEGORY_MESSAGE but still, nothing!
on my App class i put :
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(CHANNEL_DESC);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = getSystemService(NotificationManager.class);
assert manager != null;
manager.createNotificationChannel(channel);
}
Fragment :
private void T(String message){
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID)
.setContentText(message)
.setContentTitle("Test")
.setColor(0xff123456)
.setSmallIcon(R.drawable.com_facebook_button_icon)
.setCategory(CATEGORY_MESSAGE)
.setPriority(PRIORITY_HIGH);
NotificationManagerCompat compat = NotificationManagerCompat.from(getApplicationContext());
compat.notify(1,mBuilder.build());
}
This is How I want it to notify :
This is how is current notifying
I'm using the SDK 27
After discussing with OP in chat, here's my best explanation about what could have happened:
A notification channel can only be created once, after which it becomes immutable to the app. It can only be tweaked by the user through the Settings. If someone follows the examples in the official docs first, they might create the channel with IMPORTANCE_DEFAULT. After this, even if they change the code later, the channel will remain at level 'High: Make Sound' and not be set to 'Urgent: Make Sound and pop on screen' as desired. Docs on importance level
The code in the question is perfectly fine, and should create a channel with the 'Urgent' level when installed for the first time. In any case, uninstalling the app manually and then installing it again will recreate the channels, setting the level to whatever is mentioned in the latest code.
We have code similar to the following in our app
val pendingIntent = PendingIntent.getActivity(ctx, id.toInt(), intent, PendingIntent.FLAG_CANCEL_CURRENT)
val builder = NotificationCompat.Builder(ctx, Channel.TEST_CHANNEL.channelId)
builder.setTicker(tickerText)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setVibrate(vibrate)
.setSmallIcon(icon)
.setAutoCancel(true)
.setLights(-0xff0100, 300, 1000)
.setSound(uri)
.setContentIntent(pendingIntent)
.setStyle(NotificationCompat.BigTextStyle().bigText(contentText))
.addAction(R.drawable.ic_notification, ctx.getString(R.string.notification), piAction)
val notification = builder.build()
val nf = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
nf.notify(NOTIFICATION_TAG, id.toInt(), notification)
}
Starting recently we noticed that notifications on some device running Android 8+ started disappearing briefly after being shown, without user's interaction. Setting auto-cancel to false helps, but the user experience degrades.
The id is a unique item id from the database. This may be important thing to note - technically we can have a notification with such id be shown, removed/canceleld by user, and later some time used again for a similar notification with the same id. Can this be the reason?
We've updated the support libs and tried the following method on builder for luck:
builder.setTicker(tickerText)
...
.setTimeoutAfter(-1)
...
Setting this param to a positive value delayed the notification disappearing by that amount of time (so it did affect). Thus we tried a negative number, the notifications seem to stay there now.
I couldn't find any reasonable documentation explaining this, so this answer is not 100%, but keeping it here for now for others to try and see if it helps them.
Disable your application from auto optimize from battery optimization setting in android OREO. Notification will stay as long as you want
Only thing I found uncertain is NotificationCompat.Builder
Android oreo now uses Notification.Builder instead of NotificationCompat.Builder.
Might be you have to check android version like:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Use Notification.Builder
} else {
// Use NotificationCompat.Builder.
}
I don't think unique id will be an issue for disappearing notification.
Google has created open source sample for this new changes. Please refer to it for more info.
https://github.com/googlesamples/android-NotificationChannels
.setAutoCancel(false)
May be it will work for you.
I'm trying to show a notification to the user in my Android application. I'm running two emulators at the moment. One is running at API level 16, and the other one is running at API level 25. When I send a notification to the application, the notification will only show up on the emulator which is running API level 25. I want to be able to show the notification on the emulator which is running API level 16 as well.
This is my code to show the notification to the user:
private void basicNotification(String title, String body, String data, Context context) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_monetization_on)
.setContentTitle(title)
.setContentText(body);
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(0, mBuilder.build());
}
Explanation parameters:
title = The title of the notification.
body = The body of the notification.
data = The data of the notification (not being used yet).
context = This is the context which I'm using to show the notification. This context is received from a class which extends FirebaseMessagingService.
When I execute the code above, I won't receive any errors. The only thing which will come up is in the logcat. The following line will appear:
05-11 07:47:42.169 1606-1758/system_process D/ConnectivityService: handleInetConditionHoldEnd: net=0, condition=100, published condition=100
I am not sure what I'm doing wrong right now. I used the official documentation from Android to make this method. If you need more information to solve the problem just let me know :)
So, after trying a few things, I found out I made a terrible mistake. The notification was showing up, but only in the drop down menu. For anyone who struggles with this, try to check your drop down menu if your notification is in there.
Related answer: https://stackoverflow.com/a/43809420/4653908
This question already has answers here:
Android : Notification not working on 2.3.6 (Samsung galaxy y)
(3 answers)
Closed 9 years ago.
My program is calling this code from a service to show notification:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(icon)
.setContentTitle("BU").setOngoing(true).setContentText("BU is the biggest!!!");
mNM.notify(mNotificationId, mBuilder.build());
It is working on Galaxy S-III which has a version 4.1+, but it is not working neither giving an error on android v2.3.6.
I have read the api, but I might miss something. What is the problem and how can I solve it?
Thanks in advance...
Try this code as this is working for me (2.2 and up). I run this method in a seperate service class that I created. Found that my notifications does not work without a background service.
final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.notification_popup, message, System.currentTimeMillis());
// used to call up this specific intent when you click on the notification
final PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MyActivity.class), Notification.FLAG_AUTO_CANCEL);
notification.setLatestEventInfo(context, title, message, contentIntent);
notification.defaults = Notification.DEFAULT_ALL;
manager.notify(new Random(100).nextInt(), notification);
Try Notification Compat 2. It has been developed to handle this kind of problem.
From Official documentation Android
This class requires API level 11 or higher.
This document is hidden because your selected API level for the
documentation is 10. You can change the documentation API level with
the selector above the left navigation.
For more information about specifying the API level your app requires,
read Supporting Different Platform Versions.
to confrim this select api level 10 from left side on the page you will get this dialog poped up