3

我發送通知給幾個註冊ID從服務器端在PHP中。 這是請求:很多fcm註冊ID失敗時,針對多個registration_ids

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的失敗?

是否有另一種選擇獲取此信息?

問候!

回答

6

我已經找到答案:

結果陣列處於相同的順序的註冊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" 
    } 
    ] 
} 

註冊ID失敗是123456之一。

從GCM文檔:

下面是JSON結果6頁的收件人(IDS 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"} 
    ] 
} 

在這個例子中:

  • 首先消息:成功,不是必需的。
  • 第二條消息:應該重新發送(註冊令牌8)。
  • 第三條消息:發生了不可恢復的錯誤(可能是數據庫中損壞的值)。
  • 第四條消息:成功,沒有要求。
  • 第五條消息:成功,但註冊令牌應在服務器數據庫中更新(從23到32)。
  • 第六條消息:應從服務器數據庫中刪除註冊令牌(42),因爲應用程序已從設備上卸載。

我希望它有幫助,問候。