首页 > 解决方案 > 如何在 php 中向数组中的所有用户发送推送通知?

问题描述

当我使用 {select token FROM riders} 时,我想这样做,它会选择所有令牌并将推送通知发送给所有令牌,但是当我这样做时,它不会发送任何通知。

<?php


define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'android_api');

//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

$stmt = mysqli_query($conn,"SELECT token FROM riders");
$query = mysqli_fetch_array($stmt);

$token = $query['token'];


$apiKey = "AAAAKCR7bo4:APA91bHuIwpxHkR5VxODbqje6b518xbBLWUnVfucuUJNJzqzTYDyww-   dwkPtgUj.........aDWTNyfARF0ru16"; //Server Key Legacy
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';


$notification = array('title' =>'iDELIVERY',
                   'body' => 'A Request Has Been Made');

$notifdata = array('title' =>'test title data',
                 'body' => 'hello',
                 'image' => 'path'
          );

$fcmNotification = array (
                    'to'        => $token, 
                    'notification' => $notification,
                    'data' => $notifdata 
                );


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

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$fcmUrl);
            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($fcmNotification));
            $result = curl_exec($ch);
          //  curl_close($ch);
?>

标签: phpandroidfirebasecurlfirebase-cloud-messaging

解决方案


我正在发送我在我的一个移动应用程序中使用过的工作代码,您应该根据您的数据集获取多个令牌,示例代码如下所示

$stmt = mysqli_query($conn,"SELECT token FROM riders where phone='026'");

//Itrate token and store it in an array based on your dataset
$allTokens = array();
foreach($tokens as $token){
    $allTokens[] = $token['token'];
}

 // This is a sample KEY
define( 'API_ACCESS_KEY', 'AIdsdyALOdsds7pR01rydsds0dsdqQndsdsOVpPpy4adsds');

// Build your message as an array
$msg = array( 
            'title'         => "Payment Alert",
            'message'       => "You have received a test payment",
            'bigText'       => "You have received a test payment",
            "subText"       => "You have received a test payment",
            'summaryText'   => 'Alert for payment',
            'click_action'  => 'FCM_PLUGIN_ACTIVITY',
            'vibrate'       => 300,
            'sound'         => 1,
);
$fields = array(
'registration_ids'  => $allTokens,  // multiple tokens will be available in array
'data'              => $msg
);
$headers = array(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
if(!empty($allTokens)){
    $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 );
}

推荐阅读