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

San*_*dav 12 android push-notification android-notifications firebase firebase-notifications

我在下面的代码中使用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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我正在调用这个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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

kam*_*iha 12

由于在发送通知对象时未调用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();
    }
}
Run Code Online (Sandbox Code Playgroud)

并将其添加到清单中.接收方负责播放通知铃声.

 <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>
Run Code Online (Sandbox Code Playgroud)


AL.*_*AL. 6

通过Firebase控制台在Android中发送通知时,它将被视为通知消息.当应用程序处于后台时,Android设备(系统托盘)将始终自动处理通知消息(请参阅处理消息).

这意味着onMessageReceived()不会被召唤.因此,如果您打算在收到通知时始终播放声音,则必须使用数据消息*.但是,您必须在不使用Firebase控制台的情况下发送邮件.


Him*_*ami -1

您已经有了 onMessageReceived 方法,当应用程序处于后台时,firebase 会发送 remoteMessage.getData() 类型。并且remoteMessage.getNotification()将为null。因此,声音仅在应用程序位于前台时播放,而当应用程序位于后台时不播放。你需要添加

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());
        }
    }
Run Code Online (Sandbox Code Playgroud)