I use ionic in order to create an app for iOS and Android. I use push notifications in ionic over google Firebase (instructions got from this tutorial https://ionicframework.com/docs/native/push-notifications) This works really fine with Android, this callback is invoked and I store the token in the backend with this code:
PushNotifications.addListener('registration',
(token: Token) => {
console.debug('token is: ' + token.value + ', user.mobileToken is: ' + user.mobileToken + ' in PushNotifications.addListener#registration');
if(user.mobileToken != null && user.mobileToken != token.value) {
this.authService.getUser().then(response => {
user.mobileToken = token.value;
this.userService.updateUser(user).subscribe(response => {
const updatedUser = response;
const message = 'mobile token is stored in backend for user ' + user.title + ' ' + user.firstname + ' ' + user.surname;
this.toastService.presentToast('bottom', message, 500);
console.debug(message + ' in PushNotifications.addListener#registration');
});
}, error => {
console.log(error);
});
}
}
);
For iOS it is obviously needed to add the following in Xcode- AppDelegate.swift:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken)
print("AppDelegate#application first:", deviceToken.map { String(format: "%02x", $0) }.joined())
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
}
The output I get in the Xcode console is AppDelegate#application first:
639f6c0f86e74db0aaab6889832ad040f00e9e590319a513bacc1579a968f954
This token is the same token like above I get with PushNotifications.addListener('registration', ...
Further this tutorial https://firebase.google.com/docs/cloud-messaging/ios/receive?hl=de mentions that the following code has to be added in AppDelegate.swift:
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
print("Firebase registration token: \(String(describing: fcmToken))")
let dataDict: [String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(
name: Notification.Name("FCMToken"),
object: nil,
userInfo: dataDict
)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
}
So with this code a token is generated too which is e.g.
fcPGyQDykEP7nvC5k6-JIJ:APA91bEwQ7biKli9LaP4NrB_s3aqzD2iKw7wXc58_OrrN_m3v5zlFjFcWiFX9CUqij-JOppOKwkuh-g60lETrHdt7I9o2yHJXMT8crp7
So the generated tokens are completely different and only the second FCN token works, the first token above does NOT work. My question now would be how to solve this issue. Do I really have to implement to store the second token in the backend again?
Your first code snippet gets an ID token (from this.authService.getUser()
) that identifies the current user.
Your last code snippet gets an FCM device token (from messaging... didReceiveRegistrationToken
) that identifies the device.
These tokens have different meanings and goals, and can't be used interchangeably.
If you want to get the token in JavaScript, use the process describes in the Firebase Cloud Messaging documentation to access the registration token. From a quick search on ionic get fcm token it seems you can also get this in Ionic by calling the getToken()
function of the FCMPlugin
.