Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

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()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
registerForPushNotifications()
return true
}
{
"aps":{
"alert":"Testing.. (54)",
"badge":1,
"sound":"default",
"mutable-content": 1,
"attachment-url": "https://tinyurl.com/y9exh3by"
}}
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
}
// 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])
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// 1. Access the payload notification content once received
if let apnsData = response.notification.request.content.userInfo["aps"] as? [String: Any] {
// 2. Get the imaginary storeID from the payload
if let storeID = apnsData["storeID"] as? Int,
// 3. When a notification is received and your app opens up you would generally get the initial View Controller
// You can then access the InitialViewController and parse the it, and you can then access its functions.
let initialVC = self.window?.visibleViewController as? InitialViewController {
// 4. Using the initialVC function for getting a StoreViewController by a given storeID.
// The getStoreViewController could simply perform a segue programatically and navigate from the
+ 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()
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)")
}
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()
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
return true
}