标签: unnotificationtrigger

Swift 中随机重复本地通知

TL;DR:是否可以在不使用APNS(或替代推送通知服务)的情况下获得重复的随机本地通知?

我正在构建一个Core Data包含一堆对象的应用程序。(我们将它们称为小部件。)每天的某个时间(假设是中午)我想发送一条通知以查看其中一个小部件。

didFinishLaunchingWithOptions我中检查以确保通知已启用并设置委托等,然后调用函数来构建本地通知。所有这一切都运行良好。

    var date = DateComponents()
    date.hour = 12
    date.minute = 00
    let content = UNMutableNotificationContent()
    content.title = "You should see this widget!"
    content.userInfo = ["widgetID": widget.id]
    content.body = "Today's Widget:\n\(widget.title!)"
    content.sound = UNNotificationSound.default()
    let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
    let request = UNNotificationRequest(identifier: notificationIdentifer, content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error : Error?) in if let theError = error { print(theError.localizedDescription)}}
Run Code Online (Sandbox Code Playgroud)

我的委托处理程序也正常工作:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse, …
Run Code Online (Sandbox Code Playgroud)

notifications apple-push-notifications swift unusernotificationcenter unnotificationtrigger

7
推荐指数
0
解决办法
1588
查看次数

安排本地通知每天从明天开始在Swift中重复

我正在尝试安排每天(在特定时间,但从明天开始)触发本地通知。

即“从明天起每天晚上8点触发通知”

我一直在使用这个 SO问题作为指导,我相信我正在按照它说的做,但是今天当我运行以下代码时(如果我将通知安排在晚上8点之前),我仍然会收到通知:

    func testDateNotification(){

    let content = UNMutableNotificationContent()
    content.title = "Test"
    content.body = "This is a test"
    let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())

    let userCalendar = Calendar.current
    var components = userCalendar.dateComponents([.hour, .minute], from: tomorrow!)

    components.hour = 20
    components.minute = 00


    let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
    let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request) { (error) in
        if ((error) != nil){
            print("Error \(String(describing: error))")
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

ios uilocalnotification swift unnotificationtrigger

6
推荐指数
1
解决办法
2721
查看次数

跨时区触发UNNotification

我无法跨时区正确触发iOS 10本地通知(UNNotification)。

在+10小时的时区中,我希望它在17:00:00触发并安排它。

稍后,当我查看计划的通知时,我会得到它的下一个触发日期,仍在时区+10小时,它将显示17:00:00。

但是,当我在设备上将时区更改为+8小时,并查看预定的通知时,我会得到它的下一个触发日期,并且仍然显示 17:00:00。我希望它能在两个小时前的15:00:00触发。

如何使它像这样工作?我试图改变用于创建送入UNCalendarNotificationTrigger日期组件的日历时区(试过的事情是:TimeZone.init(identifier: "Somewhere")TimeZone.autoupdatingCurrent,可能还有其他人)。但这不会改变,即使我更改了时区,我也总是得到完全相同的触发日期。

timezone ios unnotificationrequest unnotificationtrigger

5
推荐指数
1
解决办法
728
查看次数

Swift UNUserNotification不触发声音

这是我的代码 UNUserNotification

     func scheduleNotification() {
        UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
        switch notificationSettings.authorizationStatus {
        case .notDetermined:
            self.requestAuthorization(completionHandler: { (success) in
                guard success else { return }

                // Schedule Local Notification
                self.scheduleLocalNotification()
            })

        case .authorized:
            // Schedule Local Notification
            self.scheduleLocalNotification()


        case .denied:
            print("Application Not Allowed to Display Notifications")
        }
      }
   }

    private func scheduleLocalNotification() {
        // Create Notification Content
        let notificationContent = UNMutableNotificationContent()

        // Configure Notification Content
        notificationContent.title = "Hello"
        notificationContent.body = ""

        // Add Trigger
        let notificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false) …
Run Code Online (Sandbox Code Playgroud)

ios swift unnotificationrequest unnotificationtrigger

2
推荐指数
1
解决办法
1870
查看次数

每 14 天(每两周)触发一次 UILocalNotification Swift

这个问题已经在 SO 上得到了回答。这是参考:

iOS 通知触发器:每两周和/或每季度一次

但到目前为止我已经尝试过:

var fortnightPart1 = DateComponents()
fortnightPart1.weekday = Calendar.current.component(.day, from: Sdate) //(Day..here Sunday)
fortnightPart1.weekdayOrdinal = 2 //= n == (nth Day in the month...here 2nd Sunday in every month month)
fortnightPart1.hour = Calendar.current.component(.hour, from: Sdate)
fortnightPart1.minute = Calendar.current.component(.minute, from: Sdate)
fortnightPart1.second = Calendar.current.component(.second, from: Sdate)
trigger = UNCalendarNotificationTrigger(dateMatching: fortnightPart1, repeats: repeate)
Run Code Online (Sandbox Code Playgroud)

我无法在 14 天后实现它。虽然,它来自一个随机数据。任何人都可以检查代码有什么问题吗?

更新:

根据您的建议(/sf/answers/3225074821/),这是最终有效的代码!

//for 1st time notification
 let center = UNUserNotificationCenter.current()
 let content = UNMutableNotificationContent()
 content.title = title
 content.body = body …
Run Code Online (Sandbox Code Playgroud)

calendar uilocalnotification swift3 unnotificationrequest unnotificationtrigger

2
推荐指数
1
解决办法
1699
查看次数