1

我發佈了用於檢索郵件並顯示來自Firebase的通知的代碼。我收到正確的文本和標題的通知,但此通知是無聲的。即使我設置了默認聲音或自定義聲音。 如何播放正確的聲音?Android通知生成器不播放自定義聲音

公共類NotificationMessagingService擴展FirebaseMessagingService {

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Log.d("sb.fbv", "NotificationMessaginService.onmessageReceived"); 
    String title = remoteMessage.getNotification().getTitle(); 
    String subText = remoteMessage.getNotification().getBody(); 
    String message = remoteMessage.getData().get("subtext"); 

    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 

    NotificationCompat.Builder nb = new NotificationCompat.Builder(this); 
    nb.setContentTitle(title); 
    nb.setSubText(message); 
    nb.setContentText(subText); 
    nb.setSmallIcon(R.drawable.notificavedelago); 
    nb.setAutoCancel(true); 
    nb.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); 
    nb.setLights(Color.BLUE, 3000, 3000); 

    //SOUND DEFAULT 
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    nb.setSound(alarmSound); 

    //CUSTOM SOUND 
    //Uri customSound= Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound); 
    //nb.setSound(customSound); 

    nb.setContentIntent(pendingIntent); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0, nb.build()); 

} 

}

+1

要設置自定義通知使用: nb.setDefaults(Notification.DEFAULT_SOUND); 對於自定義通知,我無法幫助您 –

回答

0

要設置在通知製造商定製的報警聲,這樣做:

int soundResId = R.raw.myCustomSoundResource; 
Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + soundResId); 
nb.setSound(soundUri, AudioManager.STREAM_ALARM); 

使用的資源ID號像res/raw/myCustomSoundResource.ogg這樣的文件。它可以在any of these supported file formats