register Callback in PushNotifications Plugin Fails to Trigger Registration or Error Listeners
Plugin(s)
@capacitor/push-notifications: ^6.0.2
Capacitor Version
@capacitor/cli: ^6.1.2 @capacitor/core: ^6.1.2 @capacitor/ios: ^6.1.2 @capacitor/push-notifications: ^6.0.2
Platform(s)
iOS only
Current Behavior
When calling PushNotifications.register() on an iOS device, neither the registration nor registrationError listeners are triggered. No token or error feedback is returned, despite APNs being configured in the Apple Developer Portal, necessary permissions enabled in Xcode, and no apparent errors in the console log.
Expected Behavior
After invoking PushNotifications.register(), we expect either the registration listener to provide the device token or the registrationError listener to handle errors, allowing us to proceed with or debug APNs registration.
Code Reproduction
The following setup was used to reproduce the issue.
AppDelegate.swift: import UIKit import Capacitor
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { // default functions
// Added the functions to handle APNs registration callbacks
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
}
}
PushNotificationService.ts: import { Capacitor } from “@capacitor/core”; import { PushNotifications } from “@capacitor/push-notifications”; import axios from “axios”;
class PushNotificationService { private client = axios; private apiRoot = ‘/apns’;
public async setup() {
await this.setupListeners();
if (Capacitor.getPlatform() !== 'ios') return;
const result = await PushNotifications.requestPermissions();
console.log("RESULTS: ", result)
if (result.receive === 'granted') {
await PushNotifications.register();
}
}
private async setupListeners() {
await PushNotifications.addListener('registration', (token) => {
console.log('Token registered', token.value);
this.sendTokenToBackend(token.value);
});
await PushNotifications.addListener('registrationError', (error) => {
console.log('Registration error:', error);
});
await PushNotifications.addListener('pushNotificationReceived', (notification) => {
console.log('Push notification received:', notification);
});
await PushNotifications.addListener('pushNotificationActionPerformed', (notification) => {
console.log('Push notification action performed:', notification);
});
}
private async sendTokenToBackend(token: string) {
try {
await this.client.post(`${this.apiRoot}/token`, { token });
} catch (error) {
console.error('Error sending token:', error);
}
}
}
export default new PushNotificationService();
Other Technical Details
• Device: iPhone 14 Pro Max
• iOS Version: 18.1
• Xcode Version: 16.1
Additional Context
Testing has been conducted over USB with Xcode’s console. APNs and Push capabilities are active, and necessary permissions granted. No errors appear in the logs, but we have not observed any registration response.
Logs
⚡️ To Native -> PushNotifications addListener 62681458 ⚡️ To Native -> PushNotifications addListener 62681459 ⚡️ To Native -> PushNotifications addListener 62681460 ⚡️ To Native -> PushNotifications addListener 62681461 ⚡️ To Native -> PushNotifications requestPermissions 62681462 ⚡️ TO JS {"receive":"granted"} ⚡️ To Native -> PushNotifications register 62681463 ⚡️ [log] - RESULTS: {"receive":"granted"} ⚡️ [log] - Permission granted, registering for push notifications
Im facing the same
For me I had to add these lines here in the AppDelegate.swift https://capacitorjs.com/docs/apis/push-notifications#ios
Curious if anyone who encountered this found a fix, I'm running into the same baffling problem. I have of course added the app delegate methods on the Swift side per the docs.
Sigh, at least for me it was as simple as "register your event listeners before you call register()".