This file contains hidden or 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
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
FirebaseApp.configure() | |
return true | |
} |
This file contains hidden or 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
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | |
if let apnsData = response.notification.request.content.userInfo["aps"] as? [String: Any] { | |
if let googleURL = apnsData["googleURL"] as? String { | |
UIApplication.shared.openURL(URL(string: googleURL)!) | |
} | |
completionHandler() | |
} | |
} |
This file contains hidden or 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
+ func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | |
+ if response.actionIdentifier == "like" { | |
+ print("Handle like action identifier") | |
+ } else if response.actionIdentifier == "save" { | |
+ print("Handle save action identifier") | |
+ } else { | |
+ print("No custom action identifiers chosen") | |
+ } | |
+ // Make sure completionHandler method is at the bottom of this func | |
+ completionHandler() |
This file contains hidden or 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
// 6. Add some actions and add them in a category which is then added to the notificationCategories | |
let likeAction = UNNotificationAction(identifier: "like", title: "Like", options: []) | |
let saveAction = UNNotificationAction(identifier: "save", title: "Save", options: []) | |
let category = UNNotificationCategory(identifier: "unicorning", actions: [likeAction, saveAction], intentIdentifiers: [], options: []) | |
UNUserNotificationCenter.current().setNotificationCategories([category]) |
This file contains hidden or 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
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { | |
self.contentHandler = contentHandler | |
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) | |
guard let bestAttemptContent = bestAttemptContent, // 1. Make sure bestAttemptContent is not nil | |
let apsData = bestAttemptContent.userInfo["aps"] as? [String: Any], // 2. Dig in the payload to get the | |
let attachmentURLAsString = apsData["attachment-url"] as? String, // 3. The attachment-url | |
let attachmentURL = URL(string: attachmentURLAsString) else { // 4. And parse it to URL | |
return | |
} |
This file contains hidden or 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
{ | |
"aps":{ | |
"alert":"Testing.. (54)", | |
"badge":1, | |
"sound":"default", | |
"mutable-content": 1, | |
"attachment-url": "https://tinyurl.com/y9exh3by" | |
}} |
This file contains hidden or 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
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { | |
// 1. Convert device token to string | |
let tokenParts = deviceToken.map { data -> String in | |
return String(format: "%02.2hhx", data) | |
} | |
let token = tokenParts.joined() | |
// 2. Print device token to use for PNs payloads | |
print("Device Token: \(token)") | |
} |
This file contains hidden or 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
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
registerForPushNotifications() | |
return true | |
} |
This file contains hidden or 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
func registerForPushNotifications() { | |
UNUserNotificationCenter.current().delegate = self | |
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { | |
(granted, error) in | |
print("Permission granted: \(granted)") | |
// 1. Check if permission granted | |
guard granted else { return } | |
// 2. Attempt registration for remote notifications on the main thread | |
DispatchQueue.main.async { | |
UIApplication.shared.registerForRemoteNotifications() |