173 lines
5.7 KiB
Swift
173 lines
5.7 KiB
Swift
import UIKit
|
|
import SafariServices
|
|
import UserNotifications
|
|
import Firebase
|
|
import FirebaseCore
|
|
|
|
// https://stackoverflow.com/a/41783666/1440785
|
|
|
|
class AppDelegate: UIResponder, UIApplicationDelegate {
|
|
var window: UIWindow?
|
|
let gcmMessageIDKey = "gcm.message_id"
|
|
|
|
func application(
|
|
_ application: UIApplication,
|
|
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
|
) -> Bool {
|
|
print("ApplicationDelegate didFinishLaunchingWithOptions.")
|
|
// FirebaseApp.configure() DOES NOT WORK
|
|
FirebaseConfiguration.shared.setLoggerLevel(.max)
|
|
Messaging.messaging().delegate = self
|
|
|
|
registerForPushNotifications()
|
|
UNUserNotificationCenter.current().delegate = self
|
|
|
|
// Check if launched from notification
|
|
let notificationOption = launchOptions?[.remoteNotification]
|
|
|
|
// 1
|
|
if
|
|
let notification = notificationOption as? [String: AnyObject],
|
|
let aps = notification["aps"] as? [String: AnyObject] {
|
|
print("there is a new item")
|
|
// 2
|
|
|
|
// 3
|
|
(window?.rootViewController as? UITabBarController)?.selectedIndex = 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
|
|
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
|
|
guard let aps = userInfo["aps"] as? [String: AnyObject] else {
|
|
completionHandler(.failed)
|
|
return
|
|
}
|
|
print("didReceiveRemoteNotification")
|
|
print(userInfo)
|
|
}
|
|
|
|
|
|
func application(_ application: UIApplication,
|
|
didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
|
|
print("didReceiveRemoteNotification 2")
|
|
|
|
guard let aps = userInfo["aps"] as? [String: AnyObject] else {
|
|
return
|
|
}
|
|
print(userInfo)
|
|
}
|
|
|
|
|
|
func application(
|
|
_ application: UIApplication,
|
|
didFailToRegisterForRemoteNotificationsWithError error: Error
|
|
) {
|
|
print("Failed to register: \(error)")
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
|
|
let token = tokenParts.joined()
|
|
print("Device Token: \(token)")
|
|
}
|
|
|
|
func registerForPushNotifications() {
|
|
UNUserNotificationCenter.current()
|
|
.requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, _ in
|
|
print("granted: \(granted)")
|
|
guard granted else { return }
|
|
self?.getNotificationSettings()
|
|
}
|
|
}
|
|
|
|
func getNotificationSettings() {
|
|
UNUserNotificationCenter.current().getNotificationSettings { settings in
|
|
print("Notification settings: \(settings)")
|
|
guard settings.authorizationStatus == .authorized else { return }
|
|
DispatchQueue.main.async {
|
|
UIApplication.shared.registerForRemoteNotifications()
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
// [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) {
|
|
print("willPresent")
|
|
let userInfo = notification.request.content.userInfo
|
|
|
|
// With swizzling disabled you must let Messaging know about the message, for Analytics
|
|
// Messaging.messaging().appDidReceiveMessage(userInfo)
|
|
// [START_EXCLUDE]
|
|
// Print message ID.
|
|
if let messageID = userInfo[gcmMessageIDKey] {
|
|
print("Message ID: \(messageID)")
|
|
}
|
|
// [END_EXCLUDE]
|
|
// Print full message.
|
|
print(userInfo)
|
|
|
|
// Change this to your preferred presentation option
|
|
completionHandler([[.alert, .sound]])
|
|
}
|
|
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter,
|
|
didReceive response: UNNotificationResponse,
|
|
withCompletionHandler completionHandler: @escaping () -> Void) {
|
|
print("didReceive")
|
|
let userInfo = response.notification.request.content.userInfo
|
|
|
|
// [START_EXCLUDE]
|
|
// Print message ID.
|
|
if let messageID = userInfo[gcmMessageIDKey] {
|
|
print("Message ID: \(messageID)")
|
|
}
|
|
// [END_EXCLUDE]
|
|
// With swizzling disabled you must let Messaging know about the message, for Analytics
|
|
// Messaging.messaging().appDidReceiveMessage(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: \(String(describing: fcmToken))")
|
|
|
|
let dataDict: [String: String] = ["token": fcmToken ?? ""]
|
|
NotificationCenter.default.post(
|
|
name: UserNotifications.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.
|
|
}
|
|
|
|
// [END refresh_token]
|
|
}
|