2017-12-14 2 views
0

Ich versuche, die Push-Benachrichtigung an mehrere Geräte mit PHP zu senden. Folgendes ist mein CodePHP: Senden Sie mehrere Push-Benachrichtigungen zu verschiedenen Geräten mit Firebase

define('API_ACCESS_KEY', 'mykey'); 
    $message =' some message ' ; 
    $msg = array 
      (
      'body' =>$message, 
      'title' => 'You have a new message ', 

     ); 
      $regids = array('registration_ids' =>'firstid'); 
    $fields = array 
      (
       'to'   => json_encode($regids), 

       'notification' => $msg 
      ); 

    $headers = array 
      (
       'Authorization: key=' . API_ACCESS_KEY, 
       'Content-Type: application/json' 
      ); 
     $ch = curl_init(); 
     curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send'); 
     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); 
     echo $result; 
     curl_close($ch); 

aber Folgendes ist das Ergebnis, das ich bekomme.

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

Bitte helfen Sie mir, wie behebe ich dieses

Antwort

0

Put-Geräte-IDs in diesem Array

$dv_id [] = array(
       'dv_id' => $re['dv_id'], 
      ); 
//Loop through your id's array 

for ($i = 1; $i < count($dv_id); $i++) { 

//Call your send notification function link this 
    send_notification($dv_id[$i]['dv_id'],$title,$msg); 
} 

function send_notification($device_id,$title,$message){ 

// API access key from Google API's Console 
// prep the bundle 
    $msg = array 
    (
     'to'=>$device_id, 
     'notification' => array('body'=>$message,'title'=>$title, 
      'click_action'=>'MY_ACTIVITY_1','sound'=>'tone'), 
     'data' => array('message'=>$message,'title'=>$title) 

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

    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,'https://fcm.googleapis.com/fcm/send'); 
    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($msg)); 
    $result = curl_exec($ch); 
    curl_close($ch); 

} 

Glücklich Codierung.

+0

wird es nicht diffent Entereis in Firebase protokollieren? – Sikander

+0

Versuchen Sie, den API-Schlüssel und die Geräte-ID neu zu generieren. –

Verwandte Themen