Skip to content

Notification

howardfuntek edited this page Apr 13, 2020 · 8 revisions

You have to set up a Firebase Cloud Messaging. Follow Firebase Document.

After setting up, your AppDelegate.swift will look like this.

import Firebase
import UserNotifications

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        
        application.registerForRemoteNotifications()
        Messaging.messaging().delegate = self
        return true
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
        // Print message ID.
        if let messageID = userInfo["gcm.message_id"] {
            print("Message ID: \(messageID)")
        }
        
        // Print full message.
        print(userInfo)
        
        // Change this to your preferred presentation option
        completionHandler([])
        // completionHandler([.alert, .badge, .sound])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo["gcm.message_id"] {
            print("Message ID: \(messageID)")
        }
        
        // Print full message.
        print(userInfo)
        
        completionHandler()
    }
}

extension AppDelegate: MessagingDelegate {
    // [START refresh_token]
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        
        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: 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.
    }
}

Then, you have to implement IMSubscribeTask in func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) to enable chat server pushing notification.

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        
        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: 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.
        IMSubscribeTask().perform(fcmToken: fcmToken, type: "fcm").done { _ in
            
            }.catch { error in
                print(error)
        }
    }

If you want to disable notification, use IMUnsubscribeTask.

IMUnsubscribeTask().perform().done { _ in  
}.catch { error in
    print(error)
}

Tasks

  • IMSubscribeTask
IMSubscribeTask().perform(fcmToken: String, type: String)
Parameter Type Required/Optional Default Value Description
fcmToken String Required
type String Optional "fcm" "fcm" for FCM and "ios" fon APNS
  • IMUnsubscribeTask
IMUnsubscribeTask().perform(type: String)
Parameter Type Required/Optional Default Value Description
type String Optional "fcm" "fcm" for FCM and "ios" fon APNS

Localization

Add following lines to Localizable.strings in your xcode project.

en

"im_loc_text" = "%@";
"im_loc_image" = "sent a photo";
"im_loc_audio" = "sent a voice message";
"im_loc_video" = "sent a video";
"im_loc_file" = "sent a file";
"im_loc_location" = "sent a location";
"im_loc_sticker" = "sent a sticker";
"im_loc_joinRoom" = "joined the chat";
"im_loc_leaveRoom" = "left the chat";
"im_loc_addMember" = "invited %@";
"im_loc_deleteMember" = "kicked %@";
"im_loc_addMembers" = "invited %@";

zh-Hant

"im_loc_text" = "%@";
"im_loc_image" = "傳送了照片";
"im_loc_audio" = "傳送了語音訊息";
"im_loc_video" = "傳送了影片";
"im_loc_file" = "傳送了檔案";
"im_loc_location" = "傳送了位置";
"im_loc_sticker" = "傳送了貼圖";
"im_loc_joinRoom" = "加入聊天";
"im_loc_leaveRoom" = "離開聊天";
"im_loc_addMember" = "邀請 %@ 加入";
"im_loc_deleteMember" = "將 %@ 踢出";
"im_loc_addMembers" = "邀請 %@ 加入";

Display notification while App in foreground but not in chatroom

extension UIWindow {
    
    func visibleViewController() -> UIViewController? {
        if let rootViewController: UIViewController = self.rootViewController {
            return UIWindow.getVisibleViewControllerFrom(vc: rootViewController)
        }
        return nil
    }
    
    static func getVisibleViewControllerFrom(vc:UIViewController) -> UIViewController {
        if let navigationController = vc as? UINavigationController,
            let visibleController = navigationController.visibleViewController  {
            return UIWindow.getVisibleViewControllerFrom( vc: visibleController )
        } else if let tabBarController = vc as? UITabBarController,
            let selectedTabController = tabBarController.selectedViewController {
            return UIWindow.getVisibleViewControllerFrom(vc: selectedTabController )
        } else {
            if let presentedViewController = vc.presentedViewController {
                return UIWindow.getVisibleViewControllerFrom(vc: presentedViewController)
            } else {
                return vc
            }
        }
    }
}
extension AppDelegate: UNUserNotificationCenterDelegate {
    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        if let topController = window?.visibleViewController(), topController is IMMessagesViewController {
            completionHandler([])
        }
        completionHandler([.alert, .badge, .sound])
    }
}
Clone this wiki locally