Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setDefaults is deprecated in Notification.Builder

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

like image 285
Radesh Avatar asked Jun 25 '18 08:06

Radesh


4 Answers

setDefaults should be replaced with use of the following

  • NotificationChannel.enableVibration(boolean)
  • NotificationChannel.enableLights(boolean)
  • NotificationChannel.setSound(Uri, AudioAttributes)

source

setSound should be replaced with use of

  • NotificationChannel.setSound(Uri, AudioAttributes)

source

like image 161
pau1adam Avatar answered Oct 17 '22 10:10

pau1adam


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

}
like image 21
Radesh Avatar answered Oct 17 '22 09:10

Radesh


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.

like image 42
2Dee Avatar answered Oct 17 '22 09:10

2Dee


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:

  1. NotificationChannel.enableVibration(boolean)
  2. NotificationChannel.enableLights(boolean)
  3. NotificationChannel.setSound(Uri, AudioAttributes)
like image 36
Sagar Avatar answered Oct 17 '22 11:10

Sagar