Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fabioz23 opened this issue May 4, 2022 · 6 comments

Comments

@fabioz23
Copy link

fabioz23 commented May 4, 2022

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.

@jkasten2
Copy link
Member

jkasten2 commented May 7, 2022

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

@fabioz23
Copy link
Author

fabioz23 commented May 7, 2022

i've tried to open an issue firstly on capacitor repo but i don't have received an answer yet.

ionic-team/capacitor#5542

@jkasten2
Copy link
Member

jkasten2 commented May 7, 2022

@fabioz23 I took a quick look, found a possible setting on Capacitor's end ionic-team/capacitor#5542 (comment).

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 jkasten2 changed the title notification not shown on iOS with the app in foreground iOS - Notification not shown on with the app in foreground with [email protected] May 7, 2022
@jkasten2 jkasten2 transferred this issue from OneSignal/OneSignal-iOS-SDK May 7, 2022
@seb-doncker
Copy link

seb-doncker commented Jul 28, 2022

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

@Gefyaqiilah
Copy link

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

@nan-li
Copy link
Contributor

nan-li commented Dec 11, 2023

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

@nan-li nan-li closed this as completed Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants