android - 我正在尝试显示由我创建的通知,而不是 firebase。它适用于旧手机,而不是奥利奥
问题描述
这是尝试创建通知的代码:
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
PSTrip activeTrip = getActiveTrip();
Intent intent = new Intent(context, PSNewJourneyActivity.class);
intent.putExtra("id", activeTrip.getId());
intent.putExtra("userid", activeTrip.getOwner().getId() + "");
intent.putExtra("mode", "active");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
playCheckoutSound(context, false);
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent pIntent = PendingIntent.getActivity(context, iUniqueId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "general");
mBuilder.setContentTitle(context.getString(R.string.passenger_name)).setContentText(context.getString(R.string.pit, "name")).setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentIntent(pIntent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(iUniqueId, notification);
如您所见,我选择了 Channel ID,并为通知选择了一个 uniqueID,但仍然没有运气。我在这里做错了什么?我也尝试在 Looper.getMainLooper 中调用它,不知道它会影响它,但仍然没有运气。
PS:我的应用有前台服务,所以它有一个永久的通知,让用户知道我的应用在后台运行。这是导致问题的原因吗?
编辑:
我在.notify 之前添加了这个,在答案中找到:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
if (notificationChannel == null) {
int importance = NotificationManager.IMPORTANCE_HIGH;
notificationChannel = new NotificationChannel(channelId, channelDescription, importance);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
}
我的手机震动了,但仍然没有显示通知。
开发工具包版本:
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 17
targetSdkVersion 27
}
手机:华为 Mate 10 Pro。安卓8.0
Logcat 向我抛出了这个问题:
05-22 12:34:32.916: E/NotificationService(1184): No Channel found for pkg=nl.hgrams.passenger, channelId=general, id=124397682, tag=null, opPkg=nl.hgrams.passenger, callingUid=10233, userId=0, incomingUserId=0, notificationUid=10233, notification=Notification(channel=general pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)
解决方案
如果您的应用面向 Android 8.0,那么您必须指定通知渠道。
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME,
notifManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(notificationChannel);
检查此链接以创建频道。
https://www.androidauthority.com/android-8-0-oreo-app-implementing-notification-channels-801097/
推荐阅读
- vue.js - 嵌入式社交媒体提要在 Nuxt Netlify Hosted (VUE) 上不起作用
- json - 到 JSON 的 T-SQL 输出未按预期工作
- web - 创建具有以下功能的网站:
- java - Rocker 模板是否支持 Java 11
- typescript - 强键入 Twitter 小部件以摆脱成员访问错误
- python - 使用 RegEx 修改文本文件后,如何下载新修改的文件?
- django - 带有多个 ForeignKey 引用的 Django 注释问题
- html - CSS Flexbox 纵横比
- assembly - 如何从给定的 x86-64 程序集中找到被调用者保存的寄存器和调用者保存的寄存器?
- python - 为什么迭代过去 -6 后 -50 到 50 的边缘情况不起作用?从排序的linkedList中删除重复项