iOS how to set Alarm and schedule work/notifications

后端 未结 2 1326
[愿得一人]
[愿得一人] 2021-01-14 13:45

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

2条回答
  •  长情又很酷
    2021-01-14 14:26

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

提交回复
热议问题