如何在点击按钮后发送推送通知?

问题描述:

我目前有一个iOS应用程序已经设置为接收推送通知。我想要做的是,在用户按下“加入”按钮后,我想向他们发送“欢迎”推送通知。有关如何做到这一点的任何线索?如何在点击按钮后发送推送通知?

这很简单。

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

...并有一个用于远程通知(不带按钮)

https://www.youtube.com/watch?v=LBw5tuTvKd4

+0

注意:AppCoda有一个**令人惊叹的**教程:https://www.appcoda.com/push-notification-ios/ – penatheboss

+0

我其实是在寻找推送通知,而不是定期通知,但无论如何感谢。 – SwiftyJD