My purpose is to send notification to specific login customer
Example : Customer ID : 1 , I need to subscribe them to topic “/topic/customer_1”
On JavaScript Site, after I got the permission token , I’ve called the server site to subscribe the customer to the topic by their token.
If I login by using PC , Then I can get TOKEN_A
If I login by using same account on mobile , Then I can get TOKEN_B
function IntitalizeFireBaseMessaging() {
messaging
.requestPermission()
.then(function () {
console.log("Notification Permission");
return messaging.getToken();
})
.then(function (token) {
console.log("Token : "+token);
$.ajax({
url : '/firebase-subscribe-customer-to-topic?token=' + token,
success: function(data){
console.log(data);
}
});
document.getElementById("token").innerHTML=token;
})
.catch(function (reason) {
console.log(reason);
});
}
This is Server Side (PHP)
$token = $_GET['token'];
$customer_id = 1;
$headers = array
('Authorization: key=MY_FIREBASE_KEY' ,
'Content-Type: application/json');
$ch = curl_init();
// browser token you can get it via ajax from client side
curl_setopt($ch, CURLOPT_URL, "https://iid.googleapis.com/iid/v1/$token/rel/topics/customer_" . $customer_id);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, array());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
Now the problem is that I can receive the notification only on PC and not on mobile.
And here is the send notification code for firebase
$payload = [
'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
'tab_index' => 3
];
$this->fireBaseNootification("/topics/customer_1" ,"Title","Description","https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",$payload);
private function fireBaseNootification($to, $title, $message, $img = "", $datapayload = ""){
$msg = urlencode($message);
$data = array(
'title'=>$title,
'sound' => "default",
'msg'=>$msg,
'data'=>$datapayload,
'body'=>$message,
'color' => "#79bc64"
);
// if($img){
// $data["image"] = $img;
// $data["style"] = "picture";
// $data["picture"] = $img;
// }
$fields = array(
'to'=>$to,
'notification'=>$data,
'data'=>$datapayload,
'content_available' => true,
"priority" => "high",
);
$headers = array(
'Authorization: key=MY_SERVER_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);
curl_close( $ch );
return $result;
}
Could you tell which part I was wrong?