首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建不需要在Apple提醒应用程序中显示的提醒

如何创建不需要在Apple提醒应用程序中显示的提醒
EN

Stack Overflow用户
提问于 2020-01-06 08:27:11
回答 1查看 168关注 0票数 -4

使用下面的代码,我可以成功地创建一个提醒事件,并向它添加一个警报,在创建事件后10秒触发警报。我不喜欢这个提醒的创建方式,因为它显示在苹果的提醒应用程序中,当你在设备中收到通知信息时,它会显示提醒程序的图标。

是否有可能将提醒设置为私有,这样它就不会显示在苹果的提醒应用程序中?如果没有,我有什么选择来完成这样的任务?

注意:我不介意在标准提醒本地数据库中存储提醒,只要它们不显示在默认提醒应用程序中。

代码语言:javascript
复制
import EventKit

class ViewController: UIViewController{
    var eventStore = EKEventStore()

    override func viewDidLoad(){
        super.viewDidLoad()
        // get user permission
        eventStore.requestAccess(to: EKEntityType.reminder, completion: {(granted, error) in
            if !granted{
                print("Access denied!")
            }
        })
    }

    @IBAction func createReminder(_ sender: Any) {
        let reminder = EKReminder(eventStore: self.eventStore)
        reminder.title = "Get Milk from the Store"
        reminder.calendar = eventStore.defaultCalendarForNewReminders()
        let date = Date()
        let alarm = AKAlarm (absoluteDate: date.addingTimeInterval(10) as Date)
        reminder.addAlarm(alarm)

        do {
            try eventStore.save(reminder, commit: true)
        } catch let error {
            print("Error: \(error.localizedDescription)")
        }
    }
}

FYI-要使上述代码正常工作,需要在info.plist文件中添加NSRemindersUsageDescription键。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-15 10:40:02

我要说的是,这正是我要找的。

代码语言:javascript
复制
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        if granted{
            print("User gave permissions for local notifications")
        }else{
            print("User did NOT give permissions for local notifications")
        }
    }
    return true
}


 override func viewDidLoad() {
    super.viewDidLoad()
    setReminderAtTime()
} 

func setReminderAtTime(){
    let reminderTime:TimeInterval = 60

    let center = UNUserNotificationCenter.current()
    center.removeAllPendingNotificationRequests()
    
    let content =  UNMutableNotificationContent()
    content.title = "Title"
    content.body = "Notification Message!."
    content.sound = .default
    
    let trigger =  UNTimeIntervalNotificationTrigger(timeInterval: reminderTime, repeats: false)
    let request = UNNotificationRequest(identifier: "reminderName", content: content, trigger: trigger)
    
    center.add(request) { (error) in
        if error != nil{
            print("Error = \(error?.localizedDescription ?? "error local notification")")
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59605407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档