向全局发送推送通知不起作用(Android,Firebase)

I'm having this formform and i want to send a topic notifications to all my devices that are registered to my android app.If i put the Firebase Reg id it works fine with the specific device but i want all the devices(Send to topic global).

Here are all the php files that i have:

Index.php

<html>
<head>
    <title>AndroidHive | Firebase Cloud Messaging</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="//www.gstatic.com/mobilesdk/160503_mobilesdk/logo/favicon.ico">
    <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">

    <style type="text/css">
        body{
        }
        div.container{
            width: 1000px;
            margin: 0 auto;
            position: relative;
        }
        legend{
            font-size: 30px;
            color: #555;
        }
        .btn_send{
            background: #00bcd4;
        }
        label{
            margin:10px 0px !important;
        }
        textarea{
            resize: none !important;
        }
        .fl_window{
            width: 400px;
            position: absolute;
            right: 0;
            top:100px;
        }
        pre, code {
            padding:10px 0px;
            box-sizing:border-box;
            -moz-box-sizing:border-box;
            webkit-box-sizing:border-box;
            display:block; 
            white-space: pre-wrap;  
            white-space: -moz-pre-wrap; 
            white-space: -pre-wrap; 
            white-space: -o-pre-wrap; 
            word-wrap: break-word; 
            width:100%; overflow-x:auto;
        }

    </style>
</head>
<body>
    <?php
    // Enabling error reporting
    error_reporting(-1);
    ini_set('display_errors', 'On');

    require_once __DIR__ . '/firebase.php';
    require_once __DIR__ . '/push.php';

    $firebase = new Firebase();
    $push = new Push();

    // optional payload
    $payload = array();
    $payload['team'] = 'India';
    $payload['score'] = '5.6';

    // notification title
    $title = isset($_GET['title']) ? $_GET['title'] : '';

    // notification message
    $message = isset($_GET['message']) ? $_GET['message'] : '';

    // push type - single user / topic
    $push_type = isset($_GET['push_type']) ? $_GET['push_type'] : '';

    // whether to include to image or not
    $include_image = isset($_GET['include_image']) ? TRUE : FALSE;


    $push->setTitle($title);
    $push->setMessage($message);
    if ($include_image) {
        $push->setImage('http://api.androidhive.info/images/minion.jpg');
    } else {
        $push->setImage('');
    }
    $push->setIsBackground(FALSE);
    $push->setPayload($payload);


    $json = '';
    $response = '';

    if ($push_type == 'topic') {
        $json = $push->getPush();
        $response = $firebase->sendToTopic('global', $json);
    } else if ($push_type == 'individual') {
        $json = $push->getPush();
        $regId = isset($_GET['regId']) ? $_GET['regId'] : '';
        $response = $firebase->send($regId, $json);
    }
    ?>
    <div class="container">
        <div class="fl_window">
            <div><img src="http://api.androidhive.info/images/firebase_logo.png" width="200" alt="Firebase"/></div>
            <br/>
            <?php if ($json != '') { ?>
                <label><b>Request:</b></label>
                <div class="json_preview">
                    <pre><?php echo json_encode($json) ?></pre>
                </div>
            <?php } ?>
            <br/>
            <?php if ($response != '') { ?>
                <label><b>Response:</b></label>
                <div class="json_preview">
                    <pre><?php echo json_encode($response) ?></pre>
                </div>
            <?php } ?>

        </div>

        <form class="pure-form pure-form-stacked" method="get">
            <fieldset>
                <legend>Send to Single Device</legend>

                <label for="redId">Firebase Reg Id</label>
                <input type="text" id="redId" name="regId" class="pure-input-1-2" placeholder="Enter firebase registration id">

                <label for="title">Title</label>
                <input type="text" id="title" name="title" class="pure-input-1-2" placeholder="Enter title">

                <label for="message">Message</label>
                <textarea class="pure-input-1-2" rows="5" name="message" id="message" placeholder="Notification message!"></textarea>

                <label for="include_image" class="pure-checkbox">
                    <input name="include_image" id="include_image" type="checkbox"> Include image
                </label>
                <input type="hidden" name="push_type" value="individual"/>
                <button type="submit" class="pure-button pure-button-primary btn_send">Send</button>
            </fieldset>
        </form>
        <br/><br/><br/><br/>

        <form class="pure-form pure-form-stacked" method="get">
            <fieldset>
                <legend>Send to Topic `global`</legend>

                <label for="title1">Title</label>
                <input type="text" id="title1" name="title" class="pure-input-1-2" placeholder="Enter title">

                <label for="message1">Message</label>
                <textarea class="pure-input-1-2" name="message" id="message1" rows="5" placeholder="Notification message!"></textarea>

                <label for="include_image1" class="pure-checkbox">
                    <input id="include_image1" name="include_image" type="checkbox"> Include image
                </label>
                <input type="hidden" name="push_type" value="topic"/>
                <button type="submit" class="pure-button pure-button-primary btn_send">Send to Topic</button>
            </fieldset>
        </form>
    </div>
</body>

push.php

<?php

class Push {

    // push message title
    private $title;
    private $message;
    private $image;
    // push message payload
    private $data;
    // flag indicating whether to show the push
    // notification or not
    // this flag will be useful when perform some opertation
    // in background when push is recevied
    private $is_background;

    function __construct() {

    }

    public function setTitle($title) {
        $this->title = $title;
    }

    public function setMessage($message) {
        $this->message = $message;
    }

    public function setImage($imageUrl) {
        $this->image = $imageUrl;
    }

    public function setPayload($data) {
        $this->data = $data;
    }

    public function setIsBackground($is_background) {
        $this->is_background = $is_background;
    }

    public function getPush() {
        $res = array();
        $res['data']['title'] = $this->title;
        $res['data']['is_background'] = $this->is_background;
        $res['data']['message'] = $this->message;
        $res['data']['image'] = $this->image;
        $res['data']['payload'] = $this->data;
        $res['data']['timestamp'] = date('Y-m-d G:i:s');
        return $res;
    }

}

firebase.php

    <?php

class Firebase {

    // sending push message to single user by firebase reg id
    public function send($to, $message) {
        $fields = array(
            'to' => $to,
            'data' => $message,
        );
        return $this->sendPushNotification($fields);
    }

    // Sending message to a topic by topic name
    public function sendToTopic($to, $message) {
        $fields = array(
            'to' => '/topics/' . $to,
            'data' => $message,
        );
        return $this->sendPushNotification($fields);
    }

    // sending push message to multiple users by firebase registration ids
    public function sendMultiple($registration_ids, $message) {
        $fields = array(
            'to' => $registration_ids,
            'data' => $message,
        );

        return $this->sendPushNotification($fields);
    }

    // function makes curl request to firebase servers
    private function sendPushNotification($fields) {

        require_once __DIR__ . '/config.php';

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

        $headers = array(
            'Authorization: key=' . FIREBASE_API_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);

        return $result;
    }
}

?>

And the Config.php where i define my firebase_api_key.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐