setDefaults
in Notification.Builder is deprecated in android O and above (SDK >= 26)
Also setSound
This is my code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
b.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_luncher_new)
.setContentTitle(Title)
.setTicker(Title)
.setContentText(Msg)
.setChannelId("cid")
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new Notification.BigTextStyle().bigText(Msg))
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(contentIntent);
}
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(id, b.build());`
What should I replace? i don't find any useful example
setDefaults
should be replaced with use of the following
source
setSound
should be replaced with use of
source
I use this function now and work's perfectly
private void CreateNotificationMessage(Context ctx,String Title,String Msg){
int id=15;
Intent intent= new Intent(ctx, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
Notification.Builder b = new Notification.Builder(ctx);
NotificationChannel mChannel = null;
b.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_luncher)
.setContentTitle(Title)
.setTicker(Title)
.setContentText(Msg)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(contentIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel("cid", "name",NotificationManager.IMPORTANCE_HIGH);
b.setChannelId("cid");
mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build());
}
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(id, b.build());
}
From the docs:
public Notification.Builder setDefaults (int defaults)
This method was deprecated in API level 26. use NotificationChannel.enableVibration(boolean) and NotificationChannel.enableLights(boolean) and NotificationChannel.setSound(Uri, AudioAttributes) instead.
public Notification.Builder setSound (Uri sound, AudioAttributes audioAttributes)
This method was deprecated in API level 26. use NotificationChannel.setSound(Uri, AudioAttributes) instead.
You need to change your method's signature.
Beginning from Android O (API level 26), all notifications must be assigned to a channel. These configurations can be set on notification channel using following dedicated methods:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With