iOS端Capacitor推送通知调用错误注册回调问题排查
iOS模拟器推送通知回调异常问题
我在Angular-Ionic应用的welcome-page.component.ts的ngOnInit中调用了如下代码:
if (Capacitor.platform !== "web") { this.registerPush(); }
registerPush方法实现如下:
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]); } ); }
该实现完全遵循官方文档,在Android模拟器和真机上均可正常工作,能获取设备Token并注册到PostgreSQL数据库,以便通过API发送通知。但在iOS模拟器上无法正常运行,出现回调不匹配的问题:我创建的registration回调标识为#11279332,而实际触发的是#11279336(Android端两者标识一致)。
我已完成以下配置步骤:
- 参考Capacitor和Ionic官方推送文档,安装
@capacitor/push-notifications并执行npx cap sync; - 创建Firebase项目,添加
GoogleService-Info.plist到Xcode,将Apple AuthKey添加到Firebase; - 在Podfile中添加
pod 'Firebase/Messaging',执行npx cap update ios; - 在
AppDelegate.swift中添加import Firebase、FirebaseApp.configure()及以下两个方法:
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) }
- 执行
ionic build、npx cap copy,通过npx cap open ios打开项目; - 创建包含推送通知权限的Apple配置文件并导入Xcode,在Xcode中启用推送通知能力。
我认为已完成所有必要步骤,请问哪里出现了问题?
内容的提问来源于stack exchange,提问作者Gabriel
相关产品推荐
相关产品推荐

