Estoy atascado durante varios días y necesito su amable ayuda.
Quiero recibir notificaciones automáticas silenciosas, también conocidas como notificaciones en segundo plano, para actualizar el contenido de mi aplicación.
La parte inferior de AppDelegate no se llama.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") // Does not get printed } print("success") // Does not get printed completionHandler(UIBackgroundFetchResult.newData) }A continuación se muestra el código de Python del lado del servidor. El lado del servidor dice que el envío de la notificación al dispositivo tuvo éxito, por lo que asumo que el problema radica en el lado frontal.
def send_notifications_to_fcm_token(fcm_token): try: message = messaging.Message( apns=messaging.APNSConfig( headers={ 'apns-push-type': 'background', 'apns-priority': '5', }, payload=messaging.APNSPayload( aps=messaging.Aps(content_available=True) ), ), token=fcm_token, ) response = messaging.send(message) except: response = False return responseAlgunas notas adicionales.
Las siguientes configuraciones de Xcode en los "Modos en segundo plano" están activadas: "Obtención en segundo plano", "Notificaciones remotas", "Procesamiento en segundo plano"
El modo de datos bajos y el modo de bajo consumo del iPhone están desactivados. (He leído antecedentes
la notificación no funciona si están activadas).
Logré recibir notificaciones push ordinarias (no silenciosas)
Permítanme adjuntar el código completo de AppDelegate por si acaso:
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? let gcmMessageIDKey = "gcm.message_id" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication .LaunchOptionsKey: Any]?) -> Bool { print("print did finish launching") FirebaseApp.configure() // [START set_messaging_delegate] Messaging.messaging().delegate = self // [END set_messaging_delegate] // Register for remote notifications. This shows a permission dialog on first run, to // show the dialog at a more appropriate time move this registration accordingly. // [START register_for_notifications] if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: { _, _ in } ) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications() // [END register_for_notifications] return true } func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { // If you are receiving a notification message while your app is in the background, // this callback will not be fired till the user taps on the notification launching the application. // TODO: Handle data of notification // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") } // Print full message. print("did receive remote notification print user info is \(userInfo)") } // [START receive_message] func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // If you are receiving a notification message while your app is in the background, // this callback will not be fired till the user taps on the notification launching the application. // TODO: Handle data of notification // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print message ID. //ここはSilent Notificationの設定で通知を送らないとトリガーされない模様if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") } // Print full message. print("did receive remote notification long version print user info is \(userInfo)") completionHandler(UIBackgroundFetchResult.newData) } // [END receive_message] func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { print("Unable to register for remote notifications: \(error.localizedDescription)") } // This function is added here only for debugging purposes, and can be removed if swizzling is enabled. // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to // the FCM registration token. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { print("APNs token retrieved: \(deviceToken)") // With swizzling disabled you must set the APNs token here. // Messaging.messaging().apnsToken = deviceToken } } // [START ios_10_message_handling] @available(iOS 10, *) extension AppDelegate: UNUserNotificationCenterDelegate { // Receive displayed notifications for iOS 10 devices. func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { let userInfo = notification.request.content.userInfo // With swizzling disabled you must let Messaging know about 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("will present print user info is \(userInfo)") let categoryId = userInfo["notifications_categories_id"] //as? Int //print("category id is \(categoryId!)") // Change this to your preferred presentation option completionHandler([[.alert, .sound]]) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo // [START_EXCLUDE] // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") } // [END_EXCLUDE] // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print full message. print("did receive print user info is \(userInfo)") completionHandler() } } // [END ios_10_message_handling] extension AppDelegate: MessagingDelegate { // [START refresh_token] func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { print("Firebase registration token: \(String(describing: fcmToken))") let dataDict: [String: String] = ["token": fcmToken ?? ""] NotificationCenter.default.post( name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict ) // TODO: If necessary send token to application server. // Note: This callback is fired at each app startup and whenever a new token is generated. } // [END refresh_token] }EDITAR: descubrí que este mensaje de error se está imprimiendo en el registro de la consola. [conexión] nw_read_request_report [C2] La recepción falló con el error "El software provocó la cancelación de la conexión"