相关疑难解决方法(0)

当应用程序在前景Swift 3时获取本地通知

显然,ios10现在可以实现这一点:

optional func userNotificationCenter(_ center: UNUserNotificationCenter, 
                 willPresent notification: UNNotification, 
  withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
Run Code Online (Sandbox Code Playgroud)

这个答案基本上说明了做这件事所需的工具:

当您的应用程序处于打开状态且位于前台时显示库存iOS通知横幅?

我只是不太了解如何将它们放在一起.

我不知道这有多重要,但我无法保留可选的func,xcode要我将其切换为私有.

我正试图展示徽章,文档提供

static var badge: UNNotificationPresentationOptions { get }
Run Code Online (Sandbox Code Playgroud)

这里很少丢失.

然后我假设我想要排除某个视图控制器获取这些徽章并且我没有使用导航控制器这个代码我发现它会起作用吗?:var窗口:UIWindow?

if let viewControllers = window?.rootViewController?.childViewControllers {
for viewController in viewControllers {
    if viewController.isKindOfClass(MyViewControllerClass) {
        print("Found it!!!")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ios uilocalnotification swift swift3 unusernotificationcenter

35
推荐指数
4
解决办法
4万
查看次数

在ios10中添加本地通知 - swift 3

编辑:所以把应用程序放在后台就行了.

原版的:

所以我一直在尝试向新的UNUserNotificationCenter添加通知,但我似乎没有得到它.

我的视图控制器有一个动作:

@IBAction func sendPressed(_ sender: AnyObject) {
    let content = UNMutableNotificationContent()

    content.title = "Hello"
    content.body = "What up?"
    content.sound = UNNotificationSound.default()

    // Deliver the notification in five seconds.
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)

    // Schedule the notification.
    let center = UNUserNotificationCenter.current()
    center.add(request) { (error) in
        print(error)
    }
    print("should have been added")
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let center = UNUserNotificationCenter.current()
    center.requestAuthorization([.alert, .sound]) { …
Run Code Online (Sandbox Code Playgroud)

uilocalnotification swift swift3 ios10 unusernotificationcenter

27
推荐指数
2
解决办法
5万
查看次数