如何在应用程序处于非活动状态时获取推送通知ios swift3

问题描述 投票:2回答:1

我可以在应用处于打开状态时获取推送通知。但是当我的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)
  }
}
ios iphone swift
1个回答
0
投票

您需要执行以下步骤:

注册推送通知:

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])
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.