以gcm为单位设置生存时间选项

时间:2014-05-08 05:09:53

标签: android google-cloud-messaging

我正在做一个使用gcm的Android应用程序。我必须在服务器端设置,因为消息应该在一天后删除,如果没有交付。请帮助我。以下是向gcm服务器发送通知的功能

public function send_notification($registatoin_ids, $message, $coll_key) {
        // include config
        include_once './config.php';

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

        $fields = array(
            'collapse_key' => $coll_key,
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );

        $headers = array(
            'Authorization: key='.GOOGLE_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);
        echo $result;
    }

1 个答案:

答案 0 :(得分:0)

只需添加time_to_live参数,并为其指定一个值,该值等于您希望将消息存储在GCM服务器中的最大秒数,而不是交付。

$fields = array(
    'collapse_key' => $coll_key,
    'registration_ids' => $registatoin_ids,
    'data' => $message,
    'time_to_live' => $time_to_live,
);