简体   繁体   English

如何在没有前台服务/警报管理器的情况下通过 Push Notif 在 Android 上播放警报声音,而不管设备已锁定或应用程序已被杀死?

[英]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.

相关问题 如何通过 aws-cdk 将自定义指标和警报添加到 AWS Lambda 函数 - How to add custom Metric and Alarm to AWS Lambda function via aws-cdk firebase 推送通知声音在 android 工作室中的 android 应用程序上不起作用 - firebase push notification sound not working on android app in android studio 如何将 cloudwatch 警报连接到 lambda 函数 - how to connect a cloudwatch alarm to a lambda function 如何在 AWS 中搜索引发指标警报的日志? - How to search for a logs that raised metric alarm in AWS? 如何用声音推送云消息? - How To push Cloud Messaging With Sound? 如何将 CloudWatch 警报配置为每 X 分钟评估一次 - How to configure a CloudWatch alarm to evaluate once every X minutes 应用程序被终止时,设备未收到推送通知 - push notification not received to devices when app is killed 如何对违反特定阈值的 EventBridge 规则数量创建警报? - How to create alarm on number of EventBridge Rules breaching a certain threshold? 如何使用 CW 警报和 SNS 在邮件中获取 Lambda 错误? - How to get Lambda Error in Mail using CW Alarm and SNS? 延迟 AWS Cloudwatch 警报状态更改 - Delay in AWS Cloudwatch Alarm state change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM