问题描述
我试图通知创建应用程序。通知必须从php文件发送到firebase然后发送到设备。起初我试图用firebase控制台发送,它的工作原理是完美的。但是当我尝试使用PHP发送notificaion然后我有问题。它说成功发送,但我没有得到任何通知。有我的PHP发送通知:$ p $
<?php
函数send_notification($ tokens,$ message)
{
$ url ='https://fcm.googleapis.com/fcm/send';
$ fields = array(
'to'=> $ tokens,
'data'=> $ message
);
$ headers = array(
'Authorization:key = my api key',
'Content-Type:application / json'
);
$ 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('Curl failed:'。curl_error($ ch));
}
curl_close($ ch);
返回$ result;
$ data = array(alert=>今天怎么样);
$ token =fJVM7YrWGQg ......;
$ message = array(message=>FCM PUSH NOTIFICATION TEST MESSAGE);
$ message_status = send_notification($ token,$ message);
echo $ message_status;
?>
修理它。但没有什么帮助。
Hej大家,我找到了解决办法。如果有人搜索,希望它会有帮助:
$ b $ pre $ //从API API控制台访问API
define('API_ACCESS_KEY' ,'YOUR_FIREBASE_API_KEY');
$ registrationIds = array(设备firebasetoken here。);
//准备包
$ msg = array(
'body'=>message text,
'title'=>message title,
'vibrate'=> 1,
'sound'=> 1,
);
$ fields = array(
'registration_ids'=> $ registrationIds,
'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);
curl_close($ ch);
echo $ result;
I'm trying to create app with notification. Notification must be sended from php file to firebase and then to device. At first i have tried to send with firebase console and it works perfect. But when i try to send notificaion using php then i have problems. It says successfully sended but i didn't get any notifications. there is my php for sending notification:
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'to' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = my api key',
'Content-Type: application/json'
);
$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('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$data = array("alert" => "How it is going today");
$token = "fJVM7YrWGQg......";
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE");
$message_status = send_notification($token, $message);
echo $message_status;
?>
I have searched for fix it. But nothing helped.
Hej everybody, i found solution. if someone search hope it will help:
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR_FIREBASE_API_KEY' );
$registrationIds = array( "devices firebasetoken here." );
// prep the bundle
$msg = array(
'body' => "message text",
'title' => "message title",
'vibrate' => 1,
'sound' => 1,
);
$fields = array(
'registration_ids' => $registrationIds,
'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 );
curl_close( $ch );
echo $result;
这篇关于ios发送通知与firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!