繁体   English   中英

如何使用php代码向android发送推送通知?

[英]How to send push notification to android using php code?

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

这是我在开发过程中一直参考的源代码。 你可以在 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;

?>

这段代码使用 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);
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();
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM