Android 我正在尝试显示由我创建的通知,而不是firebase。它适用于旧手机,而不是奥利奥

Android 我正在尝试显示由我创建的通知,而不是firebase。它适用于旧手机,而不是奥利奥,android,firebase,notifications,firebase-cloud-messaging,android-8.0-oreo,Android,Firebase,Notifications,Firebase Cloud Messaging,Android 8.0 Oreo,这是尝试创建通知的代码: NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notifManager.cancelAll(); PSTrip activeTrip = getActiveTrip(); Intent intent = new Intent(context, PSNewJourneyActivity.cl

这是尝试创建通知的代码:

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);
如您所见,我选择了频道ID,并为通知选择了一个唯一的ID,但仍然没有运气。我做错了什么? 我还试着在Looper.getMainLooper中调用它,不知道它会影响它,但仍然没有运气

PS:我的应用程序有一个前台服务,所以它有一个永久通知,让用户知道我的应用程序在后台工作。这就是问题的根源吗

编辑: 我以前添加过此项。通知,在答复中找到:

 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);
                    }
                }
我的手机会震动,但仍无法显示通知

SDK版本:

 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)

如果你的应用目标是安卓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);
检查此链接以创建频道


您也需要创建频道:。请检查我的编辑。立即测试您的目标sdk版本和编译器sdk版本PS:我还修改并尝试了您创建通知的代码,但相同。请参阅注释:在测试期间帮助您捕获此错误,每当你的应用程序试图发布一个没有指定频道的通知时,你可能想告诉Android 8.0显示一个祝酒词:打开你设备的“设置”应用程序。导航到“系统>开发人员选项”。将“显示通知频道警告”滑块推到“打开”位置。我没有。但是如果我检查logcat,我会发现这个问题:
05-22 12:34:32.916:E/NotificationService(1184):找不到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 Vibration=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)