Android O 通知不显示

标签 android notifications android-8.0-oreo

我正在尝试为 Android O 版本实现通知。 我已经阅读了有关通知管理器和 channel 的信息。所以 Android O 仍然不想重现通知。在 PostCreate 方法的主要 Activity 上,我写了这个。

    NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String CHANNEL_ID = "my_channel_01";
    String CHANNEL_NAME = "my Channel Name";
    int NOTIFICATION_ID = 1;

    Intent notificationIntent = new Intent(this, MainActivity.class);

    PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationChannel notificationChannel = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

        notificationChannel = new NotificationChannel(CHANNEL_ID,
                CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setShowBadge(true);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        mNotifyManager.createNotificationChannel(notificationChannel);
    }
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Notification myNotification = new NotificationCompat.Builder(MainActivity.this)
            .setContentTitle("You have been notify")
            .setContentText("This is your Notifiaction Text")
            .setSmallIcon(R.drawable.ic_donut_large_black_24dp)
            .setChannel(CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setContentIntent(notificationPendingIntent)
            .setAutoCancel(true)
            .setSound(alarmSound)
            .build();

    mNotifyManager.notify(NOTIFICATION_ID, myNotification);
    Toast.makeText(this, "accepted", Toast.LENGTH_SHORT).show();

在为第 26 个 API 构建后,它不会创建通知,触发 Toast 消息,并且日志会告诉我:

W/Notification: Use of stream types is deprecated for operations other than volume control W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

如何处理这种错误情况?

更新。经过一些调查,我发现 26 api 在通知生成器中使用了一些小改动。现在它也接受香奈儿。因此,对于 26,使用带有两个参数的生成器。

最佳答案

在创建通知/notificationcompact 对象时传递 channel ID。

Notification.Builder(Context context, String channelId)

NotificationCompat.Builder(Context context, String channelId)

关于Android O 通知不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46002219/

相关文章:

java - adb连接错误:EOF (new in android )

android - FLAG_ACTIVITY_NEW_TASK 在 PendingIntent 中使用时未按预期运行

laravel - 在 laravel 通知为 null 时调用成员函数 delay()

cocoa - 使用 NSRect 信息发布 NSNotification 的最佳方式?

android - 字体系列导致 android 26 上的 NPE

android - 如何在 Android 或 iPhone 上以 React Native 方式处理其他应用程序通知?

java - 从另一个ArrayList复制过来的ArrayList<Object>即使有数据也是空的

java - 使用 Retrofit 从最内部类获取数据。帮我显示A类和B类的数据

android - 启用 Android 零售演示

android - 如何在 Kiosk 模式下运行 Android 应用程序,禁用安全模式并防止设备硬重置?