如何使用PHP代码向Android发送推送通知?

时间:2017-11-30 07:03:18

标签: php android push-notification android-notifications android-push-notification

我正在集成android推送通知在客户端正常工作。我想使用PHP进行服务器推送通知吗? 如果你知道的话,请建议。

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

这是我为开发而提到的源代码。你可以在phpfiddle上试试。参考:Push notification (PHP)。您可以检查firebase here的有效负载通知。

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

?>

答案 2 :(得分:0)

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