android8.0后台服务显示通知栏,Android 8.0之后通知栏悬浮栏不显示问题

先上效果图

bc634dd785aca4dbbfd7e7b3de425fdd.png

参考这篇文章解决

/**

* 发送通知

* @param content

*/

@SuppressLint("WrongConstant")

private void sendNotification(String content) {

try {

String[] contents = content.split(",");

Intent intent = new Intent();

intent.setClass(this, TabHostActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

final NotificationManager notifiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

String id = getPackageName();

String name = "call_msg";

//Android 8.0 的要手动设置NotificationChannel

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

//NotificationManager.IMPORTANCE_MAX 最高支持 声音和悬浮

NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_MAX);

notifiManager.createNotificationChannel(channel);

}

Notification notification = new NotificationCompat.Builder(this)

.setAutoCancel(true)

.setPriority(Notification.PRIORITY_MAX)

.setSmallIcon(R.drawable.pre_icon)

.setContentTitle(contents[0])

.setContentText(contents[1])

.setVisibility(VISIBILITY_PUBLIC)

.setWhen(System.currentTimeMillis())

//向通知添加声音、闪灯和振动效果

.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND)

.setContentIntent(pendingIntent)

.setChannelId(id)

.build();

notifiManager.notify(1, notification);

} catch (Exception e) {

e.printStackTrace();

}

}

主要适配sdk26(Android 8.0)之后的

然后使用.setChannelId(id)方法

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

//NotificationManager.IMPORTANCE_MAX 最高支持 声音和悬浮

NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_MAX);

notifiManager.createNotificationChannel(channel);

}

标签:8.0,NotificationManager,悬浮,MAX,Intent,Notification,NotificationChannel,Android,i

来源: https://blog.csdn.net/weixin_45480533/article/details/105795419

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值