Last active
December 17, 2018 05:57
-
-
Save roytornado/8296d9511c422c67d0a828dec385e488 to your computer and use it in GitHub Desktop.
iOS Remote Notification
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
import UIKit | |
import Firebase | |
import UserNotifications | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate } | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
FirebaseApp.configure() | |
application.registerForRemoteNotifications() | |
requestNotificationAuthorization(application: application) | |
if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] { | |
NSLog("[RemoteNotification] applicationState: \(applicationStateString) didFinishLaunchingWithOptions for iOS9: \(userInfo)") | |
//TODO: Handle background notification | |
} | |
return true | |
} | |
var applicationStateString: String { | |
if UIApplication.shared.applicationState == .active { | |
return "active" | |
} else if UIApplication.shared.applicationState == .background { | |
return "background" | |
}else { | |
return "inactive" | |
} | |
} | |
func requestNotificationAuthorization(application: UIApplication) { | |
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) | |
} | |
} | |
} | |
@available(iOS 10, *) | |
extension AppDelegate : UNUserNotificationCenterDelegate { | |
// iOS10+, called when presenting notification in foreground | |
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | |
let userInfo = notification.request.content.userInfo | |
NSLog("[UserNotificationCenter] applicationState: \(applicationStateString) willPresentNotification: \(userInfo)") | |
//TODO: Handle foreground notification | |
completionHandler([.alert]) | |
} | |
// iOS10+, called when received response (default open, dismiss or custom action) for a notification | |
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | |
let userInfo = response.notification.request.content.userInfo | |
NSLog("[UserNotificationCenter] applicationState: \(applicationStateString) didReceiveResponse: \(userInfo)") | |
//TODO: Handle background notification | |
completionHandler() | |
} | |
} | |
extension AppDelegate : MessagingDelegate { | |
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { | |
NSLog("[RemoteNotification] didRefreshRegistrationToken: \(fcmToken)") | |
} | |
// iOS9, called when presenting notification in foreground | |
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { | |
NSLog("[RemoteNotification] applicationState: \(applicationStateString) didReceiveRemoteNotification for iOS9: \(userInfo)") | |
if UIApplication.shared.applicationState == .active { | |
//TODO: Handle foreground notification | |
} else { | |
//TODO: Handle background notification | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment