【问题标题】:Does FCM support the new apns-push-type for iOS 13FCM 是否支持 iOS 13 的新 apns-push-type
【发布时间】:2020-01-31 07:00:17
【问题描述】:

我们目前有一个解决方案,可以将推送通知从 FCM 发送到 APNS,然后再发送到 iOS。由于 iOS13 的引入,APNS 现在在任何传入的有效负载中都需要 apns-push-type 来指定它是警报通知、后台通知还是任何其他类型。我想知道如何在发送到 FCM 的消息中添加此信息。

目前我们使用 pyFCM 向 FCM 发送消息。我们按照此页面作为参考:https://firebase.google.com/docs/cloud-messaging/http-server-ref

from pyfcm import FCMNotification
push_service = FCMNotification(api_key="XXXX")
registration_id = '<Token>'
data_message = {
    "Score": "3*1",
    "DeviceId": "XXXXXX",
}

# Background notification
result = push_service.notify_single_device(registration_id=registration_id,
                                       content_available=True,
                                       data_message=data_message)

# Alert notification
result = push_service.notify_single_device(registration_id=registration_id,
                                       message_title='Sample title',
                                       message_body='Sample body',
                                       data_message=data_message,
                                       )

这适用于现有的 iOS 应用程序。但是对于 iOS 13,我找不到任何地方来指定 apns-push-type 或 FCM 将转换为将发送到 APNS 的 apns-push-type 的任何等效字段。

我知道 iOS 13 相对较新,所以每个人仍在努力调整现有解决方案以适应它。希望有人能给我一些见解,如何将 apns-push-type 放入我现有的解决方案中。谢谢。

【问题讨论】:

    标签: firebase-cloud-messaging ios13


    【解决方案1】:

    我们的解决方案是根据请求将其添加到标题中(此答案在 PHP 代码上)

    $headers = [
                'Authorization: key=' . $serverKey,
                'Content-Type: application/json',
                'apns-push-type: background'
              ];
    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $fcmEndpoint); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, CURL_IPRESOLVE_V4); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payloads)); 
    $result = json_decode(curl_exec($ch), true); curl_close($ch);
    

    【讨论】:

      【解决方案2】:

      您可以检查推送通知是否有效 json api post request 到 https://fcm.googleapis.com/fcm/send url。 在配置标头中 Content-Type : application/jsonAuthorization:key=&lt;Your FCm server key&gt;

      然后在请求正文中添加这些

      { "to" : "device_token",
       "notification" : 
       { 
          "title": "message title!", 
           "body": "MESSAGE BODY",
      
       "token": "XXXXXXXXXX", 
       "id": 1959,
            "sound": "default"
                                            },
      
       "apns": {
          "headers": { 
            "apns-push-type": "alert"
          }
      
        }
      }
      

      然后您可以检查它是否有效。我的项目以前在更新 IOS 13 之前工作。更新后,通知在后台不起作用, 添加

      "apns": {
          "headers": { 
            "apns-push-type": "alert"
          }
      
        }
      

      项目使接收通知成为可能

      【讨论】:

        【解决方案3】:

        您可以使用通知的“extra_kwargs”添加此选项。

        为后台通知添加 extra_kwargs={"apns_push_type":"background"}

        # Background notification 
        result = push_service.notify_single_device(registration_id=registration_id,
                                                   content_available=True,
                                                   data_message=data_message,
                                                   low_priority=True,
                                                   extra_kwargs={"apns_push_type": "background"})
        

        另外,将后台通知的优先级标记为低。这是通过将 low_priority 发送为 true 来完成的。

        对于警报通知,我们需要将 apns 推送类型发送为“警报”

         # Alert notification
        result = push_service.notify_single_device(registration_id=registration_id,
                                           message_title='Sample title',
                                           message_body='Sample body',
                                           data_message=data_message,
                                           extra_kwargs={"apns_push_type": "alert"}
                                           )
        

        【讨论】:

          【解决方案4】:

          只需在要发送到 FCM 的请求标头中添加 'apns-push-type' = 'XXX'

          【讨论】:

            猜你喜欢
            • 2021-05-21
            • 2019-12-25
            • 2021-12-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-25
            • 1970-01-01
            相关资源
            最近更新 更多