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

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

概述我正在使用FCM进行推送通知 下面的代码在收到通知时发出声音 public void playNotificationSound() { try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Rington 我正在使用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();        }    }
解决方法 通过Firebase控制台在AndroID中发送通知时,它将被视为通知消息.当应用程序处于后台时,AndroID设备(系统托盘)将始终自动处理通知消息(请参阅 Handling messages).

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

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1137467.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存