Created
December 12, 2018 11:05
-
-
Save hemangshah/0b19d796ea5e46abcb4702db96195321 to your computer and use it in GitHub Desktop.
Managing Local & Remote Notifications from different classes
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
// [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 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 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 | |
} |
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
import UIKit | |
import UserNotifications | |
class LocalNotificationManagerDelegate: NSObject, UNUserNotificationCenterDelegate { | |
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | |
// Play sound and show alert to the user | |
completionHandler([.alert, .sound]) | |
} | |
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | |
// Determine the user actions | |
completionHandler() | |
} | |
} | |
class LocalNotificationManager: NSObject { | |
private override init() {} | |
private static var notificationDelegate = LocalNotificationManagerDelegate() | |
class func register() { | |
let center = UNUserNotificationCenter.current() | |
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in | |
if !granted { | |
print("failed.") | |
} | |
} | |
} | |
class func removeNotifications() { | |
let center = UNUserNotificationCenter.current() | |
center.removeAllDeliveredNotifications() | |
center.removeAllPendingNotificationRequests() | |
} | |
class func authorizationStatus(completion: @escaping (_ status: UNAuthorizationStatus, _ authorized: Bool) -> Void) { | |
UNUserNotificationCenter.current().getNotificationSettings { (settings) in | |
let status = settings.authorizationStatus | |
completion(status, status == .authorized) | |
} | |
} | |
} |
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
class RemoteNotificationManagerDelegate: NSObject, UNUserNotificationCenterDelegate { | |
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | |
let userInfo = notification.request.content.userInfo | |
print(userInfo) | |
// Play sound and show alert to the user | |
completionHandler([.alert, .sound]) | |
} | |
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { | |
let userInfo = response.notification.request.content.userInfo | |
print(userInfo) | |
completionHandler() | |
} | |
} | |
class RemoteNotificationManagerMessagingDelegate: NSObject, MessagingDelegate { | |
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { | |
print("Firebase registration token: \(fcmToken)") | |
// Note: This callback is fired at each app startup and whenever a new token is generated. | |
} | |
// 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)") | |
} | |
} | |
class RemoteNotificationManager: NSObject { | |
private override init() {} | |
private static var notificationDelegate = RemoteNotificationManagerDelegate() | |
private static var messagingDelegate = RemoteNotificationManagerMessagingDelegate() | |
class func register() { | |
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] | |
UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (granted, error) in | |
if !granted { | |
print("failed.") | |
} | |
} | |
UIApplication.shared.registerForRemoteNotifications() | |
} | |
class func config() { | |
if UIApplication.shared.isRegisteredForRemoteNotifications { | |
UNUserNotificationCenter.current().delegate = notificationDelegate | |
Messaging.messaging().delegate = messagingDelegate | |
Messaging.messaging().shouldEstablishDirectChannel = true | |
} else { | |
register() | |
} | |
} | |
class func clear() { | |
UIApplication.shared.applicationIconBadgeNumber = 0 | |
let center = UNUserNotificationCenter.current() | |
center.removeAllDeliveredNotifications() | |
center.removeAllPendingNotificationRequests() | |
} | |
class func unregister() { | |
UIApplication.shared.unregisterForRemoteNotifications() | |
} | |
} |
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
//Using Local and Remote Notificaitons Manager class functions like this | |
@objc private func actionManagePushNotifications(switchOption: UISwitch) { | |
if switchOption.isOn { | |
if !UIApplication.shared.isRegisteredForRemoteNotifications { | |
RemoteNotificationManager.register() | |
} | |
} else { | |
RemoteNotificationManager.clear() | |
RemoteNotificationManager.unregister() | |
} | |
} | |
@objc private func actionSetBackgroundRefresh(switchOption: UISwitch) { | |
if switchOption.isOn { | |
LocalNotificationManager.authorizationStatus { [weak self] (status, authorised) in | |
if !authorised { | |
LocalNotificationManager.register() | |
} | |
} | |
} else { | |
LocalNotificationManager.removeNotifications() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick review:
I think so , I would try and write as little code as you can, the less there is the less can break ;)
Depends on how you want to use this later, it could probably work as a single class. If you want to keep it split, you should define a protocol that outlines what a Notification manager should do , then inherit from that for each type
local
andremote
. You can use extension on protocols to provide default behaviour if you need it.What you have called delegates in your code, are not delegates in my mind. I suggest you just inherit from
MessagingDelegate
etc in the classes directly.Some notes :