Issue
I try to send notification from my website on event click to my device (in this case my PC). i handle the event click with ajax and send it to my controller. btw i'm using codeigniter 4.
FCM was success but no notification to my pc. i've tried to follow this solution but still, no notification.
here's is the response of my fcm when i var_dump the curl_exec($ch)
string(144) "{"multicast_id":3939414893160702393,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"6b651aff-c990-4c90-8bf8-c24c69571f28"}]}"
so what is the problem here?
my script:
const messaging = firebase.messaging();
messaging
.requestPermission()
.then(function () {
// get the token in the form of promise
return messaging.getToken()
})
.then(function(token) {
console.log(token)
// device_token.value = token;
// server_key.value = config.serverKey;
$('#login_btn').click(function(){
var email = $('input[name=email]').val();
var pass = $('input[name=pass]').val();
var device_token = token;
var server_key = config.serverKey;
if(email == '' || pass == ''){
alert('Email dan Password tidak boleh kosong');
}else{
$.ajax({
url : '<?php echo base_url('Login/aksiLogin'); ?>',
type : 'POST',
data : {
email : email,
pass : pass,
device_token : device_token,
server_key : server_key
},
success : function(res){
alert("berhasil");
}
})
}
})
})
.catch(function (err) {
console.log("Unable to get permission to notify.", err);
});
my controller :
function aksiLogin(){
send_notif($this->input->post('device_token'), $this->input->post('server_key') , 'Title message', 'Body Message');
die();
}
my helper :
if (!function_exists('send_notif')) {
function send_notif($token, $serverKey, $title, $body){
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = [
"to" => $token,
"notification" => [
"body" => "SOMETHING",
"title" => "SOMETHING",
// "icon" => "ic_launcher"
]
];
// var_dump($fields);
// die();
// Firebase API Key
$headers = array('Authorization:key='.$serverKey.'','Content-Type:application/json');
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
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);
var_dump($result);
die();
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
}
}
is it because my device? if it so, then why when i send notification with firebase console is working just fine?
edit : i'm still can't figure it out, can anyone help me?
Solution
I figure it out.
appearantly my problem is, I opened the page browser when sending the notif. so the notif, won't shows up. I tried to setTimeout to my form, and close the browser and the notif is there.
and for sending to android/ios device, it must be online server (hosting) to get the token. I tried locally, but the token is not generate.
Answered By - Fajar Alam Rahmat Sidik Answer Checked By - Terry (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.