0

I was using AppDelegate to ask the user about notification permission once the app starts working. then I realized it's not the best user experience. so I moved the code to a customized view controller to ask for permission only when it's required. here's my code

public static func requestNofiticationPermission(completionHanlder : @escaping (Bool) -> Void){
    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = UIApplication.shared.delegate as! AppDelegate

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]

        UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { (success, error) in
            completionHanlder(success)
        })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)
    }

    UIApplication.shared.registerForRemoteNotifications()
}

AppDelegate code

// [START receive_message]
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    handleNotification(userInfo: userInfo)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    handleNotification(userInfo: userInfo)
    completionHandler(UIBackgroundFetchResult.newData)
}

// print notification data
func handleNotification(userInfo: [AnyHashable: Any]){
    let gcmMessageIDKey = "gcm.message_id"
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")
    }

    var body : String!
    var title : String?
    if let aps = userInfo["aps"] as? [String : Any] {
        if let alert = aps["alert"] as? [String : String] {
            body = alert["body"]
            title = alert["title"]
        }
    }

    print("title: \(title)")
    print("body: \(body)")
}

as you can see, I kept message handling in AppDelegate and referred to it in code like this UIApplication.shared.delegate. for some reason, it stopped handling the messages. the functions weren't triggered

Dot Freelancer
  • 4,083
  • 3
  • 28
  • 50
  • If notification permission was already asked and u had either said allow or deny when the code was in AppDelegate calling requestAuthorization now from inner class wont show anything. You cant call requestAuthorization multiple times. If so uninstall app and re-run else pick a new simulator and run if thats not the case we can further look into the issue – Sandeep Bhandari Nov 18 '17 at 17:55
  • it shows notification window, and I click allow. but I don't receive any message even when i try to send from the firebase website. if i get permission request func back to AppDelegate it works fine. – Dot Freelancer Nov 18 '17 at 18:02
  • @SandeepBhandari please check the complete source code from here https://www.dropbox.com/sh/89vfkiqjopkfwsd/AADl5XV7iryvWoQnJziufkPPa?dl=0 – Dot Freelancer Nov 18 '17 at 18:04
  • When you tap on allow does your app delegate receives the device token and have you checked your code which will update the server with device token – Sandeep Bhandari Nov 18 '17 at 18:08
  • Checked your code, Couldn't find `didRegisterForRemoteNotificationsWithDeviceToken` Am I missing anything ? How do you expect Firebase to send you notification if you dont update Firebase with your device token? Have a look at https://stackoverflow.com/questions/37667753/ios-firebase-push-notifications-how-to-give-firebase-users-device-token-and-s – Sandeep Bhandari Nov 18 '17 at 18:14
  • @SandeepBhandari this's for segments. but for topics you don't need to do that – Dot Freelancer Nov 18 '17 at 18:26

0 Answers0