0

我们的数据库中有大约 900 个用于 GCM 推送通知的 Android 设备令牌。

我使用这个 PHP 函数将推送指令发送到 GCM 服务器:

function send_android_push($android_gcm_reg_ids, $title, $message, $activityToLaunch) {

    global $ANDROID_GCM_API_KEY, $TESTING, $TEST_ANDROID_DEVICE_REG_ID;

    if ($TESTING) {
        $android_gcm_reg_ids = [
            $TEST_ANDROID_DEVICE_REG_ID
        ];
    }

    $url = "https://android.googleapis.com/gcm/send";

    $data = array(
        "title" => $title,
        "message" => $message,
        "activityToLaunch" => $activityToLaunch,
    );

    $post = array(
        'registration_ids' => $android_gcm_reg_ids,
        'data' => $data
    );

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

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        $result .= "<br />GCM error: " . curl_error($ch);
    }

    curl_close($ch);

    return $result;
}

以下是该函数/GCM 返回的 JSON 内容片段:

{
   "multicast_id":5609284883833846123,
   "success":493,
   "failure":401,
   "canonical_ids":20,
   "results":[
      {
         "registration_id":"BPA91bHP29l2j6NPOldcpAvJCJBGk8oQFErfwuh93cxF8ajJJqBlVGqFcxMDEAc2LL2GKwmP86of49UgBTMycB5IdYergWRBETDNUrnzXX_55FgstCZiauPeD7MokIIPOFoOyW9vCRiBqZhlzLCuSnJ1ENFYtIh_PQ",
         "message_id":"0:1462275569015808%814cce86f9fd7ece"
      },
      {
         "registration_id":"BPA91bHP29l2j6NPOldcpAvJCJBGk8oQFErfwuh93cxF8ajJJqBlVGqFcxMDEAc2LL2GKwmP86of49UgBTMycB5IdYergWRBETDNUrnzXX_55FgstCZiauPeD7MokIIPOFoOyW9vCRiBqZhlzLCuSnJ1ENFYtIh_PQ",
         "message_id":"0:1462275569015815%814cce86f9fd7ece"
      },
      {
         "message_id":"0:1462275569014983%814cce86f9fd7ece"
      },
      {
         "error":"NotRegistered"
      },
      {
         "message_id":"0:1462275569017976%814cce86f9fd7ece"
      },
etc., etc.

似乎是说我的数据库中有设备令牌,NotRegistered但是正如您所见,它没有告诉我哪个设备令牌未注册,所以我想知道如何在我的数据库中将其标记为过时?

I thought that maybe the elements of the JSON array returned by the GCM server might correspond to the $android_gcm_reg_idsarray I pass into my send_android_push(...)function but that can't be the case because the BPA91bHP29l2j6NPOldcpAvJCJBGk8oQFErfwuh93cxF8ajJJqBlVGqFcxMDEAc2LL2GKwmP86of49UgBTMycB5IdYergWRBETDNUrnzXX_55FgstCZiauPeD7MokIIPOFoOyW9vCRiBqZhlzLCuSnJ1ENFYtIh_PQ registration_id appears numerous times in the GCM JSON array - despite that same token only在我的数据库中出现一次。

所以大概必须有其他方法从我的数据库中删除过时的设备令牌?

我担心这一点的原因是因为我之前遇到过这种情况(使用 iOS),发送错误的设备令牌(到 APNS)导致推送通知没有发送到有效设备,我显然想保留我的数据库反正状态不错。

我在网上找不到任何可以帮助的东西,所以也许我遗漏了一些明显的东西或弄错了?

4

1 回答 1

2

要知道是哪个注册令牌导致了这种情况,您必须映射到registration_ids您对 GCM 的请求数组中的相同索引。

这里来自文档

结果:表示已处理消息状态的对象数组。对象以与请求相同的顺序列出(即,对于请求中的每个注册 ID,其结果都列在响应中的相同索引中)。

于 2016-05-04T13:31:28.260 回答