Last active
May 11, 2020 23:47
-
-
Save NickHung1982/802b6e40222152f4af83aea2b47b7573 to your computer and use it in GitHub Desktop.
AppDelegate.swift
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
// | |
// AppDelegate.swift | |
// CryptoCurrencyPayment | |
// | |
// Created by Nick on 4/16/20. | |
// Copyright © 2020 Nick. All rights reserved. | |
// | |
import UIKit | |
import Firebase | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
let gcmMessageIDKey = "gcm.message_id" | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
// Override point for customization after application launch. | |
self.window = UIWindow(frame: UIScreen.main.bounds) | |
self.window?.makeKeyAndVisible() | |
FirebaseApp.configure() | |
// [START set_messaging_delegate] | |
Messaging.messaging().delegate = self | |
// [END set_messaging_delegate] | |
// [START register_for_notifications] | |
//Register for remote notifications | |
if #available(iOS 10.0, *) { | |
// For iOS 10 display notification (sent via APNS) | |
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() | |
// [END register_for_notifications] | |
return true | |
} | |
// MARK: UISceneSession Lifecycle | |
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { | |
// Called when a new scene session is being created. | |
// Use this method to select a configuration to create the new scene with. | |
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) | |
} | |
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { | |
// Called when the user discards a scene session. | |
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. | |
// Use this method to release any resources that were specific to the discarded scenes, as they will not return. | |
} | |
// [START receive_message] | |
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { | |
// If you are receiving a notification message while your app is in the background, | |
// this callback will not be fired till the user taps on the notification launching the application. | |
// TODO: Handle data of notification | |
// With swizzling disabled you must let Messaging know about the message, for Analytics | |
Messaging.messaging().appDidReceiveMessage(userInfo) | |
// Print message ID. | |
if let messageID = userInfo[gcmMessageIDKey] { | |
print("Message ID: \(messageID)") | |
} | |
NotificationCenter.default.post(name: .didReceiveMoney, object: nil, userInfo: userInfo) | |
// Print full message. | |
print(userInfo) | |
} | |
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], | |
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { | |
// If you are receiving a notification message while your app is in the background, | |
// this callback will not be fired till the user taps on the notification launching the application. | |
// TODO: Handle data of notification | |
// With swizzling disabled you must let Messaging know about the message, for Analytics | |
Messaging.messaging().appDidReceiveMessage(userInfo) | |
// Print message ID. | |
if let messageID = userInfo[gcmMessageIDKey] { | |
print("Message ID: \(messageID)") | |
} | |
NotificationCenter.default.post(name: .didReceiveMoney, object: nil, userInfo: userInfo) | |
// Print full message. | |
print(userInfo) | |
completionHandler(UIBackgroundFetchResult.newData) | |
} | |
// [END receive_message] | |
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { | |
print("Unable to register for remote notifications: \(error.localizedDescription)") | |
} | |
// This function is added here only for debugging purposes, and can be removed if swizzling is enabled. | |
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to | |
// the FCM registration token. | |
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { | |
print("APNs token retrieved: \(deviceToken)") | |
// With swizzling disabled you must set the APNs token here. | |
// Messaging.messaging().apnsToken = deviceToken | |
} | |
} | |
// [START ios_10_message_handling] | |
@available(iOS 10, *) | |
extension AppDelegate : UNUserNotificationCenterDelegate { | |
// 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 | |
// With swizzling disabled you must let Messaging know about the message, for Analytics | |
// Messaging.messaging().appDidReceiveMessage(userInfo) | |
// Print message ID. | |
if let messageID = userInfo[gcmMessageIDKey] { | |
print("Message ID: \(messageID)") | |
} | |
NotificationCenter.default.post(name: .didReceiveMoney, object: nil, userInfo: userInfo) | |
// Print full message. | |
print(userInfo) | |
// Change this to your preferred presentation option | |
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)") | |
} | |
NotificationCenter.default.post(name: .didReceiveMoney, object: nil, userInfo: userInfo) | |
// Print full message. | |
print(userInfo) | |
completionHandler() | |
} | |
} | |
// [END ios_10_message_handling] | |
extension AppDelegate : MessagingDelegate { | |
// [START refresh_token] | |
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { | |
print("Firebase registration token: \(fcmToken)") | |
// let dataDict:[String: String] = ["token": fcmToken] | |
// NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict) | |
// TODO: If necessary send token to application server. | |
// Note: This callback is fired at each app startup and whenever a new token is generated. | |
let defaults = UserDefaults.standard | |
defaults.set(fcmToken, forKey: "fcmToken") | |
} | |
// [END refresh_token] | |
// [START ios_10_data_message] | |
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground. | |
// To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true. | |
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { | |
print("Received data message: \(remoteMessage.appData)") | |
} | |
// [END ios_10_data_message] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment