不同日期的本地通知Swift 3

时间:2017-06-14 00:27:24

标签: ios swift3 notifications uilocalnotification unusernotificationcenter

我希望每年在特定日期触发本地通知,但所有通知都有不同的标题和正文,这是我编写的代码,这是完美的但只有一个通知,第二部分是在收到通知我的应用程序之后图标有1,意味着有一个通知如何删除这个?..

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        UIApplication.shared.statusBarStyle = .default
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]){ (allowed, error) in
                UNUserNotificationCenter.current().delegate = self
        scheduleNotification()
        return true
    }



 func scheduleNotification() {

     var date = DateComponents()
     date.year = 2017
     date.month = 6
     date.day = 12
     date.hour = 22
     date.minute = 39

            let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
            let content = UNMutableNotificationContent()
            content.title = "Schedule Notification"
            content.body = "Today is my Birthday"
            content.sound = UNNotificationSound.default()
            content.badge = 1
            let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
            UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
            UNUserNotificationCenter.current().add(request) {(error) in
                if let error = error {
                    print("error: \(error)")
                }
   }

提前感谢和感谢

2 个答案:

答案 0 :(得分:0)

对于重复通知,您必须安排多个通知。你可以这样做。

func scheduleNotification(_ title:String, body:String, dateComponents :DateComponents) {        
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
    let content = UNMutableNotificationContent()
    content.title = title
    content.body = body
    content.sound = UNNotificationSound.default()
    content.badge = 1
    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("error: \(error)")
        }
    }
}

并使用上述功能安排多个这样的通知。

var date = DateComponents()
date.year = 2017
date.month = 6
date.day = 12
date.hour = 22
date.minute = 39    

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
scheduleNotification("Schedule Notification", body: "Today is my Birthday", dateComponents: date)
scheduleNotification("Schedule Notification 2", body: "Today is my Birthday 2", dateComponents: date2)
scheduleNotification("Schedule Notification 3", body: "Today is my Birthday 3", dateComponents: date3)

第二部分问题如何删除徽章图标。在applicationDidFinishLaunching或applicationDidBecomeActive中的某处添加以下代码。

UIApplication.shared.applicationIconBadgeNumber = 0;

答案 1 :(得分:0)

为每个通知设置不同的标识符会为您提供所需的结果。

notificationType-是您在安排通知时可以传递的任何字符串

let request = UNNotificationRequest(identifier: "Medicine Local Notification\(notificationType)", content: content, trigger: trigger)