在iOS中,有没有一种简便的方法可以在每天的同一时间发送10天的推送通知?我不希望将推送通知发送给所有用户。我的应用程序的工作方式是,用户可以连续十天选择一次推送通知。您是否为此建议任何API?还是有办法处理本地推送通知(在Swift中)?
谢谢!
最佳答案
尝试这个
在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)")
}
}
}
关于ios - 每日推送通知,同时持续10天,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43929331/