android - 通知不可见或未到来
问题描述
我正在尝试startForegroundService()
在 androidO
及以上设备中启动。
服务开始了。在onCreate()
服务的方法中,我添加了startForeground()
with 通知。
但是通知没有来。我无法看到它。
我在onCreate()
服务方法中的代码:
Notification.Builder builder = new Notification.Builder(this, "1")
.setContentTitle(getString(R.string.app_name))
.setContentText("in app filling")
.setAutoCancel(true);
Notification notification = builder.build();
startForeground(1, notification);
解决方案
解决方案:
第一步:创建一个NotificationChannel
NotificationChannel notificationChannel = new NotificationChannel(channel_id , channel_name, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
这里,channel_id 和 channel_name 分别是int
和string
变量。
第2步:将其附加到NotificationManager
:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
第 3 步:创建您的通知:
NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("Test Title")
.setContentText("Test Message")
.setSmallIcon(R.mipmap.ic_launcher);
Step4:在同一个NotificationManager
对象中附加通知
notificationManager.notify(1, notification.build());
最后,进行检查以通知它是否高于 Android O:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
....
}
参考和更多关于这个可以找到这里
希望能帮助到你。
推荐阅读
- javascript - 如何在 CSS 中引用 document.createElement
- reactjs - 在 MERN TypeError 中出现错误:无法读取未定义的属性“地图”
- android - 包“com.instagram.android”的开发者警告
- dotnetnuke-9 - Dnn 9 - 将管理语言设置为英语,将站点语言设置为荷兰语
- c++ - 使用 Tensorflow 的 C++ ABI 兼容性问题
- angular - 更改路由并将数据传递给另一个组件
- angular - 在构造函数中获取 Ionic 4 存储令牌值的解决方案是什么,例如在运行时将令牌设置为 http 标头?
- oauth - eBay OAuth 无法获取令牌
- mysql - MySql 服务器不是从命令提示符启动的
- google-cloud-platform - 所需的复合索引不存在,但在 index.yaml 中定义