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

时间:2016-07-14 20:38:32

标签: ios swift push-notification

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

2 个答案:

答案 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

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

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