【问题标题】:Firebase is sending notifications when notification are disabled from the app. How to handle this?当应用程序禁用通知时,Firebase 会发送通知。如何处理?
【发布时间】:2017-01-13 08:05:59
【问题描述】:

我在我的 Android 应用中使用 Firebase 推送通知。但是当应用程序在后台时它会发送通知。

我应该如何处理,以便在应用内部禁用通知时 Firebase 不会发送通知?

【问题讨论】:

  • 嗨。您究竟是如何禁用应用中的推送通知?你是如何发送通知的?请为您的场景提供相关代码 sn-ps 和更多详细信息。

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

每当您使用通知有效负载时,您的应用将始终在后台显示通知。因此,您可能只想使用数据有效负载,如果您从控制台发送消息,这是不可能的。因此,您需要拥有自己的 Web 服务器才能将仅包含 data-payload 密钥的消息发送到 fcm 服务器。完成此操作后,您需要检查您的应用程序是在前台还是后台,因为如果您独立使用数据有效负载,您的onMessageReceived() 将始终触发。这是link 向您展示如何检查您的应用程序是在前台还是后台。

这是关于您的问题的部分答案。如果您将指示用户是否禁用通知的数据存储在 sharePereference 或数据库中,您需要获取该值并将其放入您的 onMessageReceived() 中的 if 语句中

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if(appIsNotInForeGround){ //you will not want to show a notification if your app is running.
        if(!isMuteNotificaition){ // Check wether your user mute the app
            sendNotification(); //your custom notification builder.
        }
    }
}

参考我刚刚向您展示的链接appIsNotInForeGround

【讨论】:

  • 实际上我的应用中有一个通知按钮,我只想在用户打开通知时发送通知。
猜你喜欢
  • 2016-11-21
  • 1970-01-01
  • 2017-07-30
  • 2021-06-30
  • 2017-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多