【问题标题】:Android O notification not showingAndroid O 通知未显示
【发布时间】:2017-09-01 14:16:42
【问题描述】:

我正在尝试为 Android O 版本实现通知。 我已阅读有关通知管理器和频道的信息。所以 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:不推荐使用流类型进行音量控制以外的操作 W/Notification:请参阅 setSound() 的文档,了解如何使用 android.media.AudioAttributes 来限定您的播放用例

如何处理这种错误情况?

更新。经过一番调查,我发现 26 api 在通知生成器中使用了一些变化。现在它也接受chanelid。所以对于 26 使用带有两个参数的构建器。

【问题讨论】:

标签: android notifications android-8.0-oreo


【解决方案1】:

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

我看到您的重要性设置为低:NotificationManager.IMPORTANCE_LOW 用于一个不会播放声音的频道(我的也是),您需要将其设置为IMPORTANCE_HIGHIMPORTANCE_DEFAULT 才能真正看到和听到你的通知。

【讨论】:

    【解决方案2】:

    在创建通知/通知压缩对象时传递频道 ID。

    Notification.Builder(Context context, String channelId)
    

    NotificationCompat.Builder(Context context, String channelId)
    

    【讨论】:

    • 我这样做了,但是 Toast 消息会触发,并且日志继续告诉我:“W/Notification:不推荐使用流类型用于音量控制以外的操作 W/Notification:请参阅 setSound( ) 使用什么来代替 android.media.AudioAttributes 来限定您的播放用例"
    【解决方案3】:

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

    notificationChannel.setSound(Uri sound, AudioAttributes audioAttributes);
    

    【讨论】:

    • 我试过这样做,但是当通知到达时仍然会出现默认声音。任何可能缺少的建议
    【解决方案4】:

    您的 alarmSound 应该是别的东西,而不是 URI?

    【讨论】:

    • 问题出现在新的 Android O 通知中。即使没有 setSound 也不会出现,并且日志中的警告保持不变。
    猜你喜欢
    • 2013-04-09
    • 1970-01-01
    • 2021-03-23
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2021-12-09
    • 1970-01-01
    相关资源
    最近更新 更多