android9通知栏适配,android 9适配通知栏

android 9适配通知栏

发布时间:2018-11-07 15:06,

浏览次数:966

, 标签:

android

最近安装了一个9.0的模拟器,发现通知栏不显示,也没有任何打印日志,把过滤条件改成“No Filters”就可以看到

2018-11-07 14:52:03.987 1908-1992/? E/NotificationService: No Channel found

for pkg=com.dahai.floatnotes, channelId=id, id=1, tag=null,

opPkg=com.dahai.floatnotes, callingUid=10087, userId=0, incomingUserId=0,

notificationUid=10087, notification=Notification(channel=id pri=0

contentView=null vibrate=null sound=null defaults=0x0 flags=0x0

color=0xff008577 category=reminder vis=PRIVATE)

以前设置通知的代码,在8.0没有问题

NotificationCompat.Builder notificationCompatBuilder = new

NotificationCompat.Builder(getApplicationContext(), "packageName");

Notification notification = notificationCompatBuilder // Title for API <16 (4.0

and below) devices. .setContentTitle("标题") // Content for API <24 (7.0 and

below) devices. .setContentText("内容") .setSmallIcon(R.mipmap.ic_launcher)

.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.ic_logo))

.setContentIntent(notifyPendingIntent)

.setDefaults(NotificationCompat.DEFAULT_ALL)

.setColor(ContextCompat.getColor(getApplicationContext(),

R.color.colorPrimary)) .setCategory(Notification.CATEGORY_REMINDER)

.setPriority(NotificationCompat.PRIORITY_DEFAULT) .build();

NotificationManagerCompat.from(getApplicationContext()).notify(1, notification);

查看了官方文档介绍找了很久才找到如何解决官方描述

现在在设置渠道的时候需要设置到系统中去

public static String createNotificationChannel(Context context) { if

(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId =

"channelId"; CharSequence channelName = "channelName"; String

channelDescription ="channelDescription"; int channelImportance =

NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel notificationChannel

= new NotificationChannel(channelId, channelName, channelImportance); // 设置描述

最长30字符 notificationChannel.setDescription(channelDescription); // 该渠道的通知是否使用震动

notificationChannel.enableVibration(true); // 设置显示模式

notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);

NotificationManager notificationManager = (NotificationManager)

context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.createNotificationChannel(notificationChannel); return

channelId; } else { return null; } } NotificationCompat.Builder

notificationCompatBuilder = new

NotificationCompat.Builder(getApplicationContext(),

createNotificationChannel(mContext)); Notification notification =

notificationCompatBuilder // Title for API <16 (4.0 and below) devices.

.setContentTitle("标题") // Content for API <24 (7.0 and below) devices.

.setContentText("内容") .setSmallIcon(R.mipmap.ic_launcher)

.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.ic_logo))

.setContentIntent(notifyPendingIntent)

.setDefaults(NotificationCompat.DEFAULT_ALL)

.setColor(ContextCompat.getColor(getApplicationContext(),

R.color.colorPrimary)) .setCategory(Notification.CATEGORY_REMINDER)

.setPriority(NotificationCompat.PRIORITY_DEFAULT) .build();

NotificationManagerCompat.from(getApplicationContext()).notify(1, notification);

这样就能显示出来了

还有一个问题startForeground

这里也要传入一个通知,如果不错适配会直接报错

android.app.RemoteServiceException: Bad notification for startForeground:

java.lang.RuntimeException: invalid channel for service notification:

Notification(channel=id pri=0 contentView=null vibrate=null sound=null

defaults=0x0 flags=0x40 color=0xff008577 vis=PRIVATE) at

android.app.ActivityThread$H.handleMessage(ActivityThread.java:1737) at

android.os.Handler.dispatchMessage(Handler.java:106) at

android.os.Looper.loop(Looper.java:193) at

android.app.ActivityThread.main(ActivityThread.java:6669) at

java.lang.reflect.Method.invoke(Native Method) at

com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

该问题的解决方法和通知栏一样,不过还要加一个权限

android:name="android.permission.FOREGROUND_SERVICE"/> 该权限是普通权限,可以直接添加

推荐一款应用悬浮笔记 ,以上问题都在这个APP中出现过并已解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值