[英]How to play an alarm sound on Android via Push Notif regardless of Device locked or App killed without Foreground Service/Alarm Manager?
I am building an emergency security Android App.我正在构建一个紧急安全 Android 应用程序。 One of the important requirements is to play an Alarm sound from a push notification.
其中一项重要要求是从推送通知播放警报声音。 This happens very rarely so most likely the App won't be running (maybe force stopped) and device might be locked or in sleep mode.
这种情况很少发生,所以很可能应用程序不会运行(可能强制停止)并且设备可能被锁定或处于睡眠模式。
I am using FCM to send a push notification like this:我正在使用 FCM 发送这样的推送通知:
public class NotificationManager extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
playAlarmSound();
}
}
And my Service looks like this:我的服务看起来像这样:
<service
android:name=".notification.NotificationManager"
android:directBootAware="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
However, I only receive the notification if the Device is not locked.但是,如果设备未锁定,我只会收到通知。 How can ensure that happens?
如何确保发生这种情况?
I have seen some posts to use an Alarm Manager & Foreground Services - but I don't want to be constantly running and draining battery for an infrequent (but extremely important) usecase?我看过一些使用警报管理器和前台服务的帖子 - 但我不想因为不常见(但非常重要)的用例而不断运行和耗尽电池电量?
I would recommend using custom sound so you can play the alarm sound.我建议使用自定义声音,这样您就可以播放闹钟声音。 Set it to high priority so FCM will attempt to deliver it immediately even if the device is in Doze mode.
将其设置为高优先级,这样即使设备处于打盹模式,FCM 也会尝试立即传送它。
However, if you still consider using Alarm Manager, you can check out these posts about battery usage:但是,如果您仍然考虑使用 Alarm Manager,您可以查看这些关于电池使用情况的帖子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.