OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在应用程序处于非事件状态时获取推送通知 ios swift3 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:40
标题: ios - 如何在应用程序处于非事件状态时获取推送通知 ios swift3

我可以在应用打开时获取推送通知。但是当我的 iOS native 应用程序处于非事件状态时,不会触发通知。 我也分享了我的源代码。谁能指导我如何完成这项任务?

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)
  }
}



Best Answer-推荐答案


您需要执行以下步骤:

注册推送通知:

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) 委托(delegate)方法

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 - 如何在应用程序处于非事件状态时获取推送通知 ios swift3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52758736/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4