【问题标题】:Send push notification to iOS device via Firebase (FCM) using HTTP Request after app is killed [duplicate]在应用程序被杀死后,使用 HTTP 请求通过 Firebase(FCM)向 iOS 设备发送推送通知 [重复]
【发布时间】:2016-11-04 08:41:17
【问题描述】:

在应用被终止后,我无法通过 HTTP 请求通过 Firebase 向我的 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 设备发送推送通知。有谁知道我将如何解决这个问题?

【问题讨论】:

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


    【解决方案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",
        };
    

    即使应用程序被终止,这也会让您的推送通知传递。

    【讨论】:

    • 这确实是一个常见的错误:如果不将消息作为高优先级发送,它很可能(但不能保证)在很晚之后被丢弃或传递。见stackoverflow.com/questions/37332415/…
    • 收到推送通知后如何执行一些代码。喜欢通过 FCM 或 APNS 收到被杀 ios 应用的推送通知后用一些数据回复服务器?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 2017-06-07
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 2017-12-02
    • 2020-03-29
    相关资源
    最近更新 更多