如何在点击按钮后发送推送通知?
答
这很简单。
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
...并有一个用于远程通知(不带按钮)
注意:AppCoda有一个**令人惊叹的**教程:https://www.appcoda.com/push-notification-ios/ – penatheboss
我其实是在寻找推送通知,而不是定期通知,但无论如何感谢。 – SwiftyJD