Reputation: 2242
I have an Ionic 5.x app using Capacitor 3.2.3. We're using onesignal-cordova-plugin
to send push notifications and register devices to receive push notifications.
We are able to receive notifications without any issues when the app is in the background. However, when the app is in the foreground I'm unable to receive notifications. I understand that using apps later then iOS 10 require the implementation of the UNUserNotificationCenterDelegate, but even when I have that implemented I'm still unable to receive notifications in the foreground.
I've implemented the UNUserNotificationCenterDelegate
and
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Push notification received in foreground.")
completionHandler([.alert, .sound, .badge])
}
But when I set a breakpoint this function is never called.
I'm not using any of the capacitor push notification packages.
"dependencies": {
"@angular/animations": "^12.2.6",
"@angular/cdk": "^12.2.6",
"@angular/common": "^12.2.6",
"@angular/core": "^12.2.6",
"@angular/forms": "^12.2.6",
"@angular/material": "^12.2.6",
"@angular/platform-browser": "^12.2.6",
"@angular/platform-browser-dynamic": "^12.2.6",
"@angular/router": "^12.2.6",
"@capacitor-community/apple-sign-in": "^1.0.1",
"@capacitor-community/facebook-login": "^3.1.1",
"@capacitor-community/firebase-analytics": "^1.0.0",
"@capacitor/android": "^3.2.3",
"@capacitor/app": "^1.0.3",
"@capacitor/browser": "^1.0.3",
"@capacitor/core": "^3.2.3",
"@capacitor/device": "^1.0.3",
"@capacitor/haptics": "^1.0.3",
"@capacitor/ios": "^3.2.3",
"@capacitor/keyboard": "^1.0.3",
"@capacitor/splash-screen": "^1.1.2",
"@capacitor/status-bar": "^1.0.3",
"@capacitor/storage": "^1.2.0",
"@innomobile/capacitor-branch-deep-links": "^1.0.0",
"@ionic-native/core": "^5.34.0",
"@ionic-native/in-app-purchase-2": "^5.34.0",
"@ionic-native/native-storage": "^5.34.0",
"@ionic-native/splash-screen": "^5.34.0",
"@ionic-native/status-bar": "^5.34.0",
"@ionic/angular": "^5.6.12",
"@ionic/core": "^5.6.12",
"@ionic/pwa-elements": "^1.5.1",
"@types/jquery": "^3.5.1",
"capacitor-rate-app": "^1.1.0",
"cc.fovea.cordova.purchase": "^10.5.3",
"cordova-plugin-nativestorage": "^2.3.2",
"moment": "^2.27.0",
"ngx-moment": "^4.0.1",
"onesignal-cordova-plugin": "^3.0.0",
"rxjs": "~6.5.1",
"smoothscroll-polyfill": "^0.4.4",
"swiper": "^7.0.6",
"tslib": "^2.0.0",
"uuid": "^8.1.0",
"zone.js": "~0.11.4"
},
Upvotes: 1
Views: 1085
Reputation: 35
So this is an issue with the Capacitor core library (and is still currently an issue in Capacitor v6). While Capacitor have their own plugin for handling push notifications (@capacitor/push-notifications) the core Capacitor library also has logic that handles push notifications on iOS, namely the NotificationRouter
class, which is implemented in the CABridge
class.
Because the NotificationRouter
class is not open, you can't subclass it and implement your own. However, what we can do is update the handlers for push notifications
First you need to a class that conforms to NotificationHandlerProtocol
class CustomeNotificationHandler: NotificationHandlerProtocol {
func willPresent(notification: UNNotification) -> UNNotificationPresentactionOptions {
return [.banner]
}
func didReceive(response: UNNotificationResponse) {
// Do something with the notification
}
}
Then you just need to update the Capacitor bridge to use this class instead of it's own. In the View Controller that subclass's CAPBridgeViewController
, inside the capacitorDidLoad
function, update the bridge
let notificationHanlder = CustomNotificationHandler()
override func capacitorDidLoad() {
bridge?.notificationRouter.pushNotificationHandler = notificationHandler
bridge?.notificationRouter.localNotificationHandler = notificationHandler
// Register plugins etc.
}
Upvotes: 0