GCM设置推送通知的到期日期 [英] GCM set expiration date for push notifications

查看:83
本文介绍了GCM设置推送通知的到期日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功使用PHP为我的android用户发送了GCM推送通知,但是我想为我的推送通知设置过期日期,因此,如果用户在1周内没有互联网连接,它将不会收到旧的推送通知. 我如何实现它.

I am successfully sending GCM push notifications for my android users using PHP, but i want to set expiration date for my push notifications so in case the user has no internet connection for 1 week, it will not receive the old push notifications. How i can achieve it.

谢谢.

这是php代码:

 private $GOOGLE_API_KEY = "XXXXX";

    public function send($registration_id, $data) {
        // include config
        // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $registration_id,
            'data' => $data,
        );
        $headers = array(
            'Authorization: key=' . $this->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;
    }

推荐答案

有一个在发送时设置的参数time_to_live.值以秒为单位(仅一分钟以下)

there is a param time_to_live which you are setting while sending. value is in seconds (below one minute only)

$fields = array(
        'registration_ids' => $registration_id,
        'data' => $data,
        'time_to_live' => 60
    );

您可以在发送请求中使用time_to_live参数来指定消息的最大寿命.此参数的值必须为0到2,419,200秒之间的持续时间,并且它对应于GCM将存储并尝试传递消息的最大时间段.不包含此字段的请求的默认最长期限为4周.

You can use the time_to_live parameter in the send request to specify the maximum lifespan of a message. The value of this parameter must be a duration from 0 to 2,419,200 seconds, and it corresponds to the maximum period of time for which GCM will store and try to deliver the message. Requests that don't contain this field default to the maximum period of 4 weeks.

更多此处

这篇关于GCM设置推送通知的到期日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆