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.