【问题标题】:Local banner notification for terminating App终止应用程序的本地横幅通知
【发布时间】:2017-02-21 12:49:18
【问题描述】:

我想在用户退出应用程序时显示横幅通知。在点击该横幅时,我希望我的应用程序打开。

func showBanner() {

    UIApplication.shared.cancelAllLocalNotifications()

    let notif = UILocalNotification.init()
    notif.alertBody = "Your Message Here..."
    localNotif.soundName = UILocalNotificationDefaultSoundName

    UIApplication.shared.presentLocalNotificationNow(notif)
}

如果我将此代码放在applicationDidEnterBackground 中,它可以正常工作;但是,当我把它放在applicationWillTerminate 中时,它没有。

有什么解决方法吗?

编辑:

添加了fireDate,如下所示:

var dc = DateComponents()
dc.second = 2 // 2 seconds from current date time
notif.fireDate = Calendar.current.date(byAdding: dc, to: Date())

还是不行。有什么想法吗?

【问题讨论】:

  • 你在哪里称呼这个showBanner
  • @Anbu.Karthik 你提供的链接是applicationWillTerminate。但是,它不起作用。这就是问题所在。
  • @NazmulHasan 发布了答案。

标签: ios swift uilocalnotification


【解决方案1】:

当我在info.plist 中为Requires Background Modes 设置App 使用CoreBluetooth 进行通信时,相同的代码有效。

得到This Answer的提示

希望苹果不会拒绝我的应用。

【讨论】:

    【解决方案2】:

    不保证applicationWillTerminate 会被调用,这可能是您的代码不起作用的原因。因此,如果您需要在应用程序存在之前做任何事情,请致电 applicationDidEnterBackground

    更多检查iOS - How to guarantee that applicationWillTerminate will be executed

    【讨论】:

    • 但是,我希望通知仅在应用程序终止时出现。不是在后台时。有什么解决方法吗?
    • 我怎么知道?我什至没有那么多代表可以否决。 :D 顺便说一句,您可以将答案发布为问题的评论,因为它实际上不是答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2017-09-20
    • 2018-05-04
    相关资源
    最近更新 更多