1

我正在集成 android 推送通知在客户端工作正常。我想使用 PHP 制作服务器推送通知吗?请建议您是否知道相同的内容。

4

3 回答 3

3

这是我在开发过程中一直参考的源代码。你可以在 phpfiddle 上试试。参考:推送通知 (PHP)您可以在此处查看 firebase 的有效负载通知。

<?php

define( 'API_ACCESS_KEY', 'AIza......Xhdsnkf' ); // get API access 
key from Google/Firebase API's Console

$registrationIds = array( 'cyMSGTKBzwU:APA91...xMKgjgN32WfoJY6mI' ); //Replace this with your device token


// Modify custom payload here
$msg = array
(
        'mesgTitle'     => 'SMART TESTING',
        'alert'         => 'This is sample notification'

);
$fields = array
(
    'registration_ids'      => $registrationIds,
    'data'                  => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' ); //For firebase, use 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;

?>
于 2017-11-30T07:32:05.210 回答
1

这段代码使用 PHP 从 Firebase 向 android 设备发送通知

$server_key=""; // get this from Firebase project settings->Cloud Messaging
$user_token=""; // Token generated from Android device after setting up firebase
$title="New Message";
$n_msg="The is a message";

$ndata = array('title'=>$title,'body'=>$n_msg);

$url = 'https://fcm.googleapis.com/fcm/send';

$fields = array();
$fields['data'] = $ndata;

$fields['to'] = $user_token;
$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);
于 2017-11-30T07:23:32.273 回答
-1
define( 'API_ACCESS_KEY', 'add_your_server_firebase_access_key');
<!-- set payload here -->
   $msg =
        [
            'type'      => "123",
            'body'      => "By Developer",
            'title'     => "Push Testing",
            'sound'     => "default",
            'badge'     => "0",
            'id'        => "100"
        ];
$headers = array
            (
              'Authorization: key='.API_ACCESS_KEY,
              'Content-Type: application/json'
            );
    

$firebaseKey=array('firebase_key_01','firebase_key_02','firebase_key_03'); <!-- your all user firebase token id pass here --> 
    try {
            foreach ($firebaseKey as $findedKey) 
              {
                  $fields = array('registration_ids' => array($findedKey),
                  'data' => $msg,
                    );
                    
                     if (!function_exists('curl_version')) {
                        throw new Exception("CURL is not installed in this server..!");
                    }
                          
                    $ch = curl_init();
                    if (!$ch) {
                        throw new Exception("CURL intialization fails..!");
                    }

                  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 );


                     pre($result);

                }

                 if (!$result) {
                    $error = curl_error($ch) || "CURL execution fails..!";
                    throw new Exception($error);
                } if ($result) {
                    echo "Push notification send successfully..!";
                }


        }
        catch (Exception $e) 
            {
                echo 'Message: Push notification didn't send error :' .$e->getMessage();
            }
于 2020-11-24T10:10:47.030 回答