我正在从我们的应用服务器发送数据消息,调用firebase api到users.Users,我们正在接收通知,但当通知到达时,声音没有播放。
以下是代码
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();
}
}
问题是什么?
发布于 2018-06-08 15:40:03
您需要在通知生成器上设置声音。所以,试着在下面的行中取消注释。
.setSound(defaultSoundUri)
并在下面的行下注释。
//Ringtone r = RingtoneManager.getRingtone(this, defaultSoundUri);
//r.play();
如果你想在app后台播放通知声音,你需要add the sound parameter到通知负载。
支持默认或捆绑在应用中的声音资源的文件名。声音文件必须位于/res/raw/
中
https://stackoverflow.com/questions/50755365
复制相似问题