8

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 ...

rmaddy
  • 314,917
  • 42
  • 532
  • 579
A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
  • To learn about Local Notification, check out my git repo: https://github.com/nitin-agam/local-notification-demo – nitin.agam Nov 30 '18 at 12:31

2 Answers2

5

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
}
}
Natarajan
  • 3,241
  • 3
  • 17
  • 34
  • 1
    I would like to show user a picker to set date and then a picker to set time, it is behavior in android can I do this in here? – A.s.ALI Nov 30 '18 at 12:19
  • 1
    Yes of course. You can use UIDatePicker(https://developer.apple.com/documentation/uikit/uidatepicker) or create your own UIPickerView(https://developer.apple.com/documentation/uikit/uipickerview) – Natarajan Nov 30 '18 at 12:23
  • can I ask couple of question more? – A.s.ALI Nov 30 '18 at 12:49
  • yes, please. You can change notification sound in `UNMutableNotificationContent`. – Natarajan Nov 30 '18 at 13:46
  • when My app is in background I get notification, but tell me why I am not getting notification when My app is in forground? – A.s.ALI Dec 03 '18 at 10:30
  • Follow "Handle Notifications While Your App Runs in the Foreground" section in this link:https://developer.apple.com/documentation/usernotifications/handling_notifications_and_notification-related_actions. – Natarajan Dec 03 '18 at 11:12
  • great last question, how to set Date in this, for example 6 am of 09 Dec 2018 ?? I saw examples and it uses date component. Cant I set whole date?? – A.s.ALI Dec 03 '18 at 11:43
  • Cool, If your solution doesn't work, try to Use DateFormatter to get date from whole String. Refer this post https://stackoverflow.com/a/35700409/3420996. – Natarajan Dec 03 '18 at 11:49
  • thats so nice of you, but When APp is in forground, it doesnot return Response in method, so now how to get those things that I sat for notification? – A.s.ALI Dec 03 '18 at 12:17
  • I mean to say the method that has willPresent notification: UNNotification – A.s.ALI Dec 03 '18 at 12:19
  • Please see my edits in answer. You should set center.delegate = self. – Natarajan Dec 03 '18 at 12:31
  • I think you did not get my point, There are 2 functions that get call in 2 different scenario. When your app is in background func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) This functions call – A.s.ALI Dec 03 '18 at 12:35
  • For forground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) this is called. so not in this function I do not have response coming as in above method you can see – A.s.ALI Dec 03 '18 at 12:36
  • Ok, Got it. Both functions are different and will be called in different scenarios. So What do you want to get from response param? – Natarajan Dec 03 '18 at 12:50
  • I am saving my Id of model from db in notification. when app is in background It works bcz it founds that Id but when in forground I cant get it becz there is not response. I am unable to code – A.s.ALI Dec 03 '18 at 12:53
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184629/discussion-between-natarajan-and-shararti-kaki). – Natarajan Dec 03 '18 at 12:57
1
  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)
    
Sumeet.Jain
  • 1,533
  • 9
  • 26
  • 1
    Can we change notification sound? – A.s.ALI Nov 30 '18 at 13:16
  • Yes , you can do it – Amogam Aug 26 '21 at 11:45
  • can this be used to implement like a clock alarm when it rings in a loop quite long and we can put it to stop / snooze ? or more list push notification where it just have a short beep sound ? I would like to implement like an alarm rather than a push notification type of alert. – Axil Dec 31 '21 at 08:08