如何在应用程序处于非活动状态时获取推送通知ios swift3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在应用程序处于非活动状态时获取推送通知ios swift3相关的知识,希望对你有一定的参考价值。
我可以在应用处于打开状态时获取推送通知。但是当我的ios本机应用程序处于非活动状态时,通知不会触发。我也分享了我的源代码。任何人都可以指导我如何完成这项任务吗?
extension AppDelegate : MessagingDelegate {
// [START refresh_token]
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: (fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: (remoteMessage.appData)")
//creating the notification content
let content = UNMutableNotificationContent()
content.userInfo = ["title": remoteMessage.appData["title"] as Any]
content.subtitle = (content.userInfo["title"] as? String)!
// content.body = (content.userInfo["message"] as? String)!
content.badge = 1
content.sound = UNNotificationSound.default()
//getting the notification trigger
//it will be called after 5 seconds
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
//getting the notification request
let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
//adding the notification to notification center
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
答案
您需要执行以下步骤:
注册推送通知:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
setupNotifications(application)
return true
}
func setupNotifications(_ application: UIApplication) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in }
application.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Notifications registration succeeded!")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Notifications registration failed!")
}
}
2)代表方法
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
//Notification message clicked
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
}
以上是关于如何在应用程序处于非活动状态时获取推送通知ios swift3的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序处于非活动状态时,GCM 推送通知不适用于 iOS
当应用程序处于非活动状态并运行代码时,Swift iOS 应用程序会收到推送通知