Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var window: UIWindow?
- let gcmMessageIDKey = "gcm.message_id"
- static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate }
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
- FirebaseApp.configure()
- Messaging.messaging().delegate = self
- 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()
- let token = Messaging.messaging().fcmToken
- print("FCM token: (token ?? "")")
- return true
- }
- func application(received remoteMessage: MessagingRemoteMessage) {
- print(remoteMessage.appData)
- }
- func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
- if let messageID = userInfo[gcmMessageIDKey] {
- print("Message ID: (messageID)")
- }
- // Print full message.
- print(userInfo)
- }
- func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
- fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
- if let messageID = userInfo[gcmMessageIDKey] {
- print("Message ID: (messageID)")
- }
- print(userInfo)
- completionHandler(UIBackgroundFetchResult.newData)
- }
- func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
- print("Unable to register for remote notifications: (error.localizedDescription)")
- }
- func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
- Messaging.messaging().apnsToken = deviceToken
- }
- // Receive displayed notifications for iOS 10 devices.
- func userNotificationCenter(_ center: UNUserNotificationCenter,
- willPresent notification: UNNotification,
- withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
- let userInfo = notification.request.content.userInfo
- if let messageID = userInfo[gcmMessageIDKey] {
- print("Message ID: (messageID)")
- }
- print(userInfo)
- completionHandler([])
- }
- func userNotificationCenter(_ center: UNUserNotificationCenter,
- didReceive response: UNNotificationResponse,
- withCompletionHandler completionHandler: @escaping () -> Void) {
- let userInfo = response.notification.request.content.userInfo
- // Print message ID.
- if let messageID = userInfo[gcmMessageIDKey] {
- print("Message ID: (messageID)")
- }
- print(userInfo)
- completionHandler()
- }
- func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
- print("Received data message: (remoteMessage.appData)")
- }
Add Comment
Please, Sign In to add comment