Onesignal-ios-sdk: When app has closed notificationConfiguration and OSHandleNotificationActionBlock method not call

Created on 7 Jun 2017  路  5Comments  路  Source: OneSignal/OneSignal-iOS-SDK

Hello
when app in background OneSignal works correctly but when app is closed i get push notification but when tap on it
let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in }
not call.

Most helpful comment

Solved: i found the FK problem
That is need a some delay seconds for it , see this:

let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
            // This block gets called when the user reacts to a notification received
            let payload: OSNotificationPayload = result!.notification.payload

            print("Payload contains all of properties in notif is : " , payload)
            print("Payload additionalData : " , payload.additionalData)

            let when = DispatchTime.now() + 2 
            DispatchQueue.main.asyncAfter(deadline: when) {

                //Go to messages
                NotificationCenter.default.post(name: Notification.Name("goToMessagesVCPushNotifNotif"), object: nil)


           }



        }

All 5 comments

@reza-khalafi Make sure you are calling OneSignal.initWithLaunchOptions from within didFinishLaunchingWithOptions. If it is called later OneSignal won't be able to get the open even from iOS.

This is my app delegate file:
@jkasten2

import UIKit
import OneSignal
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate , OSPermissionObserver , OSSubscriptionObserver{
var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


        let notificationReceivedBlock: OSHandleNotificationReceivedBlock = { notification in
            print("Received Notification: \(notification!.payload.notificationID)")

        }

        let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
            // This block gets called when the user reacts to a notification received
            let payload: OSNotificationPayload = result!.notification.payload

            print("Payload contains all of properties in notif is : " , payload)
            print("Payload additionalData : " , payload.additionalData)


            //Go to messages
            NotificationCenter.default.post(name: Notification.Name("goToMessagesVCPushNotifNotif"), object: nil)

        }


        let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false,kOSSettingsKeyInAppLaunchURL: true]

        OneSignal.initWithLaunchOptions(launchOptions,
                                        appId: "f64b1808-1367-4598-a9df-c942474be441",
                                        handleNotificationReceived: notificationReceivedBlock,
                                        handleNotificationAction: notificationOpenedBlock,
                                        settings: onesignalInitSettings)

        OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification
        // Add your AppDelegate as an obsserver
        OneSignal.add(self as OSPermissionObserver)
        OneSignal.add(self as OSSubscriptionObserver)
        logPushNotificationStatuses()

        OneSignal.promptForPushNotifications { accepted in
            print("User accepted notifications: \(accepted)")
        }
return true
}
// Add this new method
    func onOSPermissionChanged(_ stateChanges: OSPermissionStateChanges!) {

        // Example of detecting answering the permission prompt
        if stateChanges.from.status == OSNotificationPermission.notDetermined {
            if stateChanges.to.status == OSNotificationPermission.authorized {
                print("Thanks for accepting notifications!")
            } else if stateChanges.to.status == OSNotificationPermission.denied {
                print("Notifications not accepted. You can turn them on later under your iOS settings.")
            }
        }
        // prints out all properties
        print("PermissionStateChanges: \n\(stateChanges)")
    }
func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
        if !stateChanges.from.subscribed && stateChanges.to.subscribed {
            print("Subscribed for OneSignal push notifications!")
        }
        print("SubscriptionStateChange: \n\(stateChanges)")

        guard let userId:String = stateChanges.from.userId else{
            return
        }
        print("My user id is: \n" , userId)

    }
func logPushNotificationStatuses() {
        let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
        let hasPrompted = status.permissionStatus.hasPrompted
        print("hasPrompted = \(hasPrompted)")
        let userStatus = status.permissionStatus.status
        print("userStatus = \(userStatus)")
        let isSubscribed = status.subscriptionStatus.subscribed
        print("isSubscribed = \(isSubscribed)")
        let userSubscriptionSetting = status.subscriptionStatus.userSubscriptionSetting
        print("userSubscriptionSetting = \(userSubscriptionSetting)")
        let userID = status.subscriptionStatus.userId
        print("userID = \(String(describing: userID))")
        let pushToken = status.subscriptionStatus.pushToken
        print("pushToken = \(String(describing: pushToken))")
    }

Solved: i found the FK problem
That is need a some delay seconds for it , see this:

let notificationOpenedBlock: OSHandleNotificationActionBlock = { result in
            // This block gets called when the user reacts to a notification received
            let payload: OSNotificationPayload = result!.notification.payload

            print("Payload contains all of properties in notif is : " , payload)
            print("Payload additionalData : " , payload.additionalData)

            let when = DispatchTime.now() + 2 
            DispatchQueue.main.asyncAfter(deadline: when) {

                //Go to messages
                NotificationCenter.default.post(name: Notification.Name("goToMessagesVCPushNotifNotif"), object: nil)


           }



        }

@reza-khalafi Thanks for the details. The OSHandleNotificationActionBlock will fire right away which is probably happening before the receiver of your "goToMessagesVCPushNotifNotif" message is hook up. Instead of adding a delay I would recommend setting a variable here. Then check it when you hook up the receiver. This will be less fragile than a timer.

Worked for me without any delay.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chmiiller picture chmiiller  路  6Comments

sabiland picture sabiland  路  6Comments

cassidyclawson picture cassidyclawson  路  5Comments

torben-erz picture torben-erz  路  3Comments

halilyuce picture halilyuce  路  3Comments