【问题标题】:android notification channel sound not workingandroid通知通道声音不起作用
【发布时间】:2019-05-30 00:21:46
【问题描述】:

我知道有很多关于这个问题的帖子。我都试过了。以下是我执行的步骤。

首先我发现频道一旦创建就无法更改。唯一的方法是重新安装应用程序。所以这就是我所做的,但没有奏效。

其次,有人说我可以删除频道,所以我也使用此代码这样做

val channelList = mNotificationManager.notificationChannels
        var i = 0
        while (channelList != null && i < channelList.size) {
            Log.d("channelList","channel ID is ${channelList[i].id}")
            //mNotificationManager.deleteNotificationChannel(channelList[i].id)
            i++
        }

然后在删除后重新创建频道。

第三,我尝试使用新的通知渠道,但每次使用新渠道时都会收到错误消息。

这是我在尝试过的所有解决方案中使用的代码

 val audioAttributes = AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .build()


        val importance = NotificationManager.IMPORTANCE_DEFAULT

        val channelList = mNotificationManager.notificationChannels
        var i = 0
        while (channelList != null && i < channelList.size) {
            Log.d("channelList","channel ID is ${channelList[i].id}")
            mNotificationManager.deleteNotificationChannel(channelList[i].id)
            i++
        }

        Log.d("isnotification"," is it needed $isNotificationSoundNeeded importance is $importance")
        val mChannel = NotificationChannel(CHANNEL_ID, appName,  NotificationManager.IMPORTANCE_HIGH)
        mChannel.setShowBadge(false)
        mChannel.setSound(notifSound, audioAttributes)



        val mChannelnew = NotificationChannel(CHANNEL_ID2, appName,  NotificationManager.IMPORTANCE_DEFAULT)
        mChannelnew.setShowBadge(false)
        mChannelnew.setSound(notifSound, audioAttributes)




        mNotificationManager.createNotificationChannel(mChannel)

我错过了什么?有任何想法吗?谢谢

更新:这里是 notifsound 的代码

val notifSound = Uri.parse("android.resource://" + packageName + "/" + R.raw.unconvinced)

【问题讨论】:

  • 创建notifSound 对象的代码在哪里?请注意,卸载/重新安装应用不会重置您的频道 - 您需要清除应用上的数据。
  • @ianhanniballake 我更新了我的帖子
  • 您收到哪个错误?
  • @DmitriyMiyai 如果我使用新频道,我会收到频道损坏错误。如果我使用原版它可以工作,但它根本不会注册任何声音。太奇怪了。

标签: android android-notifications android-8.0-oreo


【解决方案1】:

首先,我不知道您的通知不适用于 OreoPie 或低于 N 的设备。

For your question StackOver Flow have lots of answer.

现在根据您的问题,您只缺少一行代码但由于您尚未粘贴,因此无法检查您的整个通知代码。

我在这里粘贴一个通知代码,它只是满足您的所有通知要求。 (完全自定义通知

图片通知

public void createNotificationWithImage(String title,String message ,Bitmap image) {
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
            0 /* Request code */, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

// Custom Sound Uri

Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
                .getPackageName() + "/" + R.raw.sniper_gun);

 mBuilder = new NotificationCompat.Builder(mContext);
 mBuilder.setSmallIcon(R.mipmap.notification_icon);

 // Pay attention on below line here (NOTE)
 mBuilder.setSound(soundUri);

if (image!=null) {
            mBuilder.setContentTitle(title)
                    .setContentText(message)
                    .setAutoCancel(false)
                    .setLargeIcon(image)
                    .setStyle(new NotificationCompat.BigPictureStyle()
                            .bigPicture(image).setSummaryText(message).bigLargeIcon(null))
                    .setColor(Color.GREEN)
                    .setContentIntent(resultPendingIntent);
        }
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

现在我正在粘贴可以在 OREO 设备上方或上运行的通知代码。

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

        if(soundUri != null){
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
            notificationChannel.setSound(soundUri,audioAttributes);
        }

        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());


below middle braces use for close your method.
}

无图片通知

    public void createNotification(String title,String message){
    PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
                0 /* Request code */, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
                .getPackageName() + "/" + R.raw.sniper_gun);



        mBuilder = new NotificationCompat.Builder(mContext);
        mBuilder.setSmallIcon(R.mipmap.notification_icon);
        mBuilder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(),
                R.mipmap.icon));
        mBuilder.setSound(soundUri);
        mBuilder.setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(false)
                .setColor(Color.GREEN)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setContentIntent(resultPendingIntent);

        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//            notificationChannel.s

            if(soundUri != null){
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_ALARM)
                        .build();
                notificationChannel.setSound(soundUri,audioAttributes);
            }

            assert mNotificationManager != null;
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            mNotificationManager.createNotificationChannel(notificationChannel);

        }

        assert mNotificationManager != null;
        mNotificationManager.notify(0 /* Request Code */, mBuilder.build());

    }

注意:在我的代码中,我提到要注意一个特定的行,我描述了将设置声音 Uri 与通知。可以这样形容。

mBuilder.setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(false)
            .setSound(soundUri)
            .setColor(Color.GREEN)
            .setStyle(new NotificationCompat.BigTextStyle())
            .setContentIntent(resultPendingIntent);

但它不会为您播放声音,因为在奥利奥设备未将声音设置为优先级之后。

所以总是像我描述的那样用于声音使用代码。

【讨论】:

  • 我不得不将我的音频属性从通知类型更改为警报,就像这篇文章中一样。
【解决方案2】:

我猜您使用了错误的用法类型,请将您的 audioAttributes 用法编辑为 USAGE_NOTIFICATION

            val audioAttributes = AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .build()

来自官方doc

USAGE_NOTIFICATION : 当使用是通知时使用的使用值。

【讨论】:

    【解决方案3】:

    您需要使用音频属性,还需要定义具有权限的铃声URI。

    所以首先我们定义铃声URI:

    Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    boolean vibrate = true;
    long[] vibratePattern = new long[]{0L, 1000L};
    
    
    
    public constructor(){
     notificationBuilder = new NotificationCompat.Builder(mContext, app.getAppContext().getString(R.string.default_notification_channel_id));
            mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            mContext.grantUriPermission("com.android.systemui", ringtoneUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
     public void showNotificationNormal(String notificationTitle, String notificationBody, Intent intent) {
        String id = mContext.getString(R.string.default_notification_channel_id);
        PendingIntent lowIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext, id);
        NotificationManager mNotifyManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = mContext.getString(R.string.default_notification_channel_name);
            String description = mContext.getString(R.string.default_notification_channel_description); //user visible
            int importance = NotificationManager.IMPORTANCE_HIGH;
    
            AudioAttributes att = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build();
    
            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
            mChannel.setDescription(description);
            mChannel.enableLights(true);
            mChannel.enableVibration(vibrate);
            mChannel.setVibrationPattern(vibratePattern);
            mChannel.setLightColor(Color.RED);
            mChannel.setSound(ringtoneUri, att);
            mChannel.setBypassDnd(true);
            mChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            mChannel.setShowBadge(true);
    
            if (mNotifyManager != null) {
                mNotifyManager.createNotificationChannel(mChannel);
            }
    
            notificationBuilder
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                    .setVibrate(vibratePattern)
                    .setSound(ringtoneUri)
                    .setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
                    .setContentTitle(notificationTitle)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
                    .setAutoCancel(true)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setContentIntent(lowIntent);
    
        } else {
            notificationBuilder.setContentTitle(notificationTitle)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                    .setVibrate(vibratePattern)
                    .setSound(ringtoneUri)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
                    .setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
                    .setAutoCancel(true)
                    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                    .setContentIntent(lowIntent);
    
        }
    
        notificationBuilder.setContentText(notificationBody);
    
        if (mNotifyManager != null) {
            mNotifyManager.notify(AppConstants.NOTIFY_ID, notificationBuilder.build());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多