You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before encountering the error, I was using Xcode which had Firebase installed. At around 12pm today, I began encountering an error that looked like this
APNS device token not set before retrieving FCM Token for Sender ID 'XXXXXXXXX'.Be sure to re-retrieve the FCM token once the APNS device token is set.
Prior to 12pm, I did not encounter the error at all
After tinkering with it for 9 hours, I tried moving methods around, looked up many websites to find the exact error, debugging, and even went to YouTube of all places, but can't figure out where the error is coming from.
If it helps, I am currently using Xcode 16 with Firebase version 10.27.
Here's the code for anyone who thinks they can find the answer
This is in my AppDelegate from constant debugging
For extra context:
I have the app running on my iPhone 15 Pro Max and was running well before the error
I have Background Modes enabled (Background fetch, processing, remote notifications)
In my Firebase Console, I have the APN key in my Cloud Messaging section
I have added the app to my Firebase server
I have the Google Info.plist in my code
I have the app registred for App Attest (AppCheck) and DeviceCheck
import UIKit
import UserNotifications
import Firebase
import FirebaseMessaging
import TabularData
import FirebaseInAppMessaging
classAppDelegate:NSObject,UIApplicationDelegate,UNUserNotificationCenterDelegate,MessagingDelegate{varwindow:UIWindow?
// Configure Firebase when the app launches
func application(_ application:UIApplication,
didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey:Any]?)->Bool{FirebaseApp.configure()print("1st launch")
// Set up push notification permissions
registerForPushNotifications(application)
// Set the messaging delegate
print("2nd launch")Messaging.messaging().delegate =selfreturn true
}
// Register for push notifications
func registerForPushNotifications(_ application:UIApplication){print("3rd launch")UNUserNotificationCenter.current().delegate =selfletauthOptions:UNAuthorizationOptions=[.alert,.badge,.sound]UNUserNotificationCenter.current().requestAuthorization(options: authOptions){ granted, error inprint("Permission granted: \(granted)")}
application.registerForRemoteNotifications()}
// Called when APNs has assigned a device token to the app
func application(_ application:UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken:Data){
// Forward the APNs token to Firebase
Messaging.messaging().apnsToken = deviceToken
print("APNs Token registered: \(deviceToken)")}
// Handle any errors when trying to register for remote notifications
func application(_ application:UIApplication, didFailToRegisterForRemoteNotificationsWithError error:Error){print("Failed to register for remote notifications: \(error)")}
// Called when Firebase messaging gets a new FCM token
func messaging(_ messaging:Messaging, didReceiveRegistrationToken fcmToken:String?){
guard let fcmToken = fcmToken else{return}print("Firebase FCM token: \(fcmToken)")
// Optionally, send this token to your server
// sendFCMTokenToServer(fcmToken)
}func userNotificationCenter(_ center:UNUserNotificationCenter, willPresent notification:UNNotification, withCompletionHandler completionHandler:@escaping(UNNotificationPresentationOptions)->Void){handleDailyReportTrigger()completionHandler([.banner,.sound,.badge])}func scheduleDailyReport(){print("Scheduling the daily report in scheduleDailyReport...")
// Schedule the generation of the report
vardateComponents=DateComponents()
dateComponents.hour =16 // Adjust to your preferred time
dateComponents.minute =10print("At the UserNotificationCenter")lettrigger=UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)letrequest=UNNotificationRequest(identifier:"DailyReport", content:UNMutableNotificationContent(), trigger: trigger)UNUserNotificationCenter.current().add(request){ error in
if let error = error {print("Error scheduling daily report: \(error.localizedDescription)")}else{print("Daily report scheduled successfully.")}}
// Automatically generate and display the report when the scheduled time is reached
generateDailyReport()}privatefunc generateDailyReport(){letstartOfDay=Calendar.current.startOfDay(for:Date())letendOfDay=Calendar.current.date(byAdding:.day, value:1, to: startOfDay)!
lethistoryRef=Database.database().reference().child("history")letquery= historyRef.queryOrdered(byChild:"timestamp").queryStarting(atValue: startOfDay.timeIntervalSince1970).queryEnding(atValue: endOfDay.timeIntervalSince1970)
query.observeSingleEvent(of:.value){ snapshot invarservices:[[String:Any]]=[]
for child in snapshot.children {
if let childSnapshot = child as?DataSnapshot,let serviceData = childSnapshot.value as?[String:Any]{
services.append(serviceData)}}
// Only proceed with CSV if we have data
if !services.isEmpty {letcsvURL=self.createCSVReport(from: services)self.displayCSVFile(at: csvURL)
// Optionally delete the history
historyRef.removeValue{ error, _ in
if let error = error {print("Error deleting history: \(error.localizedDescription)")}else{print("History deleted successfully")}}}else{print("No services found for the day.")}} withCancel:{ error inprint("Error fetching history data: \(error.localizedDescription)")}}privatefunc createCSVReport(from services:[[String:Any]])->URL{vardataFrame=DataFrame()
// Create columns
letcustomerNames=Column(name:"Customer Name", contents: services.map{$0["customerName"]as?String??"N/A"})letaddresses=Column(name:"Address", contents: services.map{$0["address"]as?String??"N/A"})letphoneNumbers=Column(name:"Phone Number", contents: services.map{$0["phoneNumber"]as?String??"N/A"})letdriverNames=Column(name:"Driver Name", contents: services.map{$0["driverName"]as?String??"N/A"})letstatuses=Column(name:"Status", contents: services.map{$0["status"]as?String??"N/A"})lettimestamps=Column(name:"Timestamp", contents: services.map{ service inlettimestamp=service["timestamp"]as?TimeInterval??0letdate=Date(timeIntervalSince1970: timestamp)letdateFormatter=DateFormatter()
dateFormatter.dateFormat ="yyyy-MM-dd HH:mm:ss"return dateFormatter.string(from: date)})
// Add columns to the DataFrame
dataFrame.append(column: customerNames)
dataFrame.append(column: addresses)
dataFrame.append(column: phoneNumbers)
dataFrame.append(column: driverNames)
dataFrame.append(column: statuses)
dataFrame.append(column: timestamps)
// Export DataFrame to CSV format
letcsvData=try! dataFrame.csvRepresentation()
// Save to directory
letdocumentsDirectory=FileManager.default.urls(for:.documentDirectory, in:.userDomainMask).first!
letfileURL= documentsDirectory.appendingPathComponent("Service_Report_\(Date().toFormattedString()).csv")try! csvData.write(to: fileURL)print("CSV file created at: \(fileURL.path)")return fileURL
}privatefunc displayCSVFile(at url:URL){letactivityViewController=UIActivityViewController(activityItems:[url], applicationActivities:nil)
if let windowScene =UIApplication.shared.connectedScenes.first as?UIWindowScene{
windowScene.windows.first?.rootViewController?.present(activityViewController, animated: true, completion:nil)}}func userNotificationCenter(_ center:UNUserNotificationCenter, didReceive response:UNNotificationResponse, withCompletionHandler completionHandler:@escaping()->Void){letuserInfo= response.notification.request.content.userInfo
print("UserInfo: \(userInfo)")handleDailyReportTrigger()completionHandler()}privatefunc handleDailyReportTrigger(){letviewModel=AdminDashboardViewModel() // Access your view model here
viewModel.generateDailyReport()}}@MainActorclassNotificationManager:ObservableObject{@Publishedprivate(set)varhasPermission= false
staticletshared=NotificationManager()init(){Task{awaitgetAuthStatus()}}func request()async{do{tryawaitUNUserNotificationCenter.current().requestAuthorization(options:[.alert,.badge,.sound])awaitgetAuthStatus()}catch{print(error)}}func getAuthStatus()async{letstatus=awaitUNUserNotificationCenter.current().notificationSettings()
switch status.authorizationStatus {case.authorized,.ephemeral,.provisional:
hasPermission = true
default:
hasPermission = false
}}func postNewPickupRequestNotification(){NotificationCenter.default.post(name:.newPickupRequest, object:nil)}@MainActorfunc observeNewPickupRequestNotification(observer:Any, selector:Selector){NotificationCenter.default.addObserver(observer, selector: selector, name:.newPickupRequest, object:nil)}}extensionNotification.Name{staticletnewPickupRequest=Notification.Name("newPickupRequest")}@MainActorclassNotificationHandler:NSObject,ObservableObject{@PublishedvarnewRequestAlert= false
overrideinit(){
super.init()Task{@MainActorinNotificationManager.shared.observeNewPickupRequestNotification(observer:self, selector: #selector(handleNewPickupRequest))}}@objcprivatefunc handleNewPickupRequest(){
newRequestAlert = true
scheduleLocalNotification(title:"New Pickup Request", body:"A new pickup request has been added.", timeInterval:1)print("handleNewPickupRequest is here!")
// Dismiss alert after some time (e.g., 3 seconds)
DispatchQueue.main.asyncAfter(deadline:.now()+3){self.newRequestAlert = false
}}privatefunc scheduleLocalNotification(title:String, body:String, timeInterval:TimeInterval){letcontent=UNMutableNotificationContent()
content.title = title
content.body = body
content.sound =.default
lettrigger=UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)letrequest=UNNotificationRequest(identifier:UUID().uuidString, content: content, trigger: trigger)UNUserNotificationCenter.current().add(request)}}classNotificationDelegate:NSObject,UNUserNotificationCenterDelegate{func userNotificationCenter(_ center:UNUserNotificationCenter, didReceive response:UNNotificationResponse, withCompletionHandler completionHandler:@escaping()->Void){letuserInfo= response.notification.request.content.userInfo
print(userInfo)completionHandler()}func userNotificationCenter(_ center:UNUserNotificationCenter, willPresent notification:UNNotification, withCompletionHandler completionHandler:@escaping(UNNotificationPresentationOptions)->Void){completionHandler([.banner,.sound,.badge])}}
The text was updated successfully, but these errors were encountered:
Before encountering the error, I was using Xcode which had Firebase installed. At around 12pm today, I began encountering an error that looked like this
APNS device token not set before retrieving FCM Token for Sender ID 'XXXXXXXXX'.Be sure to re-retrieve the FCM token once the APNS device token is set.
Prior to 12pm, I did not encounter the error at all
After tinkering with it for 9 hours, I tried moving methods around, looked up many websites to find the exact error, debugging, and even went to YouTube of all places, but can't figure out where the error is coming from.
If it helps, I am currently using Xcode 16 with Firebase version 10.27.
Here's the code for anyone who thinks they can find the answer
This is in my AppDelegate from constant debugging
For extra context:
I have the app running on my iPhone 15 Pro Max and was running well before the error
I have Background Modes enabled (Background fetch, processing, remote notifications)
In my Firebase Console, I have the APN key in my Cloud Messaging section
I have added the app to my Firebase server
I have the Google Info.plist in my code
I have the app registred for App Attest (AppCheck) and DeviceCheck
The text was updated successfully, but these errors were encountered: