【问题标题】:InvalidRegistration on sending push to muliple devices向多个设备发送推送时的 InvalidRegistration
【发布时间】:2017-08-25 09:37:35
【问题描述】:

我正在尝试使用 Google Firebase 向多台设备发送推送通知,但我总是收到错误消息:“InvalidRegistration”。

这是我在“to”中发送的令牌

 :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;
?>

【问题讨论】:

    标签: php android firebase push-notification


    【解决方案1】:

    如果你想通过他们的注册ID向许多客户发送推送通知,你必须使用registration_ids而不是to字段。您可以在docs(第二个参数)中找到它。


    您还对令牌进行了两次编码:

    $fields = array('to' =&gt; json_encode($tokensPerEvent), ...

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

    尝试在数组中跳过编码$tokensPerEvent$fields = array('to' =&gt; $tokensPerEvent, ...

    【讨论】:

    • 当我这样做时,我收到了 -> 字段“to”必须是 JSON 字符串:******
    • @Yakir Okey... 您想通过他们的注册 ID 向许多客户发送推送通知吗?然后你必须使用registration_ids 而不是to 你可以在文档中找到这个......我会尝试找到这个,但我很确定它是。编辑:好的,我找到了。第二个参数firebase.google.com/docs/cloud-messaging/http-server-ref
    • @Yakir 好的,我会回答。
    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    相关资源
    最近更新 更多