admin管理员组文章数量:1332383
I send APNs notifications to my devices with the CloudKit dashboard. I test with two devices;
Device with Silent Push Working Successfully:
iOS version 16.5.1
(c), Model iPhone Xs
Silent Push Failed Device:
iOS version 15.3.1
, Model iPhone Xr
Normal alert notifications work successfully on both devices. But I can't see any log in my project even though silent push is sent successfully on one device.
What I've Checked:
content-available
is set to1
.My app is not in kill state, it is in foreground or pending in the background.
When I send simple notifications, I can see logs in my swift project and notifications on physical devices without any problem.
No, my certificate has not expired and if there was a problem with it, I would not be able to see the normal notifications.
My phone is not in power saving mode and the “Background App Refresh” option is turned on in the app's settings.
Both devices in charging state.
App capabilities: Background Modes is enabled with
Remote Notifications
,Background Fetch
,Background Processing
.
CloudKit Dashboard After Sending Silent Push:
My AppDelegate Class In Swift Project:
import SwiftUI
import UIKit
import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \(token)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
logInfo("Received Push Notification: \(userInfo)")
if let aps = userInfo["aps"] as? [String: AnyObject], aps["content-available"] as? Int == 1 {
logInfo("This is a silent push notification")
//DO SOME WORK
completionHandler(.newData)
}
completionHandler(.newData)
}
}
本文标签: swiftSilent push notifications not working on some iOS devicesStack Overflow
版权声明:本文标题:swift - Silent push notifications not working on some iOS devices - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742303305a2449420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论