Last active
August 15, 2017 15:08
-
-
Save drulabs/fbd02525ac61336f9d34ef64235bae00 to your computer and use it in GitHub Desktop.
Request notification permission iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// displaying only relevant content not all the functions | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
// other stuff of didFinishLaunchingWithOptions inside app delegate | |
if #available(iOS 10.0, *) { | |
UNUserNotificationCenter.current().delegate = self | |
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] | |
UNUserNotificationCenter.current().requestAuthorization( | |
options: authOptions, | |
completionHandler: {_, _ in }) | |
} else { | |
let settings: UIUserNotificationSettings = | |
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) | |
application.registerUserNotificationSettings(settings) | |
application.registerForRemoteNotifications() | |
} | |
FirebaseApp.configure() | |
NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification(notification:)), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil) | |
} | |
func tokenRefreshNotification(notification: NSNotification) { | |
let refreshedToken = InstanceID.instanceID().token() | |
print("Dhruw: Connected to FCM. Token : \(String(describing: refreshedToken))") | |
connectToFCM() | |
} | |
func connectToFCM() { | |
Messaging.messaging().shouldEstablishDirectChannel = true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment