每日推送通知同时为期10天

时间:2017-05-12 04:30:58

标签: ios swift

在iOS中,是否有一种简单的方法可以在每天的同一时间发送推送通知10天?我不希望将推送通知发送给所有用户。我的应用程序的工作方式,用户可以连续十天选择一次推送通知。你有没有为此推荐的API?或者有没有办法处理本地推送通知(在Swift中)?

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个

在Appdelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization([.alert, .sound]) { (granted, error) in
         if granted{ 
             // do you work
            }
        }
        return true
    }

常规功能

func scheduleMyNotificationWith(date: Date, body: String) {     
    let triggerWeekly = Calendar.current.dateComponents([.weekday,hour,.minute,.second,], from: date)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)

    let content = UNMutableNotificationContent()
    content.title = "your notification title" // it is mendatory
    content.body = body
    content.sound = UNNotificationSound.default()
    //content.categoryIdentifier = "todoList"

    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().delegate = self
    //UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
      if let error = error {
        print("Somethings went to wrong: \(error)")
      }
    }
  }