Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how to set Alarm and schedule work/notifications

Tags:

ios

swift

alarm

Hi I am new to iOS basically I am android developer. But Right now I am working on iOS app,It is simply an iOS replica of android. Let me tell you about what I want in app:

Our app features alarm, that will remind our client that on a specific date you have this meeting. For example, if user sets alarm for 1-jan-2019 at time 9:00AM then on that day and time user must be notified of this meeting.

I have read alot and found that in iOS we can not do this since when app is in background it can not run code of his own? So I have 2 basic questions:

What I want:

  1. First of all how to schedule an Alarm

  2. If alarm is set and app is in background/terminated then how to generate notification and when user click on notification take him to specific view?

  3. If app is in forground then how to take him to wanted view? also if app is on specific view how to update view itself when alarm goes on?

I know these are 3 main and major part that required too much coding. But I just want directions. Give me link of chunks of code. I am using xcode 9.2 and swift 4.0. Thanks in advance ...

like image 597
A.s.ALI Avatar asked Nov 30 '18 11:11

A.s.ALI


2 Answers

You many have to schedule local notification which is now available in UNUserNotificationCenter.

So,

  1. For Scheduling a Notification Locally from Your App, follow this doc.
  2. For Handling Notifications and Notification-Related Actions, follow this doc.

To Handle Notifications in your AppDelegate or where you want to handle UNUserNotificationCenter delegate method, add below code:

class AppDelegate:NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate{

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {


    let center = UNUserNotificationCenter.current()
    center.delegate = self // Don't forgot to set delegate

    //To get permissions from user:
    let options: UNAuthorizationOptions = [.alert, .sound, .badge];
    center.requestAuthorization(options: options) {
        (granted, error) in
        if !granted {
            print("Something went wrong")
        }
    }

    return true
}
}
like image 186
Natarajan Avatar answered Oct 24 '22 12:10

Natarajan


  1. You can use local notifications for getting an alarm notifications.
  2. You can handle the click in the delegate method of UNUserNotificationCenterDelegate and navigate to the desired page.

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 
    
  3. For displaying notification when app is in foreground use this method from same delegate

        func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    
like image 25
Sumeet.Jain Avatar answered Oct 24 '22 12:10

Sumeet.Jain