应用程序打开时,ios 9推送通知未显示

问题描述 投票:0回答:3
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()
        }

这是我的许可代码。

ios swift2
3个回答
3
投票

如果您的应用已经打开,则不会显示任何通知,但应用程序负责更新用户界面或下载新内容。

实现application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 来处理通知,如documentation中所述。在这里,您可以创建一个警报,显示标题和消息,类似于您在收到标准通知时看到的横幅。


0
投票

要在运行应用程序时显示警报视图,您必须使用:

func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject])

通知有效负载在userInfo变量中。你必须决定如何处理它,如何显示它等。

文件说:

当操作系统提供本地通知或远程通知且目标应用程序未在前台运行时,它可以通过警报,图标标记号或声音向用户显示通知。如果存在通知警报并且用户点击或单击操作按钮(或移动操作滑块),则应用程序启动并调用方法以传入本地通知对象或远程通知有效负载。如果应用程序在传递通知时在前台运行,则应用程序委托会收到本地或远程通知。

这意味着,当应用程序正在运行并收到通知时,不会显示系统警报。


0
投票
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])
}
© www.soinside.com 2019 - 2024. All rights reserved.