Firebase云消息传递

时间:2017-01-07 10:01:50

标签: android firebase-cloud-messaging

我无法通过应用服务器接收fcm中的主题消息。 我订阅的主题显示在Firebase控制台中。 当我尝试发送请求时,响应将通过标记:message_id发出 但fcm消息未出现在设备上。 我尝试了androidhive的演示,在通过单个设备发送时也是如此 选择了设备上弹出的fcm,但主题消息未在设备上发送。

<?php class Firebase { public function sendToTopic($to, $message) {
    $fields = array(
        'to' => '/topics/' . $to,
        'data' => $message,
    );
    return $this->sendPushNotification($fields);
}



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

    require_once  '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;
}

} ?&GT;

<html>
    <head>
        <title> 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 'firebase.php';
    require_once 'push.php';

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



    // 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('https://s3-ap-southeast-1.amazonaws.com/localcircles-s/circleimages/09092015/178681857255efc93313ad9_male_09092015112251_profile.jpg');
    } else {
        $push->setImage('');
    }
    $push->setIsBackground(FALSE);



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

    if ($push_type == 'topic') {
        $json = $push->getPush();
        $response = $firebase->sendToTopic('offers', $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>


        <br/><br/><br/><br/>

        <form class="pure-form pure-form-stacked" method="get">
            <fieldset>
                <legend>Send to Topic `offers`</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>

0 个答案:

没有答案