2017-09-03 80 views
16

我在自己的通知中使用了自定義的mp3聲音。它可以在API 26以下的所有設備上正常工作。我也試圖在Notification Channel上設置聲音,但仍然沒有工作。它播放默認聲音。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,它適合我。嘗試th代碼:

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

要改變重要性,聲音,燈光,振動,鎖定屏幕或免打擾設置,請卸載應用程序並重新安裝以清除頻道。請參閱https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels,esp標題爲「刪除通知渠道」 – BitByteDog

1

默認聲音覆蓋任何聲音。

你需要把這個在你的代碼:

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

參考:

Android notifications

+0

仍然沒有工作。 –

+4

您必須通過重新安裝應用程序或在添加代碼之前清除與您的應用程序關聯的數據來清除測試通道! –

+0

好的。我今晚再試一次。謝謝。 –