2016-05-19 10 views
0

私は迅速で新しく、ローカル通知を実装する方法がわかりませんいくつかのコードを試しましたが、正確には機能しないので、誰でもiOSを使用してswiftでローカル通知を実装できますか?速報を使用したローカル通知

+2

これについてはGoogleのチュートリアルがたくさんあります。イベントAppleのドキュメンテーションは、どのように動作するかを説明します。 –

+0

[UNUserNotificationCenter](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter)のローカル通知について[このSwift 3の例](https://stackoverflow.com/a/45247943/4754881)をご覧ください。 。 –

答えて

3

オンラインでたくさんのチュートリアルがありますが、これはGoogleだけでも可能です。ここで

はチュートリアルです:http://jamesonquave.com/blog/local-notifications-in-ios-8-with-swift-part-1/

もここでローカル通知の例です:

let notification = UILocalNotification() 
notification.fireDate = date 
notification.alertBody = "Alert!" 
notification.alertAction = "open" 
notification.hasAction = true 
notification.userInfo = ["UUID": "reminderID" ] 
UIApplication.sharedApplication().scheduleLocalNotification(notification) 
26

ここで私は例を共有しています、

ローカル通知のための登録、

@IBAction func registerLocal(sender: AnyObject) { 
    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 
} 

ローカル通知をスケジュールする、

@IBAction func scheduleLocal(sender: AnyObject) { 
    let notification = UILocalNotification() 
    notification.fireDate = NSDate(timeIntervalSinceNow: 5) 
    notification.alertBody = "Hey you! Yeah you! Swipe to unlock!" 
    notification.alertAction = "be awesome!" 
    notification.soundName = UILocalNotificationDefaultSoundName 
    notification.userInfo = ["CustomField1": "w00t"] 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 


    guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return } 

    if settings.types == .None { 
     let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
     return 
    } 

} 

5 secondsの後にローカル通知が発生します。

Appcodaのようなチュートリアルもあります。ちょうどあなたが多くを得るgoogle

希望:これは役に立ちます:)

関連する問題