首页 > 解决方案 > 是否可以检查firebase是否将通知传递到php中的单个设备?

问题描述

我有一个在服务器上使用 MySQL 数据库的 android 应用程序。当新数据插入表中时,我的 php 文件通过 Firebase 推送通知。如果 firebase 将通知发送到单个设备,则数据将保存到 table_1,如果 firebase 无法发送通知,则将数据保存到 table_2。我的问题是;是否可以检查firebase是否在php中传递通知?

FirebaseNotification.php

class FirebaseNotification {

public function send($registration_ids, $notification){
    $fields = array(
        'registration_ids' => $registration_ids,
        'data' => $notification
    );
    return $this->sendPushNotification($fields);
}
private function sendPushNotification($fields){

    require_once dirname(__FILE__) . '/Constants.php';

    $url = 'https://fcm.googleapis.com/fcm/send';

    $headers = array(
        'Authorization: key=' . FIREBASE_API_KEY,
        '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_SSL_VERIFYPEER, false);


    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);
    if ($result === FALSE){
        die('Curl failed: ' . curl_error($ch));
    }


    curl_close($ch);


    return $result;
}
}

PushNotification.php

class PushNotification{
private $mTitle;
private $mContent;
private $mProcess;

function __construct($mTitle, $mContent, $mProcess)
{
    $this->mTitle = $mTitle;
    $this->mContent = $mContent;
    $this->mProcess = $mProcess;
}

public function getPushNotification(){
    $res = array();
    $res['ntf']['title'] = $this->mTitle;
    $res['ntf']['content'] = $this->mContent;
    $res['ntf']['process'] = $this->mProcess;

    return $res;
}
}

标签: phpfirebasefirebase-cloud-messaging

解决方案


不幸的是,这是目前不可能的。从专用后端,您只能检查消息是否已成功传递到 Firebase 或 Firebase 是否拒绝了该消息(例如,当它格式错误或目标不存在时)。

但是,无法从专用后端跟踪或检查从 Firebase 到接收者的交付 - 没有我们可以调用的 API 端点来执行此操作。

据我所知,这目前不适用于通过 Firebase Web 控制台发送的消息。


推荐阅读