18.2 C
London
Friday, September 6, 2024

ios – Firebase Push Notifications not being obtained by SwiftUI app


I’ve grow to be utterly confused relating to what’s required as a way to obtain push notifications from Firebase. I’ve the next in my app delegate – please may somebody assist me? I’ve arrange a server script that ought to set off the notifications – however I’ve been testing it utilizing the Firebase check message web page and have had no luck.

I’ve been utilizing the fem token – however I additionally see a Machine push notification token – is that for if I need to ship messages/notifications from my app?

class AppDelegate: UIResponder, UIApplicationDelegate
{
    var window: UIWindow?
    let gcmMessageIDKey = "gcm.message_id"
    @AppStorage("fcm") var fcm: String = ""
    
    func software(_ software: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication
                        .LaunchOptionsKey: Any]?) -> Bool
    {
        UNUserNotificationCenter.present().delegate = self
        FirebaseApp.configure()
        
        // [START set_messaging_delegate]
        //FirebaseApp.configure()
        Messaging.messaging().delegate = self
        // [END set_messaging_delegate]
        
        // Register for distant notifications. This exhibits a permission dialog on first run, to
        // present the dialog at a extra acceptable time transfer this registration accordingly.
        // [START register_for_notifications]
        
        UNUserNotificationCenter.present().requestAuthorization(choices: [.alert, .badge, .sound]) { granted, error in
            if granted {
                DispatchQueue.important.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
        }
        
        software.registerForRemoteNotifications()
        
        // [END register_for_notifications]
        return true
    }
    
    func software(_ software: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
        // In case you are receiving a notification message whereas your app is within the background,
        // this callback is not going to be fired until the consumer faucets on the notification launching the appliance.
        // TODO: Deal with knowledge of notification
        
        // With swizzling disabled you will need to let Messaging know in regards to the message, for Analytics
        Messaging.messaging().appDidReceiveMessage(userInfo)
        
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: (messageID)")
        }
        // Print full message.
        print(userInfo)
    }
    
    // [START receive_message]
    func software(_ software: UIApplication,
                     didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async
    -> UIBackgroundFetchResult {
        // In case you are receiving a notification message whereas your app is within the background,
        // this callback is not going to be fired until the consumer faucets on the notification launching the appliance.
        // TODO: Deal with knowledge of notification
        
        // With swizzling disabled you will need to let Messaging know in regards to the message, for Analytics
        Messaging.messaging().appDidReceiveMessage(userInfo)
        
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: (messageID)")
        }
        
        // Print full message.
        print(userInfo)
        
        return UIBackgroundFetchResult.newData
    }
    
    // [END receive_message]
    
    func software(_ software: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Knowledge) {
        // Convert token to string and ship to your server
        let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("Machine push notification token: (tokenString)")
        // Right here you'd usually ship the token to your server
    }
    
    func software(_ software: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Didn't register for distant notifications: (error)")
    }
    
    
    
    
}
    
// [START ios_10_message_handling]

extension AppDelegate: UNUserNotificationCenterDelegate {
  // Obtain displayed notifications for iOS 10 units.
  func userNotificationCenter(_ middle: UNUserNotificationCenter,
                              willPresent notification: UNNotification) async
    -> UNNotificationPresentationOptions {
    let userInfo = notification.request.content material.userInfo

    // With swizzling disabled you will need to let Messaging know in regards to the message, for Analytics
    Messaging.messaging().appDidReceiveMessage(userInfo)

    // [START_EXCLUDE]
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print("Message ID: (messageID)")
    }
    // [END_EXCLUDE]

    // Print full message.
    print(userInfo)

    // Change this to your most well-liked presentation possibility
        return [[.banner, .sound]]
  }
    

  func userNotificationCenter(_ middle: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse) async {
    let userInfo = response.notification.request.content material.userInfo

    // [START_EXCLUDE]
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print("Message ID: (messageID)")
    }
    // [END_EXCLUDE]

    // With swizzling disabled you will need to let Messaging know in regards to the message, for Analytics
    Messaging.messaging().appDidReceiveMessage(userInfo)
      NotificationHandler.shared.deal with(notification: response)
    // Print full message.
    print(userInfo)
  }
}

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here