OneSignal-Cordova-SDK
OneSignal-Cordova-SDK copied to clipboard
iOS - Notification not shown on with the app in foreground with [email protected]
Description:
After project upgrade (from [email protected], [email protected] to [email protected], [email protected]) Onesignal push notification not shown when the app is in foreground. This happens despite having configured the proper handler for foreground notifications like Onesignal wiki suggest.
Environment
@capacitor/cli: 3.4.3 @capacitor/android: 3.4.3 @capacitor/core: 3.4.3 @capacitor/ios: 3.4.3
Steps to Reproduce Issue:
- setup a ionic capacitor project
- add onesignal push notification plugin
- create ios platform
- build and install the app on a iOS device
- send a test notification with the app in foreground
Anything else:
My workaround is to forcibly disable handleApplicationNotifications member of NotificationRouter class:
import Foundation
@objc(CAPNotificationRouter) public class NotificationRouter: NSObject, UNUserNotificationCenterDelegate {
/*var handleApplicationNotifications: Bool {
get {
return UNUserNotificationCenter.current().delegate === self
}
set {
let center = UNUserNotificationCenter.current()
if newValue {
center.delegate = self
} else if center.delegate === self {
center.delegate = nil
}
}
}*/
var handleApplicationNotifications: Bool = false
...
...
But with this ugly workaround doesn't reset the unread notification badge.
@fabioz23 Thanks for reporting, these symptoms sounds like Capacitor may have it's own UNUserNotificationCenterDelegate
and is preventing OneSignal from handling the notification. So if there was not other notification code or libraries in your upgrade from [email protected]
to [email protected]
it seems they may have changed something here OneSignal needs to look into.
i've tried to open an issue firstly on capacitor repo but i don't have received an answer yet.
https://github.com/ionic-team/capacitor/issues/5542
@fabioz23 I took a quick look, found a possible setting on Capacitor's end https://github.com/ionic-team/capacitor/issues/5542#issuecomment-1120141948.
The root of the issue is both Capacitor and OneSignal both handle the userNotificationCenter:willPresent:withCompletionHandler
event, however only one can call the completionHandler
that determines if the notification will show or not in the foreground. I think it is the first one to call it wins.
@jkasten2 I can confirm your description. Capacitor callback take the lead to OneSignal one for the return value.
So, when we use OneSignal plugin we have to disable the handleApplicationNotifications options in Capacitor configuration.
Unhappily, this option is not configurable from the capacitor.config.js file.
To do that I had to do 2 things :
In /Capacitor/CAPInstanceDescriptor.swift
: adding this code to func _parseConfiguration
:
...
if let config = config {
...
if let handleApplicationNotifs = config[keyPath: "handleApplicationNotifications"] as? Bool {
handleApplicationNotifications = handleApplicationNotifs
}
And in /project/capacitor.config.ts
, adding :
const config: CapacitorConfig = {
...
handleApplicationNotifications: false
};
There is only one TS issue with the type CapacitorConfig
which do not contain the definition for handleApplicationNotifications
options (could be "fixed" with // @ts-ignore
)
@seb-doncker i followed your suggestion, and i think i'll need Patch-Package to edit capacitor code everytime the dependency is installed. i appreciate your help, thanks a lot :)
Hey, looks like this issue has been resolved since 2022. Closing.