android 推送 声音播放器,Android推送通知声音仅在应用处于前景时播放,但在应用处于后台时不播放声音...

我正在使用FCM进行推送通知

下面的代码在收到通知时发出声音

public void playNotificationSound() {

try {

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

Ringtone r = RingtoneManager.getRingtone(mContext,notification);

r.play();

} catch (Exception e) {

e.printStackTrace();

}

}

我正在调用这个OnMessageReceived方法,但声音仅在应用程序处于前台时播放,而不是在应用程序处于后台时播放

@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

Log.e(TAG,"From: " + remoteMessage.getFrom());

if (remoteMessage == null)

return;

// Check if message contains a notification payload.

if (remoteMessage.getNotification() != null) {

Log.e(TAG,"Notification Body: " + remoteMessage.getNotification().getBody());

handleNotification(remoteMessage.getNotification().getBody());

}

// Check if message contains a data payload.

if (remoteMessage.getData().size() > 0) {

Log.e(TAG,"Data Payload: " + remoteMessage.getData().toString());

try {

JSONObject json = new JSONObject(remoteMessage.getData().toString());

handleDataMessage(json);

} catch (Exception e) {

Log.e(TAG,"Exception: " + e.getMessage());

}

}

}

private void handleNotification(String message) {

if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {

// app is in foreground,broadcast the push message

Intent pushNotification = new Intent(config.PUSH_NOTIFICATION);

pushNotification.putExtra("message",message);

LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

// play notification sound

NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());

notificationUtils.playNotificationSound();

}else if (NotificationUtils.isAppIsInBackground(getApplicationContext())){

// If the app is in background,firebase itself handles the notification

NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());

notificationUtils.playNotificationSound();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值