androID 8.0 以后的版本,在创建通知栏的时候,加了一个channelID的东西。要想在上述版本中显示通知,总共分两步
1.创建Channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelID = "whatever"; //根据业务执行 String channelname = "whatever conent"; //这个是channelID 的解释,在安装的时候会展示给用户看 int importance = notificationmanager.importANCE_HIGH; createNotificationChannel(channelID, channelname, importance);}
2.引用
Notification notification = new Notification.Builder(this,"whatever") //引用加上channelID .setSmallicon(R.drawable.donkey) .setWhen(System.currentTimeMillis()) .setContentTitle("随便") .setContentText("随随便便写") .setContentIntent(pendingIntent) .build();
为了兼容androID所有版本,最好在代码里做一下适配
manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE);Intent intent = new Intent(this, AudioPlayerActivity.class);intent.putExtra("Notifiction",true);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelID = "whatever"; //根据业务执行 String channelname = "whatever conent"; //这个是channelID 的解释,在安装的时候会展示给用户看 int importance = notificationmanager.importANCE_HIGH; createNotificationChannel(channelID, channelname, importance);}PendingIntent pendingIntent = PendingIntent.getActivity(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);Notification notification = null;if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ notification = new Notification.Builder(this,"whatever") //引用加上channelID .setSmallicon(R.drawable.donkey) .setWhen(System.currentTimeMillis()) .setContentTitle("随便") .setContentText("随随便便写") .setContentIntent(pendingIntent) .build();}else{ notification = new Notification.Builder(this) .setSmallicon(R.drawable.donkey) .setWhen(System.currentTimeMillis()) .setContentTitle("随便") .setContentText("随随便便写") .setContentIntent(pendingIntent) .build();}manager.notify(1,notification);
总结
以上是内存溢出为你收集整理的如何解决android 通知栏不显示的问题全部内容,希望文章能够帮你解决如何解决android 通知栏不显示的问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)