应用程序未运行时的本地通知 [英] Local notification while app not running

查看:58
本文介绍了应用程序未运行时的本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以显示来自应用程序的某种本地通知,即使应用程序没有运行(也不在后台)?

Is it possible to show some kind of a local notification from an app, even if the app isn't running(also not in background)?

例如每日提醒或类似的东西.我知道可以使用推送通知,但它不适合我的应用.

For example a daily reminder or something like that. I know that it is possible with push-notifications, but it doesn't fit for my app.

推荐答案

您可以轻松安排本地通知,无论应用处于何种状态,它们都会在安排的日期和时间显示.

You can easily schedule local notifications, and they will be presented at the scheduled date and time regardless of the app's state.

首先,您需要获得用户的许可才能显示通知,如下所示:

First you need to get permission from the user to present notifications, like this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound|UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
    return true
}

然后你像这样创建通知:

Then you create the notification like this:

var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "This is"
localNotification.alertBody = "A notification"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 15)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

看看本地和远程通知编程指南.

这篇关于应用程序未运行时的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆