Firebase Push Notification Plugin


#1

I am using Firebase for push notification in my ionic app. But unfortunately I am getting the same notification twice. I have searched through the net a lot and tried to apply all possible solutions found there but could not get it to work. My codes are as the following -

Web end (PHP)

$message_for_push = array(
    "data" => array(
        "title" => "New Notification",
        "body"  => "the message",
        "icon"  => "firebase-logo.png",
        'sound' => 'mySound',

        "type" => $message['type'],
        "section_id" => $message['section_id'],
        "isPush" => 1,
        "subdomain" => $subdomain, //dynamiic
        "id" => $message['id'],
        "other_key" => true
    ),
    /*"notification" => array(
        "title" => "New Notification",
        "body"  => $message['title'],
        "icon"  => "firebase-logo.png",
        'sound' => 'mySound',
        "type" => $message['type'],
        "section_id" => $message['section_id'],
        "isPush" => 1,
        "subdomain" => $subdomain, //dynamiic
        "id" => $message['id'],
    ),*/
    "registration_ids" =>  $registrationIds //array of device ids
);
$message_for_push = json_encode($message_for_push);
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_SSL_VERIFYHOST=>0,
    CURLOPT_SSL_VERIFYPEER=>0,
    CURLOPT_POSTFIELDS => $message_for_push,
    CURLOPT_HTTPHEADER => array(
        "authorization: key=THE_KEY_GIVEN_HERE",
        "cache-control: no-cache",
        "content-type: application/json"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

On the App End

I have saved the device id on my web end at the time of first login. The token is saved only once. It does not change until or unless the app is re-installed.

this.firebase.getToken().then(token => {
    this.devicInfo.uuid = token;

    console.log( 'device_id>>>>>', token ); ///saved on web end
}).catch(error => console.error('Error getting token', error));

When the notification is received (Do not think any problem here)-

this.firebase.onNotificationOpen().subscribe((pushed_data) => {
    let alert = this.alert.create({
        title: 'Notification Received!',
        message: pushed_data,
        buttons: ['OK']
    });
    alert.present();
});

The list of plugins used -

com-sarriaroman-photoviewer
cordova-plugin-console
cordova-plugin-device
cordova-plugin-file
cordova-plugin-file-opener2
cordova-plugin-file-transfer
cordova-plugin-firebase
cordova-plugin-network-information
cordova-plugin-splashscreen
cordova-plugin-statusbar
cordova-plugin-whitelist
cordova-plugin-youtube-video-player
cordova-sqlite-storage
ionic-plugin-keyboard

Any help is highly appreciable. Thanks.


#2

Currently we are also getting the same problem.
From what we understand so far, the app might be installed two receiver notification plugins. One is GCM and the other is Firebase based.

I tried push notification from Firebase Console and our app got two notifications.
But, when I push from http://apns-gcm.bryantan.info/ (using Legacy API Key), the app get only one notification.

So from this little test, we conclude there must be two plugins receive the notification, GCM (the old technology) and Firebase. Test message from Bryantan.info was catched by one of this plugin, while the other plugin failed. (probably because we use Legacy API Key?). This is purely speculation.

We still don’t know for sure if this is the reason. As information, we are using cordova-plugin-firebase and we’ve make sure we don’t install any other similar plugins such as (phonegap-plugin-push). I’ll update again, if we’ve found any progress…


#3

We think we’ve found the solution. We removed all the plugins and reiinstall it. (by typing “ionic cordova plugin rm xxxxxxx” one by one, and then add it back again “ionic cordova plugin add xxxxxxx”)

And now it only push once. Hope this may help you.