2017-09-03 3 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

확인. 오늘 밤 다시 할께. 감사. –

관련 문제