Hir*_*iro 8 apn apple-push-notifications swift
我被困了好几天了,需要你的帮助。
\n我想接收静默推送通知(又称后台通知)来更新我的应用程序内容。
\nAppDelegate 中的以下部分不会被调用。
\nfunc application(_ application: UIApplication,\n didReceiveRemoteNotification userInfo: [AnyHashable: Any],\n fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult)\n -> Void) {\n \n if let messageID = userInfo[gcmMessageIDKey] {\n print("Message ID: \\(messageID)") // Does not get printed\n }\n\n print("success") // Does not get printed\n\n completionHandler(UIBackgroundFetchResult.newData)\n \n}\n
Run Code Online (Sandbox Code Playgroud)\n下面是服务器端的Python代码。服务器端说向设备发送通知成功,所以我认为问题出在前端。
\ndef send_notifications_to_fcm_token(fcm_token):\n try:\n message = messaging.Message(\n apns=messaging.APNSConfig(\n headers={\n 'apns-push-type': 'background',\n 'apns-priority': '5',\n },\n payload=messaging.APNSPayload(\n aps=messaging.Aps(content_available=True)\n ),\n ),\n token=fcm_token,\n )\n response = messaging.send(message)\n except:\n response = False\n return response\n
Run Code Online (Sandbox Code Playgroud)\n一些附加说明。
\n“后台模式”中的以下 Xcode 设置已打开:\n“后台获取”、“远程通知”、“后台处理”
\niPhone 的低数据模式和低功耗模式已关闭。(我已阅读背景
\n如果这些打开,则通知不起作用。)
我成功接收普通(非静默)推送通知
\n让我附上整个 AppDelegate 代码以防万一:
\nclass AppDelegate: UIResponder, UIApplicationDelegate {\n var window: UIWindow?\n let gcmMessageIDKey = "gcm.message_id"\n\n func application(_ application: UIApplication,\n didFinishLaunchingWithOptions launchOptions: [UIApplication\n .LaunchOptionsKey: Any]?) -> Bool {\n \n print("print did finish launching")\n \n FirebaseApp.configure()\n\n // [START set_messaging_delegate]\n Messaging.messaging().delegate = self\n // [END set_messaging_delegate]\n // Register for remote notifications. This shows a permission dialog on first run, to\n // show the dialog at a more appropriate time move this registration accordingly.\n // [START register_for_notifications]\n if #available(iOS 10.0, *) {\n // For iOS 10 display notification (sent via APNS)\n UNUserNotificationCenter.current().delegate = self\n\n let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]\n UNUserNotificationCenter.current().requestAuthorization(\n options: authOptions,\n completionHandler: { _, _ in }\n )\n } else {\n let settings: UIUserNotificationSettings =\n UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)\n application.registerUserNotificationSettings(settings)\n }\n\n application.registerForRemoteNotifications()\n\n // [END register_for_notifications]\n return true\n }\n\n func application(_ application: UIApplication,\n didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {\n // If you are receiving a notification message while your app is in the background,\n // this callback will not be fired till the user taps on the notification launching the application.\n // TODO: Handle data of notification\n // With swizzling disabled you must let Messaging know about the message, for Analytics\n // Messaging.messaging().appDidReceiveMessage(userInfo)\n // Print message ID.\n if let messageID = userInfo[gcmMessageIDKey] {\n print("Message ID: \\(messageID)")\n }\n\n // Print full message.\n print("did receive remote notification print user info is \\(userInfo)")\n }\n\n // [START receive_message]\n func application(_ application: UIApplication,\n didReceiveRemoteNotification userInfo: [AnyHashable: Any],\n fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult)\n -> Void) {\n // If you are receiving a notification message while your app is in the background,\n // this callback will not be fired till the user taps on the notification launching the application.\n // TODO: Handle data of notification\n \n // With swizzling disabled you must let Messaging know about the message, for Analytics\n // Messaging.messaging().appDidReceiveMessage(userInfo)\n // Print message ID.\n \n //\xe3\x81\x93\xe3\x81\x93\xe3\x81\xafSilent Notification\xe3\x81\xae\xe8\xa8\xad\xe5\xae\x9a\xe3\x81\xa7\xe9\x80\x9a\xe7\x9f\xa5\xe3\x82\x92\xe9\x80\x81\xe3\x82\x89\xe3\x81\xaa\xe3\x81\x84\xe3\x81\xa8\xe3\x83\x88\xe3\x83\xaa\xe3\x82\xac\xe3\x83\xbc\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xaa\xe3\x81\x84\xe6\xa8\xa1\xe6\xa7\x98\n \n if let messageID = userInfo[gcmMessageIDKey] {\n print("Message ID: \\(messageID)")\n }\n\n // Print full message.\n print("did receive remote notification long version print user info is \\(userInfo)")\n\n completionHandler(UIBackgroundFetchResult.newData)\n }\n\n // [END receive_message]\n func application(_ application: UIApplication,\n didFailToRegisterForRemoteNotificationsWithError error: Error) {\n print("Unable to register for remote notifications: \\(error.localizedDescription)")\n }\n\n // This function is added here only for debugging purposes, and can be removed if swizzling is enabled.\n // If swizzling is disabled then this function must be implemented so that the APNs token can be paired to\n // the FCM registration token.\n func application(_ application: UIApplication,\n didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {\n print("APNs token retrieved: \\(deviceToken)")\n\n // With swizzling disabled you must set the APNs token here.\n // Messaging.messaging().apnsToken = deviceToken\n }\n}\n\n// [START ios_10_message_handling]\n@available(iOS 10, *)\nextension AppDelegate: UNUserNotificationCenterDelegate {\n // Receive displayed notifications for iOS 10 devices.\n func userNotificationCenter(_ center: UNUserNotificationCenter,\n willPresent notification: UNNotification,\n withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)\n -> Void) {\n let userInfo = notification.request.content.userInfo\n\n // With swizzling disabled you must let Messaging know about the message, for Analytics\n // Messaging.messaging().appDidReceiveMessage(userInfo)\n // [START_EXCLUDE]\n // Print message ID.\n if let messageID = userInfo[gcmMessageIDKey] {\n print("Message ID: \\(messageID)")\n }\n // [END_EXCLUDE]\n // Print full message.\n print("will present print user info is \\(userInfo)") \n \n let categoryId = userInfo["notifications_categories_id"] //as? Int\n //print("category id is \\(categoryId!)")\n\n // Change this to your preferred presentation option\n completionHandler([[.alert, .sound]])\n }\n\n func userNotificationCenter(_ center: UNUserNotificationCenter,\n didReceive response: UNNotificationResponse,\n withCompletionHandler completionHandler: @escaping () -> Void) {\n let userInfo = response.notification.request.content.userInfo\n\n // [START_EXCLUDE]\n // Print message ID.\n if let messageID = userInfo[gcmMessageIDKey] {\n print("Message ID: \\(messageID)")\n }\n // [END_EXCLUDE]\n // With swizzling disabled you must let Messaging know about the message, for Analytics\n // Messaging.messaging().appDidReceiveMessage(userInfo)\n // Print full message.\n print("did receive print user info is \\(userInfo)") \n\n completionHandler()\n }\n}\n\n// [END ios_10_message_handling]\nextension AppDelegate: MessagingDelegate {\n // [START refresh_token]\n func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {\n print("Firebase registration token: \\(String(describing: fcmToken))") \n\n let dataDict: [String: String] = ["token": fcmToken ?? ""]\n NotificationCenter.default.post(\n name: Notification.Name("FCMToken"),\n object: nil,\n userInfo: dataDict\n )\n // TODO: If necessary send token to application server.\n // Note: This callback is fired at each app startup and whenever a new token is generated.\n }\n\n // [END refresh_token]\n}\n
Run Code Online (Sandbox Code Playgroud)\n编辑:\n我发现此错误消息正在控制台日志中打印。\n[connection] nw_read_request_report [C2] 接收失败,错误为“软件导致连接中止”
\n 归档时间: |
|
查看次数: |
2254 次 |
最近记录: |