如何使用PHP发送FCM通知?

时间:2016-12-06 01:55:38

标签: php android curl firebase firebase-cloud-messaging

我尝试使用PHP代码/网络浏览器发送FCM。

但问题是当我使用PHP网络浏览器发送它时:

  • FCM通知仅显示在虚拟设备上。
  • FCM通知未出现在真实手机设备上。

我只能使用Firebase控制台向实际的手机设备发送FCM通知。

有人可以帮忙吗?代码如下。

<?php
require "init.php";
global $con;
    if(isset($_POST['Submit'])){
        $message     = $_POST['message'];
        $title       = $_POST['title'];
        $path_to_fcm = 'https://fcm.googleapis.com/fcm/send'; 
        $server_key  = "AAAA2gV_U_I:APA91bHA28EUGmA3BrDXFInGy-snx8wW6eZ_RUE7EtOyM99pbfrVZU_ME-FU0O9_dUxYpM30OYF8KWYlixod_PfwbgLNoovzdkdJ4F-30vY8X_tBz0CMrajCIAgbNVRfw203YdRGli";    
        $sql     = "SELECT fcm_token FROM fcm_table";
        $result  = mysqli_query($con, $sql);
        $row     = mysqli_fetch_row($result);
            $key = $row[0];
        $headers = array('Authorization:key=' .$server_key, 'Content-Type:application/json');
        $fields  = array('to' => $key, 'notification' => array('title' => $title, 'body'=> $message));
        $payload = json_encode($fields);
        $curl_session = curl_init();
        curl_setopt($curl_session, CURLOPT_URL, $path_to_fcm);
        curl_setopt($curl_session, CURLOPT_POST, true);
        curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_session, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($curl_session, CURLOPT_POSTFIELDS, $payload);
        $result = curl_exec($curl_session);
        curl_close($curl_session);
        mysqli_close($con);
    }   
?>
<!DOCTYPE html>
<html>
<head>
    <title>FCM Notification</title>
</head>
<body>
    <form action='fcm_notification.php' method="POST">
        <table>
            <tr>
                <td>Title : </td>
                <td><input type="text" name="title" required="required" /></td>
            </tr>
            <tr>
                <td>Message : </td>
                <td><input type="text" name="message"  required="required" /></td>
            </tr>
            <tr>
                <td><input type="submit" name="Submit" value="Send notification"></td>

            </tr>
        </table> 
    </form>
</body>
</html>

感谢。

3 个答案:

答案 0 :(得分:2)

通过以下方式,您可以使用Google FCM向移动设备发送推送通知。对我来说,它的工作符合预期。添加密钥'priority' => 'high'

function sendPushNotification($fields = array())
{
    $API_ACCESS_KEY = 'YOUR KEY';
    $headers = array
    (
        'Authorization: key=' . $API_ACCESS_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;
}

$title = 'Whatever';
$message = 'Lorem ipsum';
$fields = array
(
    'registration_ids'  => ['deviceID'],
    'data'          => '',
    'priority' => 'high',
    'notification' => array(
        'body' => $message,
        'title' => $title,
        'sound' => 'default',
        'icon' => 'icon'
    )
);

sendPushNotification($fields);

答案 1 :(得分:0)

您可以创建发送设备推送通知的功能。

a.click()

有关详细信息,请参阅此

to - 类型字符串 - (可选)[邮件的收件人] 该值必须是单个注册令牌,通知密钥或主题。发送到多个主题时不要设置此字段

registration_ids - 类型字符串数组 - (可选)[消息的收件人] 多个注册令牌,最小1个最大1000个。

优先级 - 类型字符串 - (可选)[默认正常] 允许值正常和高。

delay_while_idle - 输入布尔值 - (可选)[默认值为false] true表示在设备变为活动状态之前不应发送消息。

time_to_live - 输入JSON号码 - (可选)[默认值4周最长4周] 此参数指定在设备脱机时,应在FCM存储中保留消息的时间长度(以秒为单位)

数据 - 输入JSON对象 指定消息的有效内容的自定义键值对。 例如。 {“post_id”:“1234”,“post_title”:“博客帖子标题”}

答案 2 :(得分:0)

在Android中,您可以在onMessageReceived()中将其作为地图数据...

接收

在后台时 - 应用会在通知托盘中收到通知有效内容,并仅在用户点按通知时处理数据有效内容。

在前台时 - 应用会收到两个有效负载的消息对象。

public class FcmMessageService extends FirebaseMessagingService{
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    //onMessageReceived will be called when ever you receive new message from server.. (app in background and foreground )
    Log.d("FCM", "From: " + remoteMessage.getFrom());

    if(remoteMessage.getNotification()!=null){
    Log.d("FCM", "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }

    if(remoteMessage.getData().containsKey("post_id") && remoteMessage.getData().containsKey("post_title")){
        Log.d("Post ID",remoteMessage.getData().get("id").toString());
        Log.d("Post Title",remoteMessage.getData().get("post_title").toString());
        // eg. Server Send Structure data:{"post_id":"12345","post_title":"A Blog Post"}
    }
}}
相关问题