菜鸟教程小白 发表于 2022-12-11 20:40:56

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


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

<pre><code>extension AppDelegate : MessagingDelegate {
//
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    print(&#34;Firebase registration token: \(fcmToken)&#34;)
    let dataDict: = [&#34;token&#34;: fcmToken]
    NotificationCenter.default.post(name: Notification.Name(&#34;FCMToken&#34;), object: nil, userInfo: dataDict)
}

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print(&#34;Received data message: \(remoteMessage.appData)&#34;)
    //creating the notification content
    let content = UNMutableNotificationContent()
    content.userInfo = [&#34;title&#34;: remoteMessage.appData[&#34;title&#34;] as Any]
    content.subtitle = (content.userInfo[&#34;title&#34;] as? String)!
    // content.body = (content.userInfo[&#34;message&#34;] 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: &#34;SimplifiedIOSNotification&#34;, content: content, trigger: trigger)
    UNUserNotificationCenter.current().delegate = self
    //adding the notification to notification center
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要执行以下步骤:</p>

<p><strong>注册推送通知:</strong></p>

<pre><code>class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; 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(&#34;Notifications registration succeeded!&#34;)
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
      print(&#34;Notifications registration failed!&#34;)
    }

}
</code></pre>

<p>2) <strong>委托(delegate)方法</strong></p>

<pre><code>extension AppDelegate: UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -&gt; Void) {

      //Notification message clicked
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options:   UNNotificationPresentationOptions) -&gt; Void) {

      completionHandler([.alert, .badge, .sound])
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在应用程序处于非事件状态时获取推送通知 ios swift3,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52758736/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52758736/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在应用程序处于非事件状态时获取推送通知 ios swift3