应用程序被杀后,使用HTTP请求通过Firebase(FCM)向iOS设备发送推送通知

时间:2016-11-04 08:41:17

标签: ios firebase push-notification apple-push-notifications firebase-cloud-messaging

在应用程序被杀后,我无法通过Firebase通过HTTP请求向我的iOS设备发送推送通知。当应用程序处于前台或在后台处于活动状态时,一切都按预期工作。但如果我杀了应用程序它将无法正常工作。如果应用程序被杀,我可以通过Firebase控制台向我的应用程序发送通知,因此我认为我使用的代码一定有问题。

这是我发送推送通知的代码:

    private void SendPushNotification(string devicetoken, string header, string content, string pushdescription)
    {
        var textNotification = new
        {
            to = devicetoken,
            notification = new
            {
                title = header,
                text = content,
                content_available = true,
                sound = "enabled",
                priority = "high",
                id = pushdescription,
            },
            project_id = "rrp-mobile",
        };

        var senderId = "212579566459";
        var notificationJson = Newtonsoft.Json.JsonConvert.SerializeObject(textNotification);
        using (var client = new WebClient())
        {
            client.Encoding = Encoding.UTF8;
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            client.Headers[HttpRequestHeader.Authorization] = "key=AIfrSyAtgsWCMH4s_bOyj-Us4CrdsifHv-GqElg";
            client.Headers["Sender"] = $"id={senderId}";
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            client.UploadString("https://fcm.googleapis.com/fcm/send", "POST", notificationJson);
        }
    }

我忘记了什么吗?这适用于在前台,后台以及应用程序被杀时向Android设备发送推送通知,就像我在前台和后台对iOS设备所说的那样。

唯一的问题是在应用程序被杀死时向iOS设备发送推送通知。有没有人知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我刚刚意识到自己的错误,而且非常简单。我在这里张贴这个,因为我相信这可能是一件容易错过的事情。

    var textNotification = new
    {
        to = devicetoken,
        notification = new
        {
            title = header,
            text = content,
            content_available = true,
            sound = "enabled",
            **priority = "high",**
            id = pushdescription,
        },
        project_id = "rrp-mobile",
    };

您需要确保在“通知”范围之外定义优先级属性,如下所示:

    var textNotification = new
    {
        to = devicetoken,
      **priority = "high",**
        notification = new
        {
            title = header,
            text = content,
            content_available = true,
            sound = "enabled",
            id = pushdescription,
        },
        project_id = "rrp-mobile",
    };

即使应用程序被杀,这也会使您的推送通知得以传递。

相关问题