Skip to content

Instantly share code, notes, and snippets.

@hemangshah
Created December 12, 2018 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hemangshah/0b19d796ea5e46abcb4702db96195321 to your computer and use it in GitHub Desktop.
Save hemangshah/0b19d796ea5e46abcb4702db96195321 to your computer and use it in GitHub Desktop.
Managing Local & Remote Notifications from different classes
// [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
}
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)
}
}
}
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()
}
}
//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()
}
}
@hemangshah
Copy link
Author

Hi, thanks for coming here to review this code. I am trying to create two classes one is for local notifications (LocalNotificationManager) and one is for remote notifications (RemoteNotificationManager) to manage both types of notifications. I have implemented them in a correct way and seems they are working, however, I am not sure if this is the right (and best) way to manage this.

  • It may be possible that I have written so much code here and there which is not require.
  • May be someone can suggest to merge both classes into a single class: NotificationsManager, that can handle local as well as remote notifications.
  • May be, I don't need some of the delegates which I have implemented?

Requirements: iOS 10.x onwards - No backward compatibility.

If you have any questions feel free to comment here and we'll discuss.

Please advice me on these points and guide me to update it properly. I would appreciate any tip from you!!

@JonathanWilson1
Copy link

JonathanWilson1 commented Dec 12, 2018

Quick review:

It may be possible that I have written so much code here and there which is not require.

I think so , I would try and write as little code as you can, the less there is the less can break ;)

May be someone can suggest to merge both classes into a single class: NotificationsManager, that can handle local as well as remote notifications.

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 and remote. You can use extension on protocols to provide default behaviour if you need it.

May be, I don't need some of the delegates which I have implemented?

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 :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment