安卓 O 系统通知不显示

3

我正在尝试为Android O版本实现通知。我已经阅读了有关通知管理器和通道的相关内容。所以,Android O仍然不希望再次出现通知。在主要Activity的PostCreate方法中,我写下了以下代码:

    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();

在构建26th 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使用了通知构建器中的一些小变化。现在它也接受chanelid。因此,在26个API中使用具有两个参数的构建器。


可能是重复的问题:尽管创建了通道,但在Android O中未显示通知 - Da2da2
4个回答

4
在创建通知/通知紧凑对象时,请传递通道ID。
Notification.Builder(Context context, String channelId)

或者

NotificationCompat.Builder(Context context, String channelId)

1
我已经做了,但Toast消息会弹出,而日志继续告诉我:“W / Notification:对于除音量控制之外的操作,流类型的使用已被弃用。W / Notification:请参阅setSound()的文档以了解android.media.AudioAttributes中应使用什么来限定您的播放用例。” - eldes

2

我也收到了警告,但是我注意到实际上我仍然收到了通知(下拉托盘),只是它是静音的,并没有在系统栏中显示图标...

我发现你的重要性设置为低:NotificationManager.IMPORTANCE_LOW,这个通道不会播放声音,你需要将其设置为IMPORTANCE_HIGHIMPORTANCE_DEFAULT才能看到和听到你的通知。


2

您不应该在通知构建器上使用setSound方法,而是应该使用您创建的通知渠道来设置这些设置。

notificationChannel.setSound(Uri sound, AudioAttributes audioAttributes);

1
我尝试过这样做,但是当通知到来时仍然会出现默认的声音。有什么建议可能遗漏了吗? - Tulika

-3
你的 alarmSound 应该是其他东西,而不是一个 URI 吧?

问题出在新的Android O通知上。即使没有设置声音,它也不会出现,并且日志中的警告仍然相同。 - Oleksiy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接