2016-07-14 5 views

Antwort

3

Es ist ziemlich einfach.

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)") 
} 

Dann in Ihrer Aktion:

@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) 
} 

Nun, wenn die App im Hintergrund ist, sehen Sie eine Push-Benachrichtigung. Wenn es im Vordergrund ist, wird stattdessen Ihre didReceiveLocalNotification ausgelöst. Wenn du auf die Benachrichtigung tippst, wird deine App im Vordergrund angezeigt und auch didReceiveLocalNotification ausgelöst.

0

Auf YouTube bietet Jared Davidson einige großartige iOS-Tutorials an. Er hat zwei auf Meldungen:

Dieser ist genau das, was Sie brauchen:

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

... und es gibt eine für die Fernmeldungen (nicht mit einer Taste)

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

+0

Hinweis: AppCoda hat eine ** erstaunliche ** Tutorial: https://www.appcoda.com/push-notification-ios/ – penatheboss

+0

Ich bin eigentlich auf der Suche nach Push-Benachrichtigungen nicht regelmäßig Benachrichtigungen, aber trotzdem danke. – SwiftyJD

Verwandte Themen