我目前有一个已经设置为接收推送通知的iOS应用程序。在用户按下“加入”按钮后,我尝试做的是做什么。按钮,我想给他们发一个"欢迎"推送通知。关于如何做到这一点的任何线索?
答案 0 :(得分:3)
这很简单。
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
。
答案 1 :(得分:0)
在YouTube上,Jared Davidson提供了一些很棒的iOS教程。 他有两个通知:
这就是你需要的:
https://www.youtube.com/watch?v=tqJFJzUPpcI
...还有一个用于远程通知(不带按钮)