Hello,
I have used @ionic-native/fcm/ngx plugin in my Ionic application and the code for the same is as mentioned below:
import { FCM } from '@ionic-native/fcm/ngx';
constructor(private fcm: FCM) {this.initializeApp();}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.getCurrentLocation()
this.fcm.subscribeToTopic('marketing');
this.initializeFirebase();
//this.firebaseData()
// this.presentModal()
//this.saveToken();
this.fcm.onNotification().subscribe((response) => {
console.log('Notification received: ' + JSON.stringify(response));
if(response.wasTapped){
// Received while app in background (this should be the callback when a system notification is tapped)
// This is empty for our app since we just needed the notification to open the app
console.log("background---",response);
localStorage.setItem("forgroundNotification", "true");
} else {
// received while app in foreground (show a toast)
console.log("foreground---",response);
}
});
});
//this.saveToken();
// firebase.initializeApp(config);
}
initializeFirebase() {
console.log("initializeFirebase")
if(this.platform.is("android")) {
// this.fcm.subscribe("all");
this.initializeFirebaseAndroid()
}
if(this.platform.is("ios")) {
// this.fcm.subscribe("all");
this.initializeFirebaseIOS();
}
}
initializeFirebaseAndroid() {
console.log("initializeFirebaseAndroid")
this.fcm.getToken().then(token => {
console.log(`The getToken is ${token}`)
localStorage.setItem("FirebaseToekn", token)
this.uniqueDeviceID.get()
.then((uuid: any) => {
console.log(uuid)
this.ideaService.saveTokenToFirestore(token, 'Android', uuid)
})
.catch((error: any) => console.log(error));
});
this.fcm.onTokenRefresh().subscribe(token => {
console.log(`The onTokenRefresh is ${token}`)
localStorage.setItem("FirebaseToekn", token)
})
// this.subscribeToPushNotifications();
}
initializeFirebaseIOS() {
this.fcm.getToken().then(token => {
console.log(`The getToken is ${token}`)
this.uniqueDeviceID.get()
.then((uuid: any) => {
console.log(uuid)
this.ideaService.saveTokenToFirestore(token, 'ios', uuid)
})
.catch((error: any) => console.log(error));
});
// this.subscribeToPushNotifications();
}This code is used in two applications. The Android version of the app works perfectly fine for both the applications.
The issue is with iOS. In one application, I am not receiving the push notification when the application is in background mode and in another application I am not receiving it when in foreground mode. The server side logic is also same for both. I have tried multiple solutions but none of them worked. Require your help!