如何使用PHP cURL和FCM将通知发送到移动设备?

时间:2018-05-30 07:25:02

标签: php curl push-notification

如何使用PHP cURL和FCM向移动设备发送通知?

$ch = curl_init(); //init
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');//Fire URL
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//send 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); //.json_encode($fields);
return $result;
curl_close( $ch );

2 个答案:

答案 0 :(得分:1)

$to - 设备 ID

$title - 通知标题

$message - 通知消息

$img - 完整图片链接

$datapayload - 自定义值数组。

用法:

sendnotification($to, $title, $message, $img, $datapayload = "");

带有演示值:

sendnotification("Device_ID","Test Notification","Test Message","https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",array("ID" =>1));

function sendnotification($to, $title, $message, $img = "", $datapayload = ""){
$msg = urlencode($message);
$data = array(
    'title'=>$title,
    'sound' => "default",
    'msg'=>$msg,
    'data'=>$datapayload,
    'body'=>$message,
    'color' => "#79bc64"
);
if($img){
    $data["image"] = $img;
    $data["style"] = "picture";
    $data["picture"] = $img;
}
$fields = array(
    'to'=>$to,
    'notification'=>$data,
    'data'=>$datapayload,
    "priority" => "high",
);
$headers = array(
    'Authorization: key=GOOGLE_API_KEY',
    'Content-Type: application/json'
);
$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 );
return $result;

}

答案 1 :(得分:0)

CREATE OR REPLACE FUNCTION num_to_words (NUM IN NUMBER)
RETURN VARCHAR2
IS

v_length NUMBER;
v_words VARCHAR2(100);
word_1 VARCHAR2(100);

BEGIN
word_1 := 'HUNDRED';
v_length := LENGTH(NUM);

IF v_length = 3
THEN v_words := SUBSTR(TO_CHAR(TO_DATE(NUM,'J'),'JSP'),1,3) ||' '|| word_1;
END IF;

RETURN(v_words);
END;