【发布时间】:2016-07-14 20:38:31
【问题描述】:
我目前有一个已设置为接收推送通知的 iOS 应用程序。我想要做的是,在用户按下“加入”按钮后,我想向他们发送“欢迎”推送通知。关于如何做到这一点的任何线索?
【问题讨论】:
标签: ios swift push-notification
我目前有一个已设置为接收推送通知的 iOS 应用程序。我想要做的是,在用户按下“加入”按钮后,我想向他们发送“欢迎”推送通知。关于如何做到这一点的任何线索?
【问题讨论】:
标签: ios swift push-notification
这很容易。
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。
【讨论】:
在 YouTube 上,Jared Davidson 提供了一些很棒的 iOS 教程。 他有两个通知:
这正是你所需要的:
https://www.youtube.com/watch?v=tqJFJzUPpcI
...还有一个用于远程通知(不带按钮)
【讨论】: