如果应用程序终止,则未收到 iOS FCM 推送通知
<p><p>我正在开发一个 Swift 项目并将 FCM 集成到其中。当应用程序运行以及应用程序处于后台状态时,我能够接收推送通知。但有时当我终止(强制关闭)应用程序,然后从控制台发送通知时,不会显示任何通知。</p>
<p>我正在使用 iOS 10 并在 didFinishLaunchingWithOptions 中实现以下代码:</p>
<pre><code>UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if error == nil {
UIApplication.shared.registerForRemoteNotifications()
if granted {
print("Notification access true!")
} else {
print("Notification access false")
}
}
}
</code></pre>
<p>我还实现了 UNUserNotificationCenterDelegate 及其方法。</p>
<pre><code>func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
print(userInfo)
self.handleOnNotifClick()
completionHandler()
}
</code></pre>
<p>仅当从最近使用的应用程序抽屉中强制关闭应用程序时才会发生这种情况。请调查一下。任何帮助将不胜感激。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>解决了!!!</p>
<pre><code>func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: ,fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .active{
if let notification = userInfo["aps"] as? ,
let alert = notification["alert"] as? String {
print(alert)
let localNotification = UILocalNotification()
localNotification.alertBody = alert
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(localNotification)
}
}else if state == .inactive{
if let notification = userInfo["aps"] as? ,let alert = notification["alert"] as? String {
print(alert)
let localNotification = UILocalNotification()
localNotification.alertBody = alert
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(localNotification)
}
}else if state == .background{
UIApplication.shared.applicationIconBadgeNumber = 0
if let notification = userInfo["aps"] as? ,let alert = notification["alert"] as? String,let sound = notification["sound"] as? String{
print(alert)
var localNotification = UILocalNotification()
localNotification.alertBody = alert
localNotification.soundName = sound
UIApplication.shared.scheduleLocalNotification(localNotification)
}
}
}
</code></pre></p>
<p style="font-size: 20px;">关于如果应用程序终止,则未收到 iOS FCM 推送通知,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43068408/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43068408/
</a>
</p>
页:
[1]