我目前有一个已经设置好接收推送通知的iOS应用程序。现在我想要实现的是:当用户按下“加入”按钮后,我想要向他们发送一条“欢迎”的推送通知。你有什么办法实现吗?
我目前有一个已经设置好接收推送通知的iOS应用程序。现在我想要实现的是:当用户按下“加入”按钮后,我想要向他们发送一条“欢迎”的推送通知。你有什么办法实现吗?
It's pretty easy.
AppDelegate
:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil))
return true
}
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
print("Local notification received (tapped, or while app in foreground): \(notification)")
}
那么在你的操作中:
@IBAction func welcomeMe(sender: AnyObject) {
let notification = UILocalNotification()
notification.alertBody = "Welcome to the app!" // text that will be displayed in the notification
notification.fireDate = NSDate(timeIntervalSinceNow: 2)
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["title": "Title", "UUID": "12345"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
现在如果应用在后台运行,您会收到推送通知。 如果它在前台运行,则会触发didReceiveLocalNotification
。 点击通知将启动您的应用程序并将其带到前台,并同时触发didReceiveLocalNotification
。
https://www.youtube.com/watch?v=tqJFJzUPpcI
...还有一个用于远程通知的(不带按钮)