How to implement local notification in Swift
Tech-Today

How to implement local notification in Swift


The following steps will guide us on how to develop an application in Swift that implements Local Notification.

Requirements are:

  • Mac
  • Xcode6 - BetaX
  • iOS8

Steps

  1. Open xcode.
  2. Create a Single View Application, let's name it LocalNotification.
  3. Register for user notification.
    1. In the newly created project, open AppDelegate.swift
    2. Insert the lines below in: didFinishLaunchingWithOptions
    3. application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
      UIUserNotificationType.Badge, categories: nil
      ))
  4. To show an alert view when we received a local notification, we need to override: didReceiveLocalNotification,
  5. func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    print("received local notification")

    application.applicationIconBadgeNumber = 0

    var alert = UIAlertView()
    alert.title = "Alert"
    alert.message = notification.alertBody
    alert.addButtonWithTitle("Dismiss")

    alert.show()
    }
  6. To demo local push notification in our app, we override ViewController.swift viewDidLoad method. So open this file and paste the lines below:
  7. override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after 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 sound
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.alertAction = "View"

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    }
  8. To test if our app works, open the app and let it open the view. Then open another app, or just hide our app. After 30 seconds you should received a push notification alert.
  9. Note that local notification only works on real iPhone device.
Github code: https://github.com/czetsuya/Swift-Local-Notification-Demo




    - Swift - Displaying Actionsheet For Ipad And Iphone
    Swift - Displaying Actionsheet for Ipad and IphoneA simple tutorial on making an alert view of type action sheet that works for iphone and ipad. 1.) First you'll need a button where you can attach your pop over when you're using iPad (in iPhone...

    - How To Display An Alert View With Swift
    Display an Alert View with Swift First we'll create a constant of UIAlertController. Since we'll be using an alert, the UIAlertControllerStyle is set to default. It can have other values such as ActionSheet, Alert, and RawValue.  let alertPrompt...

    - Xcode: Create A Simple Alertview
    Open Xcode and create a new Single View Application. For product name, use SimpleAlertView and then fill out the Organization Name and Company Identifier with your customary values. Select iPhone for Devices. Drag a button inside the view controller...

    - Webview Tutorial In Ios 8 With Swift
    The UIWebView class is use to embed web content in an application. It can be done by simply creating a UIWebView object and attaching it to a window and then sending it a request to load web content. Open Xcode and create a new Single View Application....

    - How To Push External Jar Into Artifactory Using Maven
    This code assumes that you already have artifactory setup. And your local settings.xml allows you to push external jars into it. To do that you only need to setup your artifactory's username and password.  Here is the code, it uploads the file...



    Tech-Today




    



    .