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


Which fcm registration id has failed when targeted for multiple registration_ids

我从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;
}

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

我从fcm得到这个响应:

{
  "multicast_id": 7860323906688398625,
  "success": 1,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    },
    {
      "message_id": "0:1478735313889582%1b153de0f9fd7ecd"
    }
  ]
}

我如何知道哪个注册id失败了?

是否有其他选项来获取此信息?

我找到了答案:

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

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

示例响应:

{
  "multicast_id": 7860323906688398625,
  "success": 1,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    },
    {
      "message_id": "0:1478735313889582%1b153de0f9fd7ecd"
    }
  ]
}

注册号为123456,注册失败。

来自gcm文档:

以下是6个收件人(id 4、8、15、16、23和42)的JSON结果分别),其中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"}
  ]
}

在这个例子中:

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

我希望它帮助,问候。