- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在 Android 应用程序中使用 Firebase 作为消息服务。我对 Firebase 进行了相当多的研究,我明白 whether the app if running in the foreground or now will change the type of data received in the onMessageReceived method .
我想要完成的是解析来自 Remotemessage 的传入数据,并根据自定义标记对其执行不同的操作。 IE,如果数据映射包含一个名为“My_Custom_Tag”的字段,我想完全覆盖弹出的标准 firebase 消息并编写自定义消息。
问题是,当应用程序在后台运行时,我放入 onMessageReceived never 中的任何代码都会被触发。当应用程序在前台运行时它工作得很好,但如果应用程序在后台运行则不会检测/接收任何东西。
下面是一些示例代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map<String, String> aMap = remoteMessage.getData();
//Custom object here for easier parsing
PushNotificationsPojo pojo = this.convertResponseToPojo(aMap);
if(pojo == null){
passToFirebase(remoteMessage);
return;
}
if(pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_CHAT_MESSAGE)) {
createCustomNotification1(pojo);
} else if (pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_SOME_OTHER_MESSAGE)){
createCustomNotification2(pojo);
} else if (pojo.getData().getCustomTag().equals(PushNotificationsPojo.ID_TAG_STUFF_AND_WHATNOT)) {
createCustomNotification3(pojo);
} else {
passToFirebase(remoteMessage);
}
}
我的问题是,我该如何编辑 MyFirebaseMessagingService 以便我可以检查传入数据、确定标记信息,并决定将其传递给 firebase 以便它可以使用标准处理或不将其传递给 firebase并在我的应用程序在后台运行时编写我自己的自定义显示通知?
谢谢大家!
PS,有机会Firebase-Dungeon-Master Frank van Puffelen有想法吗?
最佳答案
从您的 FCM 消息中删除通知负载,以便将数据负载传送到 onMessageReceived 方法。
当您的应用程序处于后台时,数据有效载荷传送到 onMessageReceived 方法仅当没有通知有效载荷时。 (标记单词)
如果两个有效负载都存在,则系统会自动处理通知部分(系统托盘),您的应用程序会在启动器 Activity 的额外内容中获取数据有效负载(在用户点击通知后)。
为了能够成功地为 Android 和 iOS 这两个平台提供服务,您可能必须根据客户端的操作系统发送不同的 FCM 消息。
关于android - 应用程序在后台时不会调用 Firebase onMessageReceived,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40311279/
我创建了一个应用程序,当收到来自 FCM 的消息时,该应用程序会启动警报。该应用程序在前台、后台或 Activity 关闭时都可以正常工作。 但是当手机长时间处于 sleep 模式时(不知 Prop
这是我设置工具栏 (MainActivity) 的方法。 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuIn
我正在关注 this在我的 Android 设备上接收推送通知的教程。 我可以从 Firebase 控制台发送推送通知 - 我也可以在 logcat 中看到 Firebase 事件,但我的 onMes
首先,这不是因为应用程序在后台。 通知与数据消息负载一起发送。在 Play 控制台中,它表示消息已“已确认”,因此它们正在到达设备。对于大多数用户,onMessageReceived 方法被调用,但对
我正在使用以下代码接收通过 firebase 控制台发送的 fcm 消息,但从未调用此函数。相反,当应用程序处于后台时,我能够在我的启动器类中接收消息,但是当应用程序处于前台时,我无法获取它。 pub
这个问题在这里已经有了答案: Does FirebaseMessagingService run in the background by default? (2 个答案) 关闭 4 年前。 Fir
我正在使用适用于 Android 的 Firebase 云消息传递。当我的应用程序位于前台时,FCM 将在我的应用程序的 FirebaseMessagingService 子类上调用 onMessag
我实现了Huawei Push Kit。 onNewToken 在应用启动后调用。我想使用 onMessageReceived。 当我向客户端发送推送通知时,推送通知会出现在 android 上,但不
我使用 WSO2 作为我的身份提供者 (IDP)。它将 JWT 放在名为“X-JWT-Assertion”的 header 中。 为了将它输入 ASP.NET Core 系统,我添加了一个 OnMes
我刚刚开始使用 Firebase Cloud Messaging,并在 Manifest 中设置了所有依赖项、Firebase 服务及其服务标签。 从控制台发送消息时,会检测到目标用户,但应用程序不会
我正在尝试“捕获”发送到应用程序的 firebase 云消息,当我收到(/单击)一条消息时,我想根据在 firebase 控制台中添加的“自定义数据”键/值执行一些操作。 我的代码现在是这样的: My
我正在尝试实现 GCM。一切正常,除了 onMessageReceived() 方法仅在应用程序运行时被调用。 我搜索了解决方案,但找不到与我相匹配的案例。我的 GCM 请求正文仅包含“to”和“da
我有以下 list - 并遵循 on
我正在尝试从服务器向我的应用程序获取消息。我有这个服务器 url,它向所有用户发送消息: https://taub-prayer-ramym.c9.io/send?message=123 我的应用程序
实际上我需要在应用程序图标中显示徽章,但我不知道如何获取徽章以及为什么当应用程序处于后台或应用程序未运行时 onMessageReceived 不调用。 public class Custom_Fir
我已经清楚地阅读了很多关于 firebase 推送通知行为的文档。当我的应用程序处于后台时,我可以在系统托盘上看到通知,但当应用程序处于前台时,我不会触发 onMessageReceived 回调。我
当应用程序通过点击通知处于前台或后台时,应用程序调用 onMessageReceived 事件。我使用 click_action 通知。对吗? 我在应用程序位于前台时创建一个通知,当我点击该通知时它会
我正在尝试在收到 fcm 消息且 APP 处于后台时显示通知。我尝试了不同风格的通知,但没有运气。通知不会显示 这是代码 public void onMessageReceived(RemoteMes
我正在制作必须显示推送通知的应用程序。我可以正确注册 token 并在 FirebaseMessageService remoteMessage 中获取数据。但它没有显示通知。我想检查类型。如果 ty
正如您在标题中所写,当应用程序关闭时它运行良好并且 onMessageReceived 获取消息正文和标题但是如果应用程序处于前台模式(运行模式)则通知可以发送但没有消息和标题! 请问怎么办? 代码:
我是一名优秀的程序员,十分优秀!