firebase fcm发送多个推送通知,即使只发送了一个

时间:2017-08-22 09:11:51

标签: php android laravel firebase firebase-cloud-messaging

我一直在使用firebase fcm向我的应用中的Android用户发送推送通知。通常推送通知工作正常。但是,无论我通过刷卡打开或关闭消息多少次,有时同一消息仍会反复显示在应用程序上。应用程序没有任何问题,因为当从互联网断开连接时消息停止。我很困惑它是不是它的firebase故障还是我的代码问题,因为它只会在某个时候发生。

以下是用于发送推送通知的代码:

public function android_notification_by_fcm($android_users, $message)
{
    $device_tokens = array();
    if (!empty($android_users)) {
        foreach ($android_users as $android_use) {
            array_push($device_tokens, $android_use->reg_id);
        }

        $url = "https://fcm.googleapis.com/fcm/send";
        // $notification = "";
        $fields = array(
            'registration_ids' => $device_tokens,
            'data' => $message
        );


        $fields = json_encode($fields);
        $headers = array(
            'Authorization:key = secretkey',
            '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, $fields);
        $result = json_decode(curl_exec($ch));
        if ($result) {
            foreach ($result->results as $res) {
                if (isset($res->error)) {
                    \Log::warning('android sending notification for detail version failed due to: ' . $res->error);
                } else {
                    \Log::info('android sending notification for detail version success. Message id is: ' . $res->message_id);
                }
            }
        }
        curl_close($ch);
        return 1;

    } else {
        return 2;
    }


}

1 个答案:

答案 0 :(得分:-1)

`

function sendNotification($id, $message, $title='', $subTitle='', $tickerText='', $ids){
    if(count($ids)){// ids is an array
        $msg = array
        (
            'id'    => $id,
         'message'     => $message,
         'title'        => $title,
         'subtitle'    => $subTitle,
         'tickerText'    => $tickerText,
         'priority' => 'high',
         'vibrate'    => 1,
         'sound'        => 1);
        $post = array
        (
            'registration_ids'  => $ids,
            'data'              => $msg,
         'priority' => 'high',
             'notification' => array('sound' => 'default', 'title' => $title,'body' => $tickerText)
         );
         /*   API_ACCESS_KEY: is a const contain API access key from google  */
        $headers = array
        ( 
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
        $result = curl_exec($ch);
        if (curl_errno($ch)){
            echo 'GCM error: ' . curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }
}

`