仅当应用程序处于前台时才播放 Android 推送通知声音,但当应用程序处于后台时不播放声音

标签 android firebase push-notification android-notifications 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();
        }
    }

我正在调用此 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();
        }
    }

最佳答案

由于 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();
    }
}

并将其添加到 list 中。接收方负责播放通知铃声。

 <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>

关于仅当应用程序处于前台时才播放 Android 推送通知声音,但当应用程序处于后台时不播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41562915/

相关文章:

node.js - 未收到 Firebase 云消息传递收据

push-notification - 设置 webhook 结果为 'Unauthorized WebHook callback channel' 。一切都应该没问题

Android EditText 显示带有大写字母和数字行的键盘

java - 为什么我的 android openGL ES 测试渲染器崩溃

ios - 打印出 firebase 子值

ios - DidReceiveRemoteNotification 从未在 Xamarin.iOS 中调用

email - 发送包含许多大附件的电子邮件时,Gmail API 推送通知过多

android.database.sqlite.SQLiteException : near "": syntax error

android - 使用共享首选项存储 int 值

java - 参数 1 的类型为 java.util.TreeMap,得到 java.util.HashMap