SDK Notification Event Handlers

Handling Notification Events within OneSignal

The Notification Events on this page require the Major Release Versions of the OneSignal SDK.

Notification EventsDetails
Foreground Notification Received EventOneSignal SDK setNotificationWillShowInForegroundHandler method runs before displaying a notification while the app is in focus.

Use this handler to decide if the notification should show or not.
- Android Reference
- iOS Reference
Background Notification Received EventNative Methods that run while a notification is received while app is in the background.

These methods require using Native Code like Java/Kotlin or Swift/Objective-C

- Android Reference
- iOS Reference
Force-quit Notification Received EventiOS - Force-quit state is when the app has been "swiped away" and not running in the foreground or background. Apple still keeps an open connection to your app but requires the Service Extensions for notification data detection.

Android - Force-quit generally happens manually through the App Settings and prevents any communication to your app. Further, some OEMs will put your app into this force-quit state when swiping the app away. See Notifications Not Shown for more details.
Notification Opened EventOneSignal SDK setNotificationOpenedHandler method runs upon opening the app after a notification is clicked.

- Android Reference
- iOS Reference

Foreground Notification Received Event

setNotificationWillShowInForegroundHandler Method

Runs before displaying a notification while the app is in focus. Use this handler to decide if the notification should show or not.

Note: this runs after the Notification Service Extension which can be used to modify the notification before showing it.

OneSiganl.setNotificationWillShowInForegroundHandler(new NotificationWillShowInForegroundHandler() {
   void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {
     OSNotification notification = notificationReceivedEvent.getNotification();
     // Get custom additional data you sent with the notification
     JSONObject data = notification.getAdditionalData();

     if (/* some condition */ ) {
        // Complete with a notification means it will show
     else {
       // Complete with null means don't show a notification.

OSNotificationReceivedEvent methods:

Method controlling notification completion from the handler. If this is not called at the end of the notificationWillShowInForeground implementation, a runnable will fire after a 25 second timer and complete automatically.

- Display: pass the OSNotification object
- Omit: pass null to omit displaying
The notification the device received.

See OSNotification for more details.

Background Notification Received Event

Android Background Notification Received Event

iOS Background Notification Received Event

application(_:didReceiveRemoteNotification:fetchCompletionHandler:) method

Apple provides this method to indicate a notification was received when your app is running in the foreground or background. This method allows data to be fetched while the app is running in the background. See details and discussion for requirements in Apple's Developer Documentation.

If your app is force-quite, this method will not run and requires the Service Extensions for detection.

Must have background mode enabled and send the push with content_available in the iOS Message Settings for method to be called while app is in background.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print("iOS Native didReceiveRemoteNotification: ", userInfo.debugDescription)
    if let customOSPayload = userInfo["custom"] as? NSDictionary {
        if let additionalData = customOSPayload["a"] as? NSDictionary {
            print("additionalData: ", additionalData)
            if let foo = additionalData["foo"] {
                print("foo: ", foo)
        if let notificationId = customOSPayload["i"] {
            print("notificationId: ", notificationId)
        if let launchUrl = customOSPayload["u"] {
            print("launchUrl: ", launchUrl)
    if let aps = userInfo["aps"] as? NSDictionary {
        if let alert = aps["alert"] as? NSDictionary {
            if let messageBody = alert["body"] {
                print("messageBody: ", messageBody)
            if let messageTitle = alert["title"] {
                print("messageTitle: ", messageTitle)
    // This block gets called when the user reacts to a notification received
    let timeInterval = Int(NSDate().timeIntervalSince1970)
    OneSignal.sendTags(["last_push_received": timeInterval])
    print("badge number: ", UIApplication.shared.applicationIconBadgeNumber.description)