【问题标题】:Set notifications from iOS extension从 iOS 扩展设置通知
【发布时间】:2018-02-08 17:07:11
【问题描述】:

我试过了

UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

但它不返回任何东西。
另一种方法是向主应用发送消息,以便它可以设置通知,但这意味着我的应用必须监听 Userdefaults 的变化或我的 AppGroup 的变化 有没有人有任何想法或建议? 谢谢

【问题讨论】:

  • 您的要求是什么?
  • 那个函数不应该返回任何东西。什么是你试图做的却不起作用?
  • @TomHarrington 我正在尝试设置来自 ios 扩展的通知,但我没有收到任何信息,甚至没有调用上述函数时出现错误。
  • 我做到了,我可以从扩展设置通知。

标签: ios notifications ios-app-extension ios-extensions ios-app-group


【解决方案1】:

我终于可以从我的扩展程序中设置一个通知,问题是我试图通过一个使用计时器调用的函数来做到这一点。 (所以现在的问题是我的计时器在扩展程序中不起作用)
*这是行不通的*

_ = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(loopInExtension), userInfo: nil, repeats: true)

 @objc func loopInExtension() {
       UNUserNotificationCenter.current().add(request, nil)
}

所以要设置通知,我只需要添加通知,而不是使用计时器。

【讨论】:

  • 我成功设置了一个计时器,我这样做了: var timer = Timer.init(timeInterval: 3, repeats: true, block: { (Timer) in // DO SOMEHING os_log("I可以看到这个") }) RunLoop.main.add(timer, forMode: .defaultRunLoopMode)
  • 但是,如果我在计时器中设置了一个通知,我只会收到 1 个通知,在我关闭它之后,我会在我关闭分机时再次收到另一个通知。我不明白发生了什么......我会做更多的挖掘
猜你喜欢
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多