Sky*_*sey 43 android push-notification android-8.0-oreo android-8.1-oreo
尝试在Android O上显示通知时,我收到此消息.
对于音量控制以外的操作,不推荐使用流类型
该通知直接来自示例文档,并在Android 25上显示正常.
ian*_*ake 60
根据此Google+帖子的评论:
当
NotificationCompat
在Android O设备上使用时,这些[警告]目前是预期的(即使您从未传递自定义声音也NotificationCompat
始终呼叫setSound()
).在支持库更改其代码以使用
AudioAttributes
版本之前setSound
,您将始终收到该警告.
因此,对于此警告,您无能为力.根据通知渠道指南,Android O根本不赞成在单个通知上设置声音,而是让您在特定类型的所有通知所使用的通知通道上设置声音.
Sky*_*sey 47
从Android O开始,您需要配置NotificationChannel,并在尝试显示通知时引用该通道.
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
...
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setVibrate(new long[]{0, 100, 100, 100, 100, 100})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Content Title")
.setContentText("Content Text");
notificationManager.notify(NOTIFICATION_ID, builder.build());
Run Code Online (Sandbox Code Playgroud)
几个重要的注意事项:
NotificationChannel
在实际中指定的覆盖中指定的振动模式等设置Notification
.我知道,它反直觉.您应该移动将更改为通知的设置,或者为每个配置使用不同的NotificationChannel.NotificationChannel
设置传递给之后,您无法修改它们createNotificationChannel()
.你甚至无法打电话deleteNotificationChannel()
然后尝试重新添加它.使用已删除的ID NotificationChannel
将使其复活,它将与首次创建时一样不可变.在卸载应用程序之前,它将继续使用旧设置.因此,您最好确定自己的频道设置,如果您正在使用这些设置,请重新安装该应用,以使其生效.@ sky-kelsey所描述的一切都很好,只是一些小小的补充:
如果它已经注册,你不应该每次注册相同的频道,所以我有Utils类方法为我创建一个频道:
public static final String NOTIFICATION_CHANNEL_ID_LOCATION = "notification_channel_location";
public static void registerLocationNotifChnnl(Context context) {
if (Build.VERSION.SDK_INT >= 26) {
NotificationManager mngr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
if (mngr.getNotificationChannel(NOTIFICATION_CHANNEL_ID_LOCATION) != null) {
return;
}
//
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID_LOCATION,
context.getString(R.string.notification_chnnl_location),
NotificationManager.IMPORTANCE_LOW);
// Configure the notification channel.
channel.setDescription(context.getString(R.string.notification_chnnl_location_descr));
channel.enableLights(false);
channel.enableVibration(false);
mngr.createNotificationChannel(channel);
}
}
Run Code Online (Sandbox Code Playgroud)
strings.xml中:
<string name="notification_chnnl_location">Location polling</string>
<string name="notification_chnnl_location_descr">You will see notifications on this channel ONLY during location polling</string>
Run Code Online (Sandbox Code Playgroud)
在我要显示类型的通知之前,我每次都调用该方法:
...
NotificationUtil.registerLocationNotifChnnl(this);
return new NotificationCompat.Builder(this, NotificationUtil.NOTIFICATION_CHANNEL_ID_LOCATION)
.addAction(R.mipmap.ic_launcher, getString(R.string.open_app),
activityPendingIntent)
.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.remove_location_updates),
servicePendingIntent)
.setContentText(text)
...
Run Code Online (Sandbox Code Playgroud)
另一个典型问题 - 频道默认声音 - 在此描述:https://stackoverflow.com/a/45920861/2133585
归档时间: |
|
查看次数: |
29465 次 |
最近记录: |