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