Skip to content

Instantly share code, notes, and snippets.

@drulabs
Last active August 15, 2017 15:08
Show Gist options
  • Save drulabs/fbd02525ac61336f9d34ef64235bae00 to your computer and use it in GitHub Desktop.
Save drulabs/fbd02525ac61336f9d34ef64235bae00 to your computer and use it in GitHub Desktop.
Request notification permission iOS
// 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