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:
- Open xcode.
- Create a Single View Application, let's name it LocalNotification.
- Register for user notification.
- In the newly created project, open AppDelegate.swift
- Insert the lines below in: didFinishLaunchingWithOptions
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
UIUserNotificationType.Badge, categories: nil
))
- To show an alert view when we received a local notification, we need to override: didReceiveLocalNotification,
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()
}
- To demo local push notification in our app, we override ViewController.swift viewDidLoad method. So open this file and paste the lines below:
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)
}
- 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.
- 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