2017-09-03 9 views
16

私は自分の通知に使用するカスタムmp3サウンドを持っています。それはAPI 26の下のすべてのデバイスで正常に動作します。私は通知チャネルでサウンドを設定しようとしましたが、まだ動作しません。デフォルトのサウンドが再生されます。通知音のAPI 26

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId) 
      .setAutoCancel(true) 
      .setSmallIcon(R.drawable.icon_push) 
      .setColor(ContextCompat.getColor(this, R.color.green)) 
      .setContentTitle(title) 
      .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification)) 
      .setDefaults(Notification.DEFAULT_VIBRATE) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
      .setContentText(message); 
     Notification notification = builder.build(); 
     NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { 
      NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT); 
      AudioAttributes audioAttributes = new AudioAttributes.Builder() 
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 
        .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE) 
        .build(); 
      channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification), audioAttributes); 
      notificationManager.createNotificationChannel(channel); 
     } 
     notificationManager.notify(1, notification); 
+0

デフォルトの通知音をミュートしましたか?私も同じ問題に直面しています。 NotificationManager.IMPORTANCE_MAXを使用して通知音を鳴らしたくない。これで私を助けてくれますか? –

答えて

7

私はRingtoneManagerを使いました。それは私の仕事です。 thiusコードを試してください:

NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this); 
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert); 
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")); 
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); 
    builder.setContentIntent(pendingIntent); 
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 
    builder.setContentTitle("Notification title"); 
    builder.setContentText("Notification message."); 
    builder.setSubText("Url link."); 

    try { 
     Uri notification = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_ringtone); 
     Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); 
     r.play(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(1, builder.build()); 
+0

ありがとうございます。それは働いている。 –

+0

私の経験では、何度も音を再生しています。 – HoseinIT

+0

このソリューションの問題点は、ユーザーが通知音をミュートできないことです。それは常に音を演奏する。 –

12

チャンネルは、元々デフォルトサウンドで作成されている場合があります。チャンネルが作成されると、変更することはできません。アプリを再インストールするか、新しいチャンネルIDでチャンネルを作成する必要があります。

+0

チャンネルの名前を変更しようとしましたが、まだ動作しません。 –

+0

小さな世界:-)おかげでPaweł、あなたは私に多くの時間を節約! –

+0

重要度、音、光、振動、ロック画面、またはDND設定を変更するには、アプリをアンインストールしてからもう一度インストールしてチャンネルをクリアします。 https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels、「通知チャンネルの削除」のセクションをご覧ください。 – BitByteDog

1

デフォルトのサウンドはすべてのサウンドよりも優先されます。

あなたはあなたのコードでこれを配置する必要があります。

notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE; 

はリファレンス:オレオで

Android notifications

+0

まだ動作しません。 –

+4

テストチャンネルをクリアする必要があります。アプリを再インストールするか、アプリに関連付けられたデータをクリアしてからコードを追加してください。 –

+0

私は今夜​​もう一度お試しになります。ありがとう。 –