今天在写一个demo时,通知栏死活显示不出来,通过各种百度,终于解决了。
解决方法:
= "channel_01";
String name="我是渠道名字";
//启动浏览器
Intent contentIntent = new Intent();
contentIntent.setData(Uri.parse("http://www.baidu.com"));//Url 就是你要打开的网址
contentIntent.setAction(Intent.ACTION_VIEW);
//点击通知栏做的事
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, contentIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//获取系统通知服务
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
Toast.makeText(this, mChannel.toString(), Toast.LENGTH_SHORT).show();
notificationManager.createNotificationChannel(mChannel);
notification = new Notification.Builder(this)
.setContentTitle("新消息1")
.setContentText("Hello world")
.setSmallIcon(R.mipmap.ic_launcher)
.setChannelId(id)
.setContentIntent(pendingIntent)//设置pendingIntent,点击通知时就会用到
.build();
} else {
notification = new NotificationCompat.Builder(this)
.setContentTitle("新消息2")
.setContentText("Hello world")
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setChannelId(id)
.setContentIntent(pendingIntent) //设置pendingIntent,点击通知时就会用到
.build();
}
notificationManager.notify(0, notification);
谢谢阅读