if (deviceToken == nil) {
print("There is no deviceToken saved yet.")
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
这是我的许可代码。
如果您的应用已经打开,则不会显示任何通知,但应用程序负责更新用户界面或下载新内容。
实现application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
来处理通知,如documentation中所述。在这里,您可以创建一个警报,显示标题和消息,类似于您在收到标准通知时看到的横幅。
要在运行应用程序时显示警报视图,您必须使用:
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
通知有效负载在userInfo
变量中。你必须决定如何处理它,如何显示它等。
文件说:
当操作系统提供本地通知或远程通知且目标应用程序未在前台运行时,它可以通过警报,图标标记号或声音向用户显示通知。如果存在通知警报并且用户点击或单击操作按钮(或移动操作滑块),则应用程序启动并调用方法以传入本地通知对象或远程通知有效负载。如果应用程序在传递通知时在前台运行,则应用程序委托会收到本地或远程通知。
这意味着,当应用程序正在运行并收到通知时,不会显示系统警报。
Need to add the completionHandler part, otherwise push notification is received. but it will be not shown.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}