Hello,
We are developing a parental control app consisting of two parts: a parent app to manage settings and a child app to enforce these settings using iOS's Screen Time API, CoreData, and other components. We've attempted to use silent notifications with Firebase Cloud Messaging (FCM) to communicate updates from the parent app to the child app. Our current implementation involves background modes for remote messages and background tasks.
However, we're facing a challenge: while normal FCM push notifications with a 'message' key work as expected, silent notifications (with only a 'data' key) do not trigger the desired behavior in the child app, even though FCM returns a success response.
We're looking for assistance with two main issues:
- Alternative Approaches: Is there a better way to notify the child app of changes? We're considering a system where the child app periodically checks for updates via API and then updates CoreData and managed settings. Any recommendations for this architecture or a more reliable notification system would be greatly appreciated.
- Debugging Silent Notifications: If our current approach using silent notifications is feasible, could someone help us debug why these notifications are not working as expected? We've been stuck on this for a week, and any help would be a lifesaver.
Here's the relevant part of our AppDelegate code:
import UIKit
import FirebaseCore
import FirebaseMessaging
import BackgroundTasks
@objc class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
let gcmMessageIDKey = "gcm.message_id"
let backgroundTaskIdentifier = "com.your-company.your-app.silentnotification"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
// Register for remote notifications
UNUserNotificationCenter.current().delegate = self
application.registerForRemoteNotifications()
// Register background task
BGTaskScheduler.shared.register(forTaskWithIdentifier: backgroundTaskIdentifier, using: nil) { task in
self.handleBackgroundTask(task: task as! BGProcessingTask)
}
return true
}
// Handle incoming remote notifications
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let aps = userInfo["aps"] as? [String: Any],
let contentAvailable = aps["content-available"] as? Int, contentAvailable == 1 {
// This is a silent notification
handleSilentNotification(userInfo: userInfo, completionHandler: completionHandler)
} else {
// This is a regular notification
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(.newData)
}
}
// Handle silent notification
func handleSilentNotification(userInfo: [AnyHashable: Any], completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let request = BGProcessingTaskRequest(identifier: backgroundTaskIdentifier)
request.requiresNetworkConnectivity = true
do {
try BGTaskScheduler.shared.submit(request)
performAPICall { result in
switch result {
case .success(_):
completionHandler(.newData)
case .failure(_):
completionHandler(.failed)
}
}
} catch {
completionHandler(.failed)
}
}
// Handle background task
func handleBackgroundTask(task: BGProcessingTask) {
task.expirationHandler = {
task.setTaskCompleted(success: false)
}
performAPICall { result in
task.setTaskCompleted(success: result != nil)
}
}
// Perform API call (placeholder implementation)
func performAPICall(completion: @escaping (Data?) -> Void) {
// Your API call implementation here
// For testing, you can use a simple delay:
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
completion(Data())
}
}
}
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
print("FCM token: \(fcmToken ?? "nil")")
// TODO: Send this token to your server
}
}
Additionally, here is how we're sending notifications from the server side using Node.js:
// Import the required Firebase Admin SDK (assumed to be initialized elsewhere)
// const { getMessaging } = require('firebase-admin/messaging');
/**
* Sends a background push notification to an iOS device
* @returns {Promise<string>} The message ID if successful
* @throws Will throw an error if the sending process fails
*/
async function sendBackgroundPushNotification() {
// Construct the message object for a background push notification
const message = {
apns: {
headers: {
// Set the priority of the push notification
"apns-priority": "5",
priority: "5",
// Indicate that this is a background refresh notification
"content-available": "1",
content_available: "1",
// Specify the push type as background
"apns-push-type": "background",
// Set the topic to your app's bundle identifier
"apns-topic": "com.your-company.your-app", // Replace with your actual bundle identifier
},
payload: {
aps: {
// This tells iOS to wake up your app in the background
"content-available": 1,
},
},
},
// Custom data payload to be sent with the notification
// Modify this object to include the data you want to send
data: {
// Add your custom key-value pairs here
},
// Uncomment the following block if you want to include a visible notification
// notification: {
// title: "Notification Title",
// body: "Notification Body",
// },
token
token: "DEVICE_FCM_TOKEN_PLACEHOLDER",
};
try {
// Attempt to send the message using Firebase Cloud Messaging
const response = await getMessaging().send(message);
console.log("Successfully sent background data to iOS:", response);
return response;
} catch (error) {
console.error("Error sending background data to iOS:", error);
throw error;
}
}
// Example usage:
// sendBackgroundPushNotification()
// .then((response) => console.log("Message sent successfully:", response))
// .catch((error) => console.error("Failed to send message:", error));
We would really appreciate any insights or guidance on these issues. Thank you!