简体   繁体   中英

firebase fcm sending multiple push notification even though only one has been sent

I have been using firebase fcm for sending push notification to android users in my app. Usually the push notification works fine. But, sometime same message keeps showing on the app repeatedly no matter how many times I open or close the message by swiping. There is nothing wrong with app as the message stops when disconnected from the internet. I am confused whether it's firebase glitch or an issue with my code as it only happens sometime.

Here's the code that is used to send push notification:

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


}

`

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

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM