Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php代码向android发送推送通知?_Php_Android_Push Notification_Android Notifications_Android Push Notification - Fatal编程技术网

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

如何使用php代码向android发送推送通知?,php,android,push-notification,android-notifications,android-push-notification,Php,Android,Push Notification,Android Notifications,Android Push Notification,我正在集成android推送通知,它在客户端运行良好。是否要使用PHP生成服务器推送通知? 如果您知道同样的情况,请建议。这段代码使用PHP从Firebase向android设备发送通知 $server_key=""; // get this from Firebase project settings->Cloud Messaging $user_token=""; // Token generated from Android device after setting up fireb

我正在集成android推送通知,它在客户端运行良好。是否要使用PHP生成服务器推送通知?
如果您知道同样的情况,请建议。

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

这是我在开发中提到的源代码。你可以在phpfiddle上试试。参考文献:。您可以检查有效负载通知是否存在

定义('API\u访问密钥','add\u your\u server\u firebase\u访问密钥');
$msg=
[
'类型'=>“123”,
“主体”=>“由开发人员编写”,
“标题”=>“推送测试”,
“声音”=>“默认值”,
“徽章”=>“0”,
“id”=>“100”
];
$headers=数组
(
“Authorization:key=”.API_ACCESS_key,
'内容类型:应用程序/json'
);
$firebaseKey=array('firebase_key_01'、'firebase_key_02'、'firebase_key_03');
试一试{
foreach($firebaseKey作为$findedKey)
{
$fields=array('registration\u id'=>array($findedKey),
“数据”=>$msg,
);
如果(!function_存在('curl_version')){
抛出新异常(“此服务器中未安装CURL…”);
}
$ch=curl_init();
如果(!$ch){
抛出新异常(“CURL初始化失败…”);
}
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\u exec($ch);
卷曲关闭($ch);
预处理(结果);
}
如果(!$result){
$error=curl_error($ch)| |“curl执行失败…”;
抛出新异常($error);
}如果($结果){
echo“推送通知发送成功…”;
}
}
捕获(例外$e)
{
回显“消息:推送通知未发送错误:”。$e->getMessage();
}

您正在使用Firebase云消息吗?谢谢。你能说一下如何在相同的消息字段上设置android通知徽章吗?请不要只发布代码作为答案,还要解释你的代码的作用以及它是如何解决问题的。带解释的答案通常更有帮助,质量更好,更容易吸引选票,但这没有用。什么是注册id?你的例子中没有定义它们,你也没有给出任何关于如何获得它们的建议。
<?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;

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