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

[question]: Trying to capture notification payload from OneSignal #1513

Closed
1 task done
arbyruns opened this issue Nov 5, 2024 · 4 comments
Closed
1 task done

[question]: Trying to capture notification payload from OneSignal #1513

arbyruns opened this issue Nov 5, 2024 · 4 comments

Comments

@arbyruns
Copy link

arbyruns commented Nov 5, 2024

How can we help?

I'm attempting to capture the incoming notification, but I'm unable to hit my breakpoints within UNUserNotificationCenterDelegate. How would I go about capturing the notification from OneSignal?

extension AppDelegate: UNUserNotificationCenterDelegate {
    // Called when a notification is delivered to a foreground app.
    nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        // Log the notification content
        let userInfo = notification.request.content.userInfo
        logger.info("Notification received in foreground: \(userInfo)")

        // You can also display an alert, sound, or badge
        completionHandler([.banner, .sound, .badge, .list])
    }

    // Called when a notification is opened
    nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {

        // Log the notification content when it's opened
        let userInfo = response.notification.request.content.userInfo
        logger.info("Notification opened: \(userInfo)")

        completionHandler()
    }
}

Code of Conduct

  • I agree to follow this project's Code of Conduct
@mirko-milovanovic-vidiemme
Copy link

mirko-milovanovic-vidiemme commented Nov 12, 2024

Having the same issue can't get any UNUserNotificationCenterDelegate methods to trigger.

I'm most probably missing a step when configuring the library but i can't figure out what is it

Might've figured it out, the UNUserNotificationCenter delegate must be assigned after OneSignal's initialization like so

OneSignal.initialize("oneSignalAPIKey", withLaunchOptions: launchOptions)
UNUserNotificationCenter.current().delegate = self

🤪

@arbyruns
Copy link
Author

arbyruns commented Nov 12, 2024

@mirko-milovanovic-vidiemme I was able to get it working by following https://documentation.onesignal.com/docs/mobile-sdk-reference#addforegroundlifecyclelistener.

Update: actually that appears to be only when the app is in the foreground.

@bbjay
Copy link

bbjay commented Nov 27, 2024

you have to implement a Notification Service Extension to always get the notification payload.
Example here:
https://github.com/OneSignal/OneSignal-iOS-SDK/blob/main/iOS_SDK/OneSignalDevApp/OneSignalNotificationServiceExtension/NotificationService.m

For background pushs (without a visible notification), there is an extra setup needed to get the payload.

@nan-li
Copy link
Contributor

nan-li commented Dec 5, 2024

Thanks for your suggestions from @mirko-milovanovic-vidiemme and @bbjay. I will close this issue, please ping me to re-open if the question is not resolved.

@nan-li nan-li closed this as completed Dec 5, 2024
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

4 participants