OneSignal-Cordova-SDK icon indicating copy to clipboard operation
OneSignal-Cordova-SDK copied to clipboard

iOS - Notification not shown on with the app in foreground with [email protected]

Open fabioz23 opened this issue 2 years ago • 5 comments

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:

  1. setup a ionic capacitor project
  2. add onesignal push notification plugin
  3. create ios platform
  4. build and install the app on a iOS device
  5. 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 avatar May 04 '22 07:05 fabioz23

@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.

jkasten2 avatar May 07 '22 05:05 jkasten2

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 avatar May 07 '22 05:05 fabioz23

@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 avatar May 07 '22 06:05 jkasten2

@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 avatar Jul 28 '22 15:07 seb-doncker

@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 :)

Gefyaqiilah avatar Sep 21 '22 07:09 Gefyaqiilah

Hey, looks like this issue has been resolved since 2022. Closing.

nan-li avatar Dec 11 '23 04:12 nan-li