so, in ngOnInit of the welcome-page.component.ts of my angular-ionic app i’m calling this:
if (Capacitor.platform !== "web") { this.registerPush(); }
which is:
`private async registerPush() {
if (Capacitor.getPlatform() === “android”) {
let permStatus = await PushNotifications.checkPermissions();
if (permStatus.receive === "prompt") {
permStatus = await PushNotifications.requestPermissions();
}
if (permStatus.receive !== "granted") {
throw new Error("User denied permissions!");
}
await PushNotifications.register();
} else if (Capacitor.getPlatform() === "ios") {
PushNotifications.requestPermissions().then((result) => {
if (result.receive === "granted") {
PushNotifications.register();
} else {
console.error("User denied push notification permissions!");
}
});
}
PushNotifications.addListener("registration", async (token) => {
console.log("My token: " + token.value);
const user = await this.authService.getUserInSession();
const decryptUser = Globals.decryptOjbectsInSession(user);
this.authService
.updateRegKey({
cpf_cnpj: decryptUser.usuario.cpfcnpj,
reg_key: token.value,
})
.subscribe((response: any) => {
console.log("response: ", response);
});
});
PushNotifications.addListener("registrationError", (error) => {
console.log("Error on registration: " + JSON.stringify(error));
});
PushNotifications.addListener(
"pushNotificationReceived",
(notification) => {
console.log("Push notification received: ", notification);
}
);
PushNotifications.addListener(
"pushNotificationActionPerformed",
(notification) => {
console.log(
"Push notification action performed",
notification.actionId,
notification.inputValue
);
this.router.navigate([notification.notification.data.url]);
}
);
}`
and i believe this is exactly done as in the documentation is said to. and it DOES work on android, both simulator and physical device, it takes the device token and then registers on my postgres db, so that i can use API to send the notification.
enter image description here
BUT, for iOS simulator (i don’t have a physical one to test) it simply doesn’t work.
ios add listeners
ios doesnt call the right callback
and i don’t know how this works exactly, but it looks like it is calling another callback. see, the registration callback that i have created, is apparently the “#11279332” one, and when it’s called, the one that’s called is the “#11279336” one. on Android, they’re the same (#102964981)
i tried following both documentations that i could find:
https://capacitorjs.com/docs/guides/push-notifications-firebase
https://ionicframework.com/docs/native/push-notifications
installed capacitor/push-notifications and used the command “npx cap sync”
created the firebase project, got the GoogleService-Info.plist, added in xCode like it is asked to, and i also added the authkey from apple to the firebase project
added the “pod Firebase/Messaging'” to the Pods file and updated using “npx cap update ios”
also added the “import Firebase” and the “FirebaseApp.configure()” to the AppDelegate.swift, and both functions:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
Messaging.messaging().token(completion: { (token, error) in
if let error = error {
NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
} else if let token = token {
NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: token)
}
})
}`
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error) }
i’ve used the commands: “ionic build” and “npx cap copy”, and then opened the vscode with “npx cap open ios”
i’ve created the apple profile with the push-notifications permission and uploaded it to xcode,
ios profile
enter image description here
i also enabled the push-notifications capability inside vscode.
there are a lot of steps, and i believe i have done them all. can someone tell me what am i doing wrong?