当应用程序处于后台时,我已成功收到通知但是在iOS 11中未使用Swift4调用回复方法 didReceiveRemoteNotification 。我想在应用程序处于后台收到的通知中时获取通知有效负载数据。我正在使用firebase控制台发送通知。当应用程序打开时,会调用 willPresent 。但在背景中没有任何方法调用 我正在使用代码: -
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any])
{
print(userInfo)
saveNotificationDetail(userInfo: userInfo)
}
func notificationIntegrate(application: UIApplication)
{
FirebaseApp.configure()
Messaging.messaging().shouldEstablishDirectChannel = true
Messaging.messaging().delegate=self
// [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]
// Add observer for InstanceID token refresh callback.
NotificationCenter.default
.addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotification),
name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
}
@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
// Print full message.
print(userInfo)
saveNotificationDetail(userInfo: userInfo)
completionHandler(UNNotificationPresentationOptions.alert)
// Change this to your preferred presentation option
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
{
let userInfo = response.notification.request.content.userInfo
print(userInfo)
}
}
答案 0 :(得分:1)
推送通知中的配置设置可能存在问题。验证以下步骤:
<强> 1。检查您是否在remote-notifcation
标签中为您的应用启用了background-fetch
和Capabilities
。
<强> 2。推送通知有效负载由content-available
密钥组成:
关键内容 - 可用是一项新功能,正是这一关键使得无声推送成为可能。
您可以使用content-available = 1启用。但是,使用content-available = 0禁用是错误的。要禁用,您必须删除有效负载中的密钥。
此外,验证您的有效负载中是否存在以下密钥
{
"aps": {
"alert": {
"title": "", (“notification_title” will be here)
"subtitle": "",
"body": “”(“description” will be here)
},
"badge": 1,
"sound": "default",
"content-available": 1
}
}
第3。您的通知行为将如下所示:
App位于前景
application:didReceiveRemoteNotification:fetchCompletionHandler:
被称为应用在背景中
application:didReceiveRemoteNotification:fetchCompletionHandler:
被称为应用已暂停
application:didReceiveRemoteNotification:fetchCompletionHandler:
被称为应用未运行,因为已被用户杀死