当针对多个registration_ids时,fcm注册ID失败

Jav*_*gas 4 php android google-cloud-messaging firebase-cloud-messaging

我在php中从服务器端向几个注册ID发送通知.这是请求:

public function androidPushNotification($registration_ids, $title, $message) {
    $msg = array (
            'message' => $message,
            'title' => $title 
    );

    $fields = array (
            'registration_ids' => $registration_ids,
            'data' => $msg 
    );

    $headers = array (
            'Authorization: key=' . $this->API_ACCESS_KEY,
            'Content-Type: application/json' 
    );

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $this->GCM_URL );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode ( $fields ) );
    $result = curl_exec ( $ch );
    curl_close ( $ch );

    return $result;
}
Run Code Online (Sandbox Code Playgroud)

registration_ids变量在一个数组中有两个注册ID,其中一个来自客户端应用程序的旧安装,另一个是当前的安装.

我从fcm得到这个回复:

{
  "multicast_id": 7860323906688398625,
  "success": 1,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    },
    {
      "message_id": "0:1478735313889582%1b153de0f9fd7ecd"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我怎么知道哪个注册ID失败了?

有没有其他选择来获取此信息?

问候!

Jav*_*gas 8

我找到了答案:

结果数组的顺序与注册ID相同.例如,如果请求是:

$fields = array (
            'registration_ids' => array('123456','987654'),
            'data' => array ('message' => 'This is the message','title' => 'Hi there!')
);
Run Code Online (Sandbox Code Playgroud)

示例回复:

{
  "multicast_id": 7860323906688398625,
  "success": 1,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    },
    {
      "message_id": "0:1478735313889582%1b153de0f9fd7ecd"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

注册ID失败的是123456.

从gcm文档:

以下是6个收件人的JSON结果(分别为ID 4,8,15,16,23和42),成功处理了3条消息,返回了1个规范注册令牌,以及3个错误:

{ 
  "multicast_id": 216,
  "success": 3,
  "failure": 3,
  "canonical_ids": 1,
  "results": [
    { "message_id": "1:0408" },
    { "error": "Unavailable" },
    { "error": "InvalidRegistration" },
    { "message_id": "1:1516" },
    { "message_id": "1:2342", "registration_id": "32" },
    { "error": "NotRegistered"}
  ]
}
Run Code Online (Sandbox Code Playgroud)

在这个例子中:

  • 第一条消息:成功,不是必需的.
  • 第二条消息:应重新发送(注册令牌8).
  • 第三条消息:有一个不可恢复的错误(可能是数据库中的值已损坏).
  • 第四个信息:成功,没有必要.
  • 第五条消息:成功,但应在服务器数据库中更新注册令牌(从23到32).
  • 第六条消息:应从服务器数据库中删除注册令牌(42),因为应用程序已从
    设备中卸载.

我希望它有所帮助,问候.