Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android推送通知声音仅在应用处于前景时播放,但在应用处于后台时不播放声音大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用FCM进行推送通知
下面的代码在收到通知时发出声音
public void playNotificationSound() {
        try {

            Uri notification = @L_821_3@manager.getDefaultUri(@L_821_3@manager.TYPE_NOTIFICATION);
            ringtone r = @L_821_3@manager.getringtone(mCo@R_801_10443@t,notification);
            r.play();

        } catch (Exception E) {
            e.printStackTrace();
        }
    }

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

@Override
    public void onmessageReceived(Remotemessage remotemessagE) {
        Log.e@R_675_10374@,"From: " + remotemessage.getFrom());

        if (remotemessage == null)
            return;

        // check if message contains a notification payload.
        if (remotemessage.getNotification() != null) {
            Log.e@R_675_10374@,"Notification Body: " + remotemessage.getNotification().getBody());
            handleNotification(remotemessage.getNotification().getBody());
        }

        // check if message contains a data payload.
        if (remotemessage.getData().size() > 0) {
            Log.e@R_675_10374@,"Data Payload: " + remotemessage.getData().toString());

            try {
                JSONObject json = new JSONObject(remotemessage.getData().toString());
                handleDatamessage(json);
            } catch (Exception E) {
                Log.e@R_675_10374@,"Exception: " + e.getmessage());
            }
        }
    }





 private void handleNotification(String messagE) {
        if (!NotificationUtils.isAppIsInBACkground(getApplicationCo@R_801_10443@t())) {
            // app is in foreground,broadcast the push message
            Intent pushNotification = new Intent(con@L_262_12@.PUSH_NOTIFICATION);
            pushNotification.putExtra("message",messagE);
            LocalBroadcastManager.geTinstance(this).sendBroadcast(pushNotification);

            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationCo@R_801_10443@t());
            notificationUtils.playNotificationSound();
        }else if (NotificationUtils.isAppIsInBACkground(getApplicationCo@R_801_10443@t())){
            // If the app is in BACkground,firebase itself handles the notification
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationCo@R_801_10443@t());
            notificationUtils.playNotificationSound();
        }
    }

解决方法

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

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

大佬总结

以上是大佬教程为你收集整理的Android推送通知声音仅在应用处于前景时播放,但在应用处于后台时不播放声音全部内容,希望文章能够帮你解决Android推送通知声音仅在应用处于前景时播放,但在应用处于后台时不播放声音所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。