2017-08-25 89 views
1

我試圖發送推送通知到多個設備使用谷歌Firebase和我總是收到錯誤:「InvalidRegistration」。無效的註冊發送推送到多個設備

這是我在「到」

:json_encode($tokensPerEvent): 
["eV9g4oTwjZs:APA91bF3YLGtDkCDekvR6eahbVAn-jIY0sVGjxMWyBEyR- 
    3AB9q6RBhw4fyeqE4ZkZxQs0TsYhUee9Txy_exAGxtrBPV_- 
    sjKlWcV3z3nDYXOcVSVwlpPyGzUJKxGMU16drMR41bLI4t"] 

發出的令牌,這是響應:

{ 
    "multicast_id":***, 
    "success":0, 
    "failure":1, 
    "canonical_ids":0, 
    "results":[ 
     { 
     "error":"InvalidRegistration" 
     } 
    ] 
} 

另一個問題:如果令牌的一個不存在了,這對所有其他代幣有影響,或者只有唯一的舊代碼會影響?

這是我的代碼:

<?php 
    require_once '../CommonFunctions.php'; 

    ignore_user_abort(); 
    ob_start(); 

    $url = 'https://fcm.googleapis.com/fcm/send'; 

    //GET TOKENS FROM DB 
    $db = new Database(); 
    $db->query("SELECT push_token FROM User"); 

    $db1 = new Database(); 
    $db1->query("SELECT phone FROM invite_list where event_id = 137"); 

    $response = $db->resultset(); 
    $response1 = $db1->resultset(); 

    $arr2 = array_column($response1, 'phone'); 
    $phones = join("','",$arr2); 

    $db2 = new Database(); 
    $db2->query("SELECT push_token FROM User WHERE phone IN ('$phones')"); 

    $tokensPerEvent = array(); 
    $tokensrr = $db2->resultset(); 
    $tokensPerEvent = array_column($tokensrr, 'push_token'); 

    echo json_encode($tokensPerEvent);  

    $fields = array('to' => json_encode($tokensPerEvent), 
    'notification' => array('body' => 'HI', 'title' => ':)')); 


    define('GOOGLE_API_KEY', '***********'); 

    $headers = array(
      'Authorization:key='.GOOGLE_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_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

    $result = curl_exec($ch); 
    if($result === false) 
    die('Curl failed ' . curl_error()); 
    curl_close($ch); 
    return $result; 
?> 

回答

1

如果你想通過他們的註冊ID來推送通知發送給很多客戶,你必須使用registration_ids,而不是領域。你可以在docs(第二個參數)中找到它。


您還編碼標記兩次:

$fields = array('to' => json_encode($tokensPerEvent), ...

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

嘗試通過在陣列跳過編碼$tokensPerEvent$fields = array('to' => $tokensPerEvent, ...

+0

當我這樣做時,我收到 - >「字段」爲「必須是JSON字符串:****** – Yakir

+0

@Yakir Okey ...您想通過他們的註冊ID向許多客戶端發送推送通知? 然後,你必須使用'registration_ids'而不是'to' 你可以在文檔中找到它...我將盡力找到這個,但我很確定它是這樣的。編輯:Okey,我找到了。第二個參數https://firebase.google.com/docs/cloud-messaging/http-server-ref –

+0

你救了我一天!感謝男人! – Yakir

相關問題