Skip to content

Instantly share code, notes, and snippets.

@likil33
Last active September 19, 2022 12:53
Show Gist options
  • Select an option

  • Save likil33/6d03aae961d614c697ce49dd5c39e3da to your computer and use it in GitHub Desktop.

Select an option

Save likil33/6d03aae961d614c697ce49dd5c39e3da to your computer and use it in GitHub Desktop.
Firebase push notification
pod 'Firebase/Analytics'
pod 'Firebase/Messaging'
import UserNotifications
import UserNotificationsUI
import Firebase
import FirebaseMessaging
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//Notification settings
self.registrationforPushNotification(application)
FirebaseApp.configure()
Messaging.messaging().delegate = self
}
func registrationforPushNotification(_ application: UIApplication){
//Firebase Configuration
// Register for Push
DispatchQueue.main.async {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { (granted, error) in
if granted{
DispatchQueue.main.async(execute: {
application.registerForRemoteNotifications()
})
}
})
}
}
//MARK: - Getting DeviceToken
extension AppDelegate {
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var token = ""
for i in 0..<deviceToken.count {
token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
}
print("DeviceToken ----> \(token)")
Messaging.messaging().apnsToken = deviceToken
}
}
//MARK: - Handling Push Notification
extension AppDelegate {
func handlePushNotification(userinfo:[String:Any]) {
DispatchQueue.main.async {
if UserDefaults.standard.value(forKey: kLoginDetails) != nil{
if UserDefaults.standard.bool(forKey: kInNotificationPage){
UserDefaults.standard.set(false, forKey: kInNotificationPage)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LoadingNotificationList"), object: nil)
}
else {
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "RedirectNotificationPage"), object: nil)
}
}
}
}
}
//MARK: - UserNotification Delegates
extension AppDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// let userInfo = notification.request.content.userInfo as! [String: Any]
completionHandler([.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo as! [String:Any]
completionHandler()
self.handlePushNotification(userinfo: userInfo)
}
}
//MARK: - MessagingDelegate
extension AppDelegate: MessagingDelegate {
func applicationReceivedRemoteMessage(_ remoteMessage: MessagingRemoteMessage){
print("Success")
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
UserDefaults.standard.set(fcmToken, forKey: kDeviceToken)
UserDefaults.standard.synchronize()
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment