Description
I am developing a standalone app for Apple tvOS. I am running the following code
import SwiftUI
import Firebase
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
@AppStorage("fcmToken") var fToken = ""
let gcmMessageIDKey = "gcm.message_id"
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.banner, .badge, .sound])
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
// Register for remote notifications
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
Messaging.messaging().delegate = self
return true
}
// MARK: - Push Notifications
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print("APNS device token: \(deviceTokenString)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications with error: \(error.localizedDescription)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if userInfo[gcmMessageIDKey] != nil {
print("Received data message: \(userInfo)")
completionHandler(.newData)
}
}
// MARK: - Messaging
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
guard let fcmToken = fcmToken else { return }
fToken = fcmToken
print("FCM token: \(fcmToken)")
}
}
@main
struct SomeExampleApp_TVApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
I am also getting this from console
2023-04-24 20:04:46.212321+0530 SomeExampleApp[1262:39696] void * _Nullable NSMapGet(NSMapTable * _Nonnull, const void * _Nullable): map table argument is NULL
2023-04-24 20:04:46.221547+0530 SomeExampleApp[1262:39865] 9.6.0 - [FirebaseMessaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '012849237482'. Notifications to this FCM Token will not be delivered over APNS.Be sure to re-retrieve the FCM token once the APNS device token is set.
FCM token: someFcmToken
2023-04-24 20:04:48.650580+0530 SomeExampleApp[1262:39858] [] nw_path_necp_check_for_updates Failed to copy updated result (22)
FCM token: someFcmToken
APNS device token: someApnToken
Received data message: [AnyHashable("aps"): {
alert = {
body = "Entry Lights turned on as you entered Office";
title = "Location Automation";
};
"content-available" = 1;
}, AnyHashable("google.c.a.e"): 1, AnyHashable("google.c.sender.id"): 984386421852, AnyHashable("google.c.fid"): citAzVs8zUeFtNaj_EZJmN, AnyHashable("gcm.message_id"): 1682346907169586]
Received data message: [AnyHashable("google.c.a.e"): 1, AnyHashable("gcm.message_id"): 1682347026515009, AnyHashable("aps"): {
alert = {
body = "Entry Lights turned on as you entered Office";
title = "Location Automation";
};
"content-available" = 1;
}, AnyHashable("google.c.fid"): citAzVs8zUeFtNaj_EZJmN, AnyHashable("google.c.sender.id"): 012849237482]
Received data message: [AnyHashable("aps"): {
alert = {
body = "Entry Lights turned on as you entered Office";
title = "Location Automation";
};
"content-available" = 1;
}, AnyHashable("google.c.sender.id"): 984386421852, AnyHashable("gcm.message_id"): 1682347042989389, AnyHashable("google.c.a.e"): 1, AnyHashable("google.c.fid"): citAzVs8zUeFtNaj_EZJmN]
Firebase SDK Version
9.6.0
Xcode Version
Version 14.2 (14C18)
Installation Method
Swift Package Manager
Firebase Product(s)
Messaging
Targeted Platforms
tvOS
Run above code as tvos with firebase cloud messaging and apn setup.