Android Notification一些坑

Android0上运行以前正常的通知可能会有各种问题

  • 1.通知栏不显示(使用PendingIntent.getBroadcast方式)

第一步添加代码如下

 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channel = AppUtil.getChannel();
            NotificationChannel mChannel = new NotificationChannel(context.getPackageName(), channel, NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(mChannel);
        }
        xxxxx
  builder.setChannelId(context.getPackageName());

第二:部分机型(比如vivio)需要在设置里面打开对应应用的通知开关

  • 2.通知栏点击没反应
    需要添加代码如下:
 Intent broadcastIntent = new Intent(actionNAme);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            broadcastIntent.setClass(context, A.Class);
        }

官方说明
I ran into the same, in Android Oreo you need to make it a explicit Intent (is not enough with putting the receiver on the manifest, in fact, it won’t pay attention to that), so when you make the intent, make it explicit using the setClass method:

你可能感兴趣的:(android应用,Notification)