【问题标题】:Push notification from PHP to an android app with FCM使用 FCM 将通知从 PHP 推送到 android 应用程序
【发布时间】:2018-08-23 15:04:15
【问题描述】:

我是 PHP 的初学者,我想编写一个程序来从本地主机(在 PHP 中)向 Android 应用程序发送一个简单的通知(一个标题和一条小消息)。为此,我使用 Firebase。目前,我可以从 FCM 向 android 发送通知,但不能从 PHP 程序向 android 应用程序发送通知。我尝试了很多不同的方式和程序来发送此通知,但总是失败。 这是我使用的程序:

<html>
<?php
 $read = shell_exec ('gpio read 0');
 $status = intval($read);
 if ($status == 1) {
 function sendFCM($mess,$id) {
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array (
        'AAAAS4YkIPw:A.........rLQjQ' => $id,   
        'notification' => array (
        "body" => $mess,
                "Doorbell" => "Title",
        //        "icon" => "myicon"
        )
    );
    $fields = json_encode ( $fields );
    $headers = array (
            'Authorization: key=' . "AIzaSy.....J_Hy0",
            'Content-Type: application/json'
    );

    $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_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    curl_close ( $ch );
    }
    print ("oui");
    sleep(30);
    header("Refresh:0");
 } 
    else {
    print ("non");
    sleep(2);
    header("Refresh:0");
}
?>
</html>

在我使用的每个程序中,也许我每次都犯同样的错误,但我没能找到它。只是为了解释一下,这个程序读取了一个 GPIO,作为结果的函数,他告诉“oui”或“non”并发送一个通知,它是“oui”。我在一个网站上的程序在第 6 行和第 30 行之间。其余的都可以。我的问题是我必须没有收到通知。

【问题讨论】:

  • 谁在调用你的函数 sendFCM()?什么时候?以及使用哪些参数?

标签: php android firebase push-notification localhost


【解决方案1】:

我看到一个错误后更改了我的程序,所以这是新的。

<html>
<head>
</head>
<?php

function sendFCM($mess,$id) {
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array (
        'AAAAS4Yk.........KirLQjQ' => $id,  
        'notification' => array (
        "body" => $mess,
                "Doorbell" => "Title",
        //        "icon" => "myicon"
        )
    );
    $fields = json_encode ( $fields );
    $headers = array (
            'Authorization: key=>' . "$ip",
            'Content-Type: application/json'
    );

    $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_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    curl_close ( $ch );
    }

$read = shell_exec ('gpio read 0');
$status = intval($read);
if ($status == 1) {
    sendFCM('Yolo','AIzaSy......xdJ_Hy0');
    print ("oui");
    sleep(30);
    header("Refresh:0");
} 
    else {
    print ("non");
    sleep(2);
    header("Refresh:0");
}
?>
</html>

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 2018-05-05
    • 2017-04-11
    • 1970-01-01
    • 2017-09-15
    • 2019-08-05
    • 2017-12-02
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多