Skip to content

Instantly share code, notes, and snippets.

@amixpal
Created December 29, 2017 08:55
Show Gist options
  • Save amixpal/6db0a77ab27789b411cc67cebd160e63 to your computer and use it in GitHub Desktop.
Save amixpal/6db0a77ab27789b411cc67cebd160e63 to your computer and use it in GitHub Desktop.
extension AppDelegate {
static func registerForNotification(application: UIApplication) {
if #available(iOS 10.0, *) {
let acceptAction = UNNotificationAction(identifier: acceptActionIdentifier, title: acceptTitle,
options: [.foreground])
UNUserNotificationCenter.current().setNotificationCategories([guideCategory, touristCategory, guideStartTourCategory, touristFinishCategory, touristCancelTourCategory, guideCancelTourCategory])
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization (
options: authOptions,
completionHandler: {_, _ in
})
}else {
let acceptAction = UIMutableUserNotificationAction()
acceptAction.identifier = acceptActionIdentifier
acceptAction.title = acceptTitle
acceptAction.activationMode = UIUserNotificationActivationMode.foreground
acceptAction.isAuthenticationRequired = true
acceptAction.isDestructive = false
let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound],
categories: [guideCategory, touristCategory, guideStartTourCategory, touristFinishCategory])
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
processNotificationInfo(userInfo: userInfo as! [String : Any])
}
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
}
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
processNotificationInfo(userInfo: userInfo as! [String : Any])
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if (response.actionIdentifier == acceptActionIdentifier) {
var tripInfo = userInfo
tripInfo[apiParametersConstant.STATUS] = acceptActionIdentifier
UserDefaults.standard.set(tripInfo, forKey: apiParametersConstant.TRIP_INFO)
UserDefaults.standard.synchronize()
NotificationCenter.default.post(name: Notification.Name.Guided.GuideInvitationAccepted,
object: nil, userInfo: tripInfo)
}
else if (response.actionIdentifier == rejectedActionIdentifier) {
}
else {
processNotificationInfo(userInfo: userInfo as! [String : Any])
}
completionHandler()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment