ios - IOS Firebase 推送通知在后台模式下不起作用
问题描述
我实现了firebase通知,当我的设备处于前台时,通知也可以通过firebase控制台和我的后端服务器正常工作。但是在后台模式的情况下,当我通过控制台发送通知时它工作正常,但是当它通过我的后端服务器发送时它不起作用也尝试使用推送尝试等在线工具但不起作用。
我在 Firebase 控制台中设置了身份验证密钥。在我的应用程序委托文件中,代码如下..
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
....
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// Messaging.messaging().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)
}
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
return true
}
我添加的另一种方法
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("receive new data from didReceiveRemoteNotification")
print(userInfo)
completionHandler(.newData)
}
//MARK:Messaging delegate
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(String(describing: fcmToken))")
user.fcmToken = fcmToken
user.firebaseToken = fcmToken
print("user token >>>>> \(user.fcmToken)")
}
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
// Print full message.
print("user_info---",userInfo)
// Change this to your preferred presentation option
completionHandler([.alert,.sound])
}
//--- one
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print("user_info---",userInfo)
print("tap on on forground app",userInfo)
completionHandler()
}
}
我在 xcode 中的功能部分
通知负载:
{"to":"mytoken","data":{"aps":{"alert":{"title":"Portugal vs. Denmark","body":"great match!"},"badge":1}},"priority":"high"}
解决方案
添加值为1的“内容可用”
例子:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
有关后台通知的更多信息,请参阅 Apple 文档:
ps 我在评论方面没有太多声誉,所以我会尽力帮助回答
推荐阅读
- arrays - 动态数组不接受在 1x2 中重新调整尺寸(2x2 和 2x1 有效)
- python - 如何将充满数字字符串的csv文件读取到numpy数组中
- python - 如何快速向数据框插入一行?
- flutter - 当我从 FLUTTER 的下拉列表中更新数据时出错?
- c# - 当我使用 .NET Core HttpClient 时,Google FCM 给了我 BadRequest
- c - 函数位于目标文件之一中,但链接器未链接它们
- javascript - 当 api 的加载变量为 false 时反应更新视图
- neo4j - 如何计算数组中的字符串频率(cypher,neo4j)
- extjs - 如何在网格中调整 multiSortLimit
- wordpress - 联系表格 DB - 导出到 excel 的 WordPress 插件不起作用