ios - 如何在应用程序处于非事件状态时获取推送通知 ios swift3
<p><p>我可以在应用打开时获取推送通知。但是当我的 iOSnative 应用程序处于非事件状态时,不会触发通知。
我也分享了我的源代码。谁能指导我如何完成这项任务?</p>
<pre><code>extension AppDelegate : MessagingDelegate {
//
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict: = ["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)
}
}
</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: ?) -> 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!")
}
}
</code></pre>
<p>2) <strong>委托(delegate)方法</strong></p>
<pre><code>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])
}
}
</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]