通知未在Android 9中显示

Pun*_*mar 6 android-notifications android-9.0-pie

我正在使用此代码在我的android应用程序中显示通知。这在所有android版本中都可以正常工作,但是在Android 9中没有任何通知显示。

我试图用不同的方法来实现这一点,但没有任何效果。

    public void showNotification(String heading, String description, String imageUrl, Intent intent){
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    createChannel();
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"channelID")
            .setSmallIcon(R.drawable.logo)
            .setContentTitle(heading)
            .setContentText(description)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int notificationId = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
    notificationManager.notify(notificationId, notificationBuilder.build());
}

public void createChannel(){
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationManager notificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel channel = new NotificationChannel("channelID","name", NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Description");
    notificationManager.createNotificationChannel(channel);
}
Run Code Online (Sandbox Code Playgroud)

谢谢..

Jaw*_*Zeb 1

您好,这是一个很晚的答案,但我仍然想标记代码中的错误以及工作代码片段,以便它可以帮助其他人。

您在 showNotification(..) 函数中有两个 notificationManager 实例,在 createChannel() 函数中有另一个实例。您必须在 notificationManager 的同一实例中创建通道,因此您的工作代码将如下所示:

  public void showNotification(String heading, String description, String imageUrl, Intent intent){
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"channelID")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle(heading)
                .setContentText(description)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int notificationId = 1;
        createChannel(notificationManager);
        notificationManager.notify(notificationId, notificationBuilder.build());
    }

    public void createChannel(NotificationManager notificationManager){
        if (Build.VERSION.SDK_INT < 26) {
            return;
        }
        NotificationChannel channel = new NotificationChannel("channelID","name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("Description");
        notificationManager.createNotificationChannel(channel);
    }
Run Code Online (Sandbox Code Playgroud)

打电话就像:

showNotification("Heading","Description","",new Intent(this,MainActivity.class));
Run Code Online (Sandbox Code Playgroud)

结果: 在此输入图像描述