setDefaults
也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());`
我应该替换什么?我找不到任何有用的例子
答案 0 :(得分:4)
setDefaults
应该替换为以下
setSound
应该替换为
答案 1 :(得分:1)
来自docs:
public Notification.Builder setDefaults(默认值)
在API级别26中不推荐使用此方法。 NotificationChannel.enableVibration(boolean)和 NotificationChannel.enableLights(boolean)和 而不是NotificationChannel.setSound(Uri,AudioAttributes)。
public Notification.Builder setSound(Uri声音,AudioAttributes audioAttributes)
在API级别26中不推荐使用此方法。 而不是NotificationChannel.setSound(Uri,AudioAttributes)。
您需要更改方法的签名。
答案 2 :(得分:1)
从Android O(API级别26)开始,必须将所有通知分配给一个频道。 可以使用以下专用方法在通知通道上设置这些配置:
答案 3 :(得分:0)
我现在使用此功能,并且工作正常
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());
}