我正在从我们的app服务器发送数据消息,呼叫firebase api给用户。用户正在接收通知,但是当通知到达时声音没有播放。
这是代码
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
sendNotification(remoteMessage);
}
private void sendNotification(RemoteMessage message) {
Intent intent;
intent = new Intent(this, HomeActivity.class);
Log.d(TAG, message.getData().toString());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(this, defaultSoundUri);
r.play();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(message.getData().get("title"))
.setContentText(Html.fromHtml(message.getData().get("body")))
.setAutoCancel(true)
// .setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
saveMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
可能是什么问题???
答案 0 :(得分:0)
您需要在通知构建器上设置声音。 因此,请尝试在线下取消注释。
.setSound(defaultSoundUri)
并在下面注释。
//Ringtone r = RingtoneManager.getRingtone(this, defaultSoundUri);
//r.play();
如果您想在应用处于后台时播放通知声音,则需要add the sound parameter到通知有效负载。
支持应用中捆绑的声音资源的"默认" 或文件名。声音文件必须位于/res/raw/