I'm trying to setup a server to send push notifications.
I'm trying to send push notification toward user of an specified package e.g. something like com.mysite.android
(This options is available in Firebase console).
Checking answers like this I could not understand how to set to
parameter to send push notifications to user of an specified package. I can find sample which send notification to specific devices by their ids
or news topics
.
Clarification: I'm the owner of application which I want sent push notifications.
If it helps here is my code:
<?php
/*
Parameter Example
$data = array('post_id'=>'12345','post_title'=>'A Blog post');
$target = 'single tocken id or topic name';
or
$target = array('token1','token2','...'); // up to 1000 in one request
*/
echo "Start<br/>";
$result = sendMessage('{"id":"hello"}',null);
var_dump($result);
function sendMessage($data,$target){
//FCM api URL
$url = 'https://fcm.googleapis.com/fcm/send';
//api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'xxxxxxxxxxxxxxxxxxxkeyxxxxxxxxxxxxxxxxxxxxxxxx';
$fields = array();
$fields['data'] = $data;
$fields['restricted_package_name']='com.mysite.android ';
$fields['dry_run']=true;
if (isset($target)){
if(is_array($target)){
$fields['registration_ids'] = $target;
}else{
$fields['to'] = $target;
}
}
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$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_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
?>
Notice: I removed my server_key
intentionally.