[英]Android Push notification sound is played only when app is in foreground but not playing sound when app is in background
I am using FCM for push notification below code to play sound when notification received我在代码下方使用 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();
}
}
I am calling this OnMessageReceived Method but the sound is only play when app is in foreground,not playing when app is in background我正在调用此 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();
}
}
Since onMessageReceived()
is not called when a notification object is sent, I've created a BroadcastReceiver to handle it when a notification arrives:由于在发送通知对象时不会调用onMessageReceived()
,因此我创建了一个 BroadcastReceiver 来在通知到达时处理它:
public class NotificationReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
playNotificationSound(context);
}
public void playNotificationSound(Context context) {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
and added it to manifest.并将其添加到清单中。 The Receiver is responsible to play the notification ringtone. Receiver 负责播放通知铃声。
<receiver
android:name=".notification.NotificationReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
When sending notifications in Android through the Firebase Console, it will be treated as a Notification Message.在 Android 中通过 Firebase 控制台发送通知时,它将被视为通知消息。 Notification messages will always be handled automatically by the Android device (System Tray) when the app is in background (see Handling messages ).当应用程序处于后台时,通知消息将始终由 Android 设备(系统托盘)自动处理(请参阅处理消息)。
Which means that onMessageReceived()
will not be called.这意味着不会调用onMessageReceived()
。 Hence, if you intend to always play a sound upon receiving a notification, you'll have to make use of a Data Message* instead.因此,如果您打算在收到通知时始终播放声音,则必须改用数据消息*。 But you'll have to send the messages without the use of the Firebase Console.但是您必须在不使用 Firebase 控制台的情况下发送消息。
You need to enable sound in Firebase Notification Composer under Advanced settings.您需要在高级设置下的 Firebase Notification Composer 中启用声音。 :) :)
private void sendNotification(String messageBody, Intent intent) {
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent/*.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)*/,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//<--
Bitmap bitmap = getBitmapfromUrl(postImageUrl);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_recive)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notification_recive))
.setContentTitle(postTitle + "")
.setStyle(new NotificationCompat.BigPictureStyle()
.setSummaryText(postTitle + "")
.bigPicture(bitmap))
.setContentText(messageBody)
.setLights(getResources().getColor(R.color.blue),1000,1500)
.setAutoCancel(true)
.setSound(defaultSoundUri)//<--
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
You need to set the default channel using meta-data into your Manifest.xml
like this:您需要使用元数据将默认频道设置到您的Manifest.xml
中,如下所示:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="NAME_OF_YOUR_DEFAULT_CHANNEL" />
You Already Have method onMessageReceived, firebase send remoteMessage.getData() type when application is in background.您已经拥有 onMessageReceived 方法,firebase 在应用程序处于后台时发送 remoteMessage.getData() 类型。 and remoteMessage.getNotification() will be null.并且 remoteMessage.getNotification() 将为空。 so sound is only play when app is in foreground,not playing when app is in background.所以声音只在应用程序在前台时播放,当应用程序在后台时不播放。 you need to add你需要添加
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleNotification(json.getString("msg");//I am assuming message key is msg
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.