The next steps volition direct us on how to educate an application inward Swift that implements Local Notification.
Requirements are:
- Open xcode.
- Create a Single View Application, let's nurture it LocalNotification.
- Register for user notification.
- In the newly created project, opened upward AppDelegate.swift
- Insert the lines below in: didFinishLaunchingWithOptions
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: naught ))
- To demo an alarm thought when nosotros received a local notification, nosotros necessitate to override: didReceiveLocalNotification,
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() }
- To demo local force notification inward our app, nosotros override ViewController.swift viewDidLoad method. So opened upward this file too glue the lines below:
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) }
- 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.
- Note that local notification alone industrial plant on existent iPhone device.
0 komentar:
Please comment if there are any that need to be asked.