【问题标题】:Sending different push notification to each device向每个设备发送不同的推送通知
【发布时间】:2014-06-04 06:55:34
【问题描述】:

我已经设置了将发送推送通知的 php 脚本。一切正常,它向数组中的每个设备发送相同的消息。但是让我烦恼的是,如果我想在每个设备上发送不同的消息怎么办(例如取决于设备偏好)?

这里的正确方法是什么?我的第一个想法是分别为每个设备调用 pushNotification 函数,而不是调用一次并将设备数组发送到 GCM?

这是我正在使用的代码,所以请指教...

function sendPushNotificationToGCM($registatoin_ids, $message) {
    //Google cloud messaging GCM-API url
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );
    // Google Cloud Messaging GCM API Key
    define("GOOGLE_API_KEY", "my_api_key");    
        $headers = array(
            'Authorization: key=' . GOOGLE_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_VERIFYHOST, 0); 
        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);
}

$devices = array();

    while($results = $result->fetch_array()) {
        $devices[] = $results['dev_reg_id'];
    }

    //return $devices;

    $pushMessage = $message;

    $message = array("m" => $pushMessage); 
    $pushStatus = sendPushNotificationToGCM($devices, $message);

【问题讨论】:

  • 好吧,我不是 php 人,但您似乎将设备 ID 和消息数组作为参数传递给此函数。传递具有特定设备 ID 的特定消息以更具体地发送通知
  • @ajitksharma 谢谢。这就是我的想法,使用特定消息分别为每个设备调用 sendNotification 函数,而不是在一个函数调用中为所有设备发送相同的消息。我想知道这是否是正确的方法......

标签: php android push-notification google-cloud-messaging


【解决方案1】:

您不能在对 GCM 服务器的同一个 HTTP 请求中向不同设备发送不同的消息。如果每个设备都需要唯一的消息,则还需要对您的 sendPushNotificationToGCM 函数进行唯一调用。如果某些设备共享相同的消息,您可以在同一个函数调用中将该消息发送给它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2017-04-18
    相关资源
    最近更新 更多