代码之家  ›  专栏  ›  技术社区  ›  Ashish John

自定义通知声音在Oreo中不工作

  •  19
  • Ashish John  · 技术社区  · 8 年前
    Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
                mBuilder.setSound(sound);
    

    我已将MP3(notification_mp3.mp3)文件复制到res文件夹的原始文件夹中。当通知被触发时,它会产生给定的MP3声音 安卓本体 但默认声音输入 安卓奥里奥 。 我参考了很多网站,但没有任何工作 安卓奥里奥 . 在上面的Android操作系统中,我没有发现Android文档中关于通知声音的任何更改。 为了使这个代码在Android O中也能工作,应该做些什么更改?

    4 回复  |  直到 8 年前
        1
  •  25
  •   Ashish John    8 年前

    要在Oreo中将声音设置为通知,必须将声音设置为 NotificationChannel 而不是在 Notification Builder 本身。你可以这样做

    Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.notification_mp3);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
            NotificationChannel mChannel = new NotificationChannel("YOUR_CHANNEL_ID",
                "YOUR CHANNEL NAME",
                NotificationManager.IMPORTANCE_DEFAULT)
    
            AudioAttributes attributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
    
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, 
                    context.getString(R.string.app_name),
                    NotificationManager.IMPORTANCE_HIGH);
    
            // Configure the notification channel.
            mChannel.setDescription(msg);
            mChannel.enableLights(true);
            mChannel.enableVibration(true);
            mChannel.setSound(sound, attributes); // This is IMPORTANT
    
    
            if (mNotificationManager != null)
                mNotificationManager.createNotificationChannel(mChannel);
        }
    

    这将为您的通知设置自定义声音。但是,如果应用程序正在更新,并且以前使用过通知通道,它将不会被更新。也就是说,你需要创建一个不同的频道,并设置声音使其工作。但这将在应用程序信息的通知部分显示多个频道。如果您将声音设置为全新的频道,这很好,但如果您希望以前使用该频道,则必须删除现有频道并重新创建该频道。要做到这一点,您可以在创建频道之前做类似的事情。

    if (mNotificationManager != null) {
                List<NotificationChannel> channelList = mNotificationManager.getNotificationChannels();
    
                for (int i = 0; channelList != null && i < channelList.size(); i++) {
                    mNotificationManager.deleteNotificationChannel(channelList.get(i).getId());
                }
            }
    
        2
  •  0
  •   Jay Thummar    8 年前

    创建一个通道(我在 Application.clss 创建通道)

      public void initChannels(Context context) {
        if (Build.VERSION.SDK_INT < 26) {
            return;
        }
        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel("default"/*CHANNEL ID*/,
                "CHANNEL_NAME",
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("Channel description");
        assert notificationManager != null;
        notificationManager.createNotificationChannel(channel);
    }
    

    使用这个频道 default 创建的实例时 NotificationCompat

     .... notificationBuilder = new NotificationCompat.Builder(this,"default") ....
    
        3
  •  0
  •   VIKAS SHARMA    8 年前

    试试这个:

    /**
     * show notification
     *
     * @param message
     */
    private static void showNotification(RemoteMessage message, Context baseContext) {
        Context context = baseContext.getApplicationContext();
        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context.getApplicationContext());
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, null)
                .setSmallIcon(R.drawable.ic_logo_xxxdpi)
                .setContentTitle(message.getData().get(TITLE))
                .setContentText(message.getData().get(BODY))
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setAutoCancel(true)
                .setVibrate(new long[]{500, 500})
                .setLights(Color.RED, 3000, 3000)
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                .setContentIntent(getPendingIntent(context, message));
        managerCompat.notify(getRandom(), builder.build());
    }
    
        4
  •  0
  •   Nawrez    8 年前

    android o附带了notificationchannel,使用它来代替

     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});
                assert mNotificationManager != null;
                mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
                mNotificationManager.createNotificationChannel(notificationChannel);