Friday, August 28, 2015

How to make an notification on swift

to make a notification app is simple.




1, add code on ViewContrller.swift like this.


import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        var localNotification:UILocalNotification = UILocalNotification()
        localNotification.alertAction = "Testing notifications"
        localNotification.alertBody = "hello world! "
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 30)
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}



2, add code on AppDelegate.swift like this.

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound |
            UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories:nil))
        
        return true
    }


3, run your simulator. Don't forget to waiting at home screen.

I referred here
https://www.topcoder.com/blog/notifications-in-ios-8-using-swift/

No comments:

Post a Comment