
普通通知生成器不会在Android O上显示通知.
如何在AndroID 8 Oreo上显示通知?
是否有任何新代码要添加以在AndroID O上显示通知?
解决方法:
在AndroID O中,必须在Notification Builder中使用频道
下面是一个示例代码:
// Sets an ID for the notification, so it can be updated.int notifyID = 1; String CHANNEL_ID = "my_channel_01";// The ID of the channel. CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.int importance = notificationmanager.importANCE_HIGH;NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);// Create a notification and set the notification channel.Notification notification = new Notification.Builder(MainActivity.this) .setContentTitle("New Message") .setContentText("You've received new messages.") .setSmallicon(R.drawable.ic_notify_status) .setChannelID(CHANNEL_ID) .build();或者处理兼容性:
NotificationCompat notification = new NotificationCompat.Builder(this) .setSmallicon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setChannelID(CHANNEL_ID).build();notificationmanager mnotificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE); mnotificationmanager.createNotificationChannel(mChannel);// Issue the notification.mnotificationmanager.notify(notifyID , notification);或者如果您想要一个简单的修复,那么使用以下代码:
notificationmanager mnotificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { mnotificationmanager.createNotificationChannel(mChannel); }更新:
NotificationCompat.Builder reference
NotificationCompat.Builder(Context context)在API级别26.0.0中不推荐使用此构造函数
所以你应该使用
Builder(Context context, String channelID)所以不需要使用新的构造函数setChannelID.
您应该使用当前最新的AppCompat库26.0.2
compile "com.androID.support:appcompat-v7:26.0.+"来源于Android Developers Channel on Youtube
另外,你可以查看official Android Docs
总结以上是内存溢出为你收集整理的android – 通知没有显示在奥利奥全部内容,希望文章能够帮你解决android – 通知没有显示在奥利奥所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)