Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Notification using ios using swift [closed]

I'm new in swift, I don't know how to implement local notification I have tried some code but it's not exactly work, so Anyone can help to implement local Notification in iOS using swift?

like image 571
Nipul Daki Avatar asked May 19 '16 13:05

Nipul Daki


People also ask

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.

What is local notification in iOS Swift?

Use local notifications to get the user's attention. You can display an alert, play a sound, or badge your app's icon. For example, a background app could ask the system to display an alert when your app finishes a particular task. Always use local notifications to convey important information that the user wants.

What is the difference between local notification and push notification?

The difference between the two stems from one major point: Local notifications originate from the application itself, as opposed to Push notifications which are triggered from a remote server.


Video Answer


2 Answers

Here i am sharing example,

Register for local notification,

@IBAction func registerLocal(sender: AnyObject) {
    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
}

Schedule local notification,

@IBAction func scheduleLocal(sender: AnyObject) {
    let notification = UILocalNotification()
    notification.fireDate = NSDate(timeIntervalSinceNow: 5)
    notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
    notification.alertAction = "be awesome!"
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.userInfo = ["CustomField1": "w00t"]
    UIApplication.sharedApplication().scheduleLocalNotification(notification)


    guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return }

    if settings.types == .None {
        let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
        return
    }

}

It will fire local notification after 5 seconds.

Hope this will help :)

like image 80
Ketan Parmar Avatar answered Oct 06 '22 07:10

Ketan Parmar


There are lots of tutorials online for this, you could just Google it.

Here's a tutorial: http://jamesonquave.com/blog/local-notifications-in-ios-8-with-swift-part-1/

Also here's an example of a local notification:

let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = "Alert!"
notification.alertAction = "open"
notification.hasAction = true
notification.userInfo = ["UUID": "reminderID" ]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
like image 36
user1195300 Avatar answered Oct 06 '22 07:10

user1195300