ios使用firebase发送通知

MRu*_*ade 6 php push-notification ios firebase firebase-cloud-messaging

我正在尝试创建带有通知的应用程序。通知必须从 php 文件发送到 firebase,然后再发送到设备。起初,我尝试使用 firebase 控制台发送,并且效果很好。但是当我尝试使用 php 发送通知时,我遇到了问题。它说已成功发送,但我没有收到任何通知。有我的 php 用于发送通知:

<?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;
Run Code Online (Sandbox Code Playgroud)

?>

我已经寻找修复它。但没有任何帮助。

MRu*_*ade 6

大家好,我找到了解决方案。如果有人搜索希望它会有所帮助:

// 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;
Run Code Online (Sandbox Code Playgroud)

  • @MRustamzade 如何通过推送发送一些额外的密钥?像 order_id 或类型? (2认同)