当前位置: 首页 > 知识库问答 >
问题:

Android推送通知声音仅在应用程序位于前台时播放,而在应用程序位于后台时不播放

姬高澹
2023-03-14

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

共有3个答案

浦德义
2023-03-14

您需要在“高级设置”下的Firebase Notification Composer中启用声音。:)

胥诚
2023-03-14

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

这意味着不会调用onMessageReceived()。因此,如果您希望在收到通知时始终播放声音,则必须使用数据消息*。但您必须在不使用Firebase控制台的情况下发送消息。

苏丰茂
2023-03-14

由于发送通知对象时不调用onMessageReception(),因此我创建了一个BroadcastRecencer来在通知到达时处理它:

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

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

 <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>
 类似资料:
  • 我在我的react native应用程序中使用react native push notification来接收远程推送通知。一切正常,当应用程序处于前台或后台时,我会收到所有通知。 但是我希望通知仅在应用程序在后台时显示。如果应用程序在前台,它不应该显示通知。 任何帮助都将不胜感激。 我的清单文件包含以下代码:

  • 当应用程序在前台时不接收推送通知,但当应用程序在后台时接收我已遵循来自https://medium . com/@ ankushaggarwal/GCM-setup-for-Android-push-notifications-656 cf DD 8 adbd的FCM教程

  • 问题内容: 我知道关于SOF的问题很多,但这是“最新的”问题。事实是,我正在尝试创建和警报应用程序,并且我知道有一个警报应用程序可以很好地运行(某种程度上),即使该应用程序未运行且在后台。 我的问题是:你是怎么开始播放声音 后, 你的应用程序已经在后台? UILocalNotification很棒,但是只有在用户单击通知后您才能获得,因此使用AVAudioPlayer播放声音不起作用,无论用户单击

  • 这是苹果公司设计的吗?或者我错过了一些我可以申请的设置?

  • 我想为一个聊天应用程序实现FCM推送通知服务,我遵循Firebase文档中的步骤,当通知从Firebase控制台发送到我的设备时,我会得到通知。当我尝试使用http post to https://FCM.googleapis.com/FCM/send通过FCM将通知通过服务器端发送到设备时: 当应用程序处于活动状态并且我正在将此通知发送到我的设备时,Im在我的控制台日志中收到以下消息,所以我认为

  • 当应用程序在后台时,如何更新包含数据负载的Firebase推送通知?有没有办法在通知中指定通知id给Firebase API? 完整的项目在github https://github.com/akshatashan/FireBaseCloudMessagingDemo中