Android 8和9上未显示通知

问题描述 投票:0回答:1

我正在开发一个包含通知的Android应用程序。我试过新的Android 8通知代码:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "Channel"+notificationId)
                .setSmallIcon(R.drawable.ic_notification_logo)
                .setContentTitle(notification.getTitle())
                .setContentText(notification.getBody())
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setChannelId("Channel"+notificationId);

NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String CHANNEL_ID = "Channel" + notificationId;// The id of the channel.
        CharSequence name = "MyApp";// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        notificationManager.createNotificationChannel(mChannel);
    }

    notificationManager.notify(notificationId, notification);

但仍然不起作用,并且在8和9上都没有显示任何通知。

android android-notifications android-8.0-oreo notificationmanager
1个回答
1
投票

我相信你需要更换

notificationManager.notify(notificationId, notification);

notificationManager.notify(notificationId, notificationBuilder.build());

使它工作。

© www.soinside.com 2019 - 2024. All rights reserved.