Notification.Builder中的setDefaults
在android O及以上版本中被弃用(SDK> = 26)
还有setSound
这是我的代码
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());`
我应该更换什么?我找不到任何有用的例子
来自docs:
public Notification.Builder setDefaults(int defaults)
此方法在API级别26中已弃用。请改用NotificationChannel.enableVibration(boolean)和NotificationChannel.enableLights(boolean)和NotificationChannel.setSound(Uri,AudioAttributes)。
(臭名昭着).VilanderSounds(太阳山,EssentialDissues)
此方法在API级别26中已弃用。请改用NotificationChannel.setSound(Uri,AudioAttributes)。
您需要更改方法的签名。
从Android O(API级别26)开始,必须将所有通知分配给通道。可以使用以下专用方法在通知通道上设置这些配置:
我现在使用这个功能,工作完美
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());
}