2016-07-14 103 views
3

我目前有一個iOS應用程序已經設置爲接收推送通知。我想要做的是,在用戶按下「加入」按鈕後,我想向他們發送「歡迎」推送通知。有關如何做到這一點的任何線索?如何在點擊按鈕後發送推送通知?

回答

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

0

在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