推送通知不工作迅速
因此,当我的应用程序当前未打开并且收到推送通知时,当横幅出现并按下时,“didrecieveremotenotification”完美无缺。但是当应用程序打开时,根本没有横幅出现。我希望推送通知在应用程序打开时工作。推送通知不工作迅速
我试过使用UNNotificationCenter,但一般都丢失了推送通知。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
if (granted) {
UIApplication.shared.registerForRemoteNotifications()
} else{
print("Notification permissions not granted")
}
})
}
//Called when a notification is delivered to a foreground app.
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
completionHandler([.sound, .alert, .badge])
}
//Called to let your app know which action was selected by the user for a given notification.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) {
completionHandler()
}
更新:UNUserNotificationCenter.current().requestAuthorization
运行
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
时授予许可。
但是,当我不使用UNNotificationCenter并使用旧方法时,它不会失败。
为什么一个方法无法注册远程通知,但另一个不是?
更新2:在模拟器工作大声笑,这就是为什么它失败。但是现在,当我运行 UNUserNotificationCenterDelegate
方法只有
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive
方法的工作,而不是
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent
更新3:我已经跑出去的可能的事情去尝试。什么会阻止代码发射willPresent
而不是didReceive
。这是残酷的。有没有人曾经有过这个问题?
更新4:已解决。我用于推送通知的服务要求用户与服务断开连接以发送推送通知。当你在后台时它会自动断开你的连接,这就是为什么这个工作正常,并且它不在前台工作。
奇怪的方式为他们配置,我可能会发一封电子邮件。
实施UNUserNotificationCenterDelegate
委托方法获取通知(托盘顶部),而应用程序在前台。但它只会与IOS 10一起工作。
在你的didFinishLaunchingWithOptions
方法集UNUserNotificationCenterDelegate
委托像这样。
UNUserNotificationCenter.current().delegate = self
实现委托方法...
//Called when a notification is delivered to a foreground app.
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {
completionHandler([.sound, .alert, .badge])
}
//Called to let your app know which action was selected by the user for a given notification.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) {
completionHandler()
}
一切看起来不错..尝试删除该应用。 – Bilal
试过已经 – NickPali
可以提供'didFailToRegisterForRemoteNotificationsWithError'错误吗? – Imotep