【问题标题】:OnMessageRecieved not getting fired in background appOnMessageRecieved 没有在后台应用程序中被解雇
【发布时间】:2020-03-11 13:08:36
【问题描述】:

我正在尝试在单击不同的通知时重定向到不同的页面。 只有当应用程序在前台运行时,才会触发 OnMessagedRecieved。

根据文档notification foreground, background,它是被触发的系统托盘。

所以我查了一下如何让它在后台运行,我发现了这个tutorial

按照本教程:

如下所述,通过使用 FCM 控制台,您只能发送通知消息。通知消息可以由前台应用程序中的 onMessageReceived 方法处理,并在后台应用程序中传递到设备的系统托盘。用户点击通知和默认应用程序启动器将打开。如果您想在应用程序的每个状态下处理通知,您必须使用数据消息和 onMessageReceived 方法。

所以我查找了数据消息到底是什么以及通知消息是什么。 Documentation

我按照教程进行了操作,但它对我不起作用。 这是我的代码:

public async Task<bool> WakeUp(string[] tokens)
    {
        var message = new Message()
        {
            RegistrationID= tokens,
            Notification = new Notification()
            {
                Title = "testing",
                Body = "test"
            },
            Android = new AndroidConfig()
            {
                Notification = new AndroidNotification()
                {
                    Color = "#FF0000",
                    ClickAction = "MyActivity"
                }
            },
            Data = new Dictionary<string, string>()
            {
                {
                    "notificationType", NotificationType.WakeUp.ToString()
                }
            }
        };

        return await SendAsyncMessage(message).ConfigureAwait(false);
    }


public async Task<bool> SendAsyncMessage(Message message)
    {
        var jsonMessage = JsonConvert.SerializeObject(message);
        var request = new HttpRequestMessage(HttpMethod, FIREBASE_PUSH_NOTIFICATION_URL)
        {
            Content = new StringContent(jsonMessage, Encoding.UTF8, "application/json")
        };
        request.Headers.TryAddWithoutValidation("Authorization", $"key={ConfigurationManager.AppSettings["FirebaseNotificationServerKey"]}");
        request.Headers.TryAddWithoutValidation("Sender", $"id={ConfigurationManager.AppSettings["FirebaseNotificationSenderID"]}");
        HttpResponseMessage result;
        using (var client = new HttpClient())
        {
            result = await client.SendAsync(request).ConfigureAwait(false);
        }

        return result.IsSuccessStatusCode;
    }

这就是我在应用程序中接收代码的方式

public override void OnMessageReceived(RemoteMessage message)
    {
       Console.WriteLine("message recieved");
    }

原始 Json

{
"registration_ids":["myToken"],
"condition":null,
"data":
{
    "notificationType":"WakeUp"
},
"notification":
{
    "title":"testing",
    "body":"test",
    "image":null
},
"android":
{
    "collapse_key":null,
    "restricted_package_name":null,
    "data":null,
    "notification":
    {
        "title":null,
        "body":null,
        "icon":null,
        "color":"#FF0000",
        "sound":null,
        "tag":null,
        "image":null,
        "click_action":"mActivity",
        "title_loc_key":null,
        "title_loc_args":null,
        "body_loc_key":null,
        "body_loc_args":null,
        "channel_id":null
    },
    "fcm_options":null,
    "priority":"high",
    "ttl":null
},
"webpush":null,
"apns":null,
"fcm_options":null,
"topic":null

}

收到通知但未触发 OnMessageRecieved。我认为通知中心负责显示通知。

可能是什么问题?

【问题讨论】:

  • 原始 json 消息是什么样子的
  • @tyczj 我编辑了页面。忘了那个抱歉
  • 如您所见,"data":null, 为空,它永远不会被发送到后台
  • @tyczj 该数据是空的,但上面的数据不是。标题和正文也在一般通知中,因为我填写了数据,所以我假设 android 也可以从那里读取它。我可能错了
  • @tyczj 我将代码更改为也将数据放入空数据属性中。但是它仍然没有触发

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


【解决方案1】:

1。为什么会这样?

FCM(Firebase Cloud Messaging)中有两种类型的消息:

  1. 显示消息:这些消息仅在您的应用处于前台时触发onMessageReceived()回调
  2. 数据消息:这些消息会触发onMessageReceived() 回调即使如果您的应用处于前台/后台/已终止

注意: Firebase 团队尚未开发 UI 以将 data-messages 发送至 你的设备,但是。您应该使用您的服务器发送此类型!



2。怎么做?

为此,您必须对以下 URL 执行 POST 请求:

POSThttps://fcm.googleapis.com/fcm/send

标题

  • 键: Content-Type值: application/json
  • 键: Authorization值: key=&lt;your-server-key&gt;

正文使用主题

{
    "to": "/topics/my_topic",
    "data": {
        "my_custom_key": "my_custom_value",
        "my_custom_key2": true
     }
}

或者如果您想将其发送到特定设备

{
    "data": {
        "my_custom_key": "my_custom_value",
        "my_custom_key2": true
     },
    "registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
}


注意:确保您不添加 JSON 密钥notification
注意:要获取您的服务器密钥,您可以在 Firebase 控制台中找到它:Your project -&gt; settings -&gt; Project settings -&gt; Cloud messaging -&gt; Server Key

3。推送通知消息如何处理?

这是您处理收到的消息的方式:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map<String, String> data = remoteMessage.getData();
     String myCustomKey = data.get("my_custom_key");

     // Manage data
}

这是指here

【讨论】:

    猜你喜欢
    • 2020-03-28
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多