0
PHP를 사용하여 푸시 알림을 여러 장치에 보내려고합니다. 다음은 내 코드입니다PHP : 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);
그러나 결과는 다음과 같습니다.
{"multicast_id":idhere,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
내가이 배열에
$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);
}
해피 코딩이
firebase에 diffent entereis를 기록하지 않습니까? – Sikander
API 키와 기기 ID를 다시 생성 해보세요. –