How To Implement Local Notification Inward Swift

The next steps volition direct us on how to educate an application inward Swift that implements Local Notification.

Requirements are:

  • Mac
  • Xcode6 - BetaX
  • iOS8

Steps

  1. Open xcode.
  2. Create a Single View Application, let's nurture it LocalNotification.
  3. Register for user notification.
    1. In the newly created project, opened upward AppDelegate.swift
    2. Insert the lines below in: didFinishLaunchingWithOptions
    3. application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |             UIUserNotificationType.Badge, categories: naught             )) 
  4. To demo an alarm thought when nosotros received a local notification, nosotros necessitate to override: didReceiveLocalNotification,
  5. func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {  print("received local notification")    application.applicationIconBadgeNumber = 0    var alarm = UIAlertView()  alert.title = "Alert"  alert.message = notification.alertBody  alert.addButtonWithTitle("Dismiss")    alert.show() } 
  6. To demo local force notification inward our app, nosotros override ViewController.swift viewDidLoad method. So opened upward this file too glue the lines below:
  7. override func viewDidLoad() {  super.viewDidLoad()  // Do whatsoever additional setup subsequently loading the view, typically from a nib.    var localNotification = UILocalNotification()  localNotification.fireDate = NSDate(timeIntervalSinceNow: 30)  localNotification.alertBody = "Hello World"  localNotification.timeZone = NSTimeZone.defaultTimeZone()  localNotification.applicationIconBadgeNumber = 1
            //play a audio  localNotification.soundName = UILocalNotificationDefaultSoundName;  localNotification.alertAction = "View"    UIApplication.sharedApplication().scheduleLocalNotification(localNotification) } 
  8. To exam if our app works, opened upward the app too permit it opened upward the view. Then opened upward roughly other app, or only cover our app. After xxx seconds you lot should received a force notification alert.
  9. Note that local notification alone industrial plant on existent iPhone device.
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.