Daily notifcations Swift 3

元气小坏坏 提交于 2019-12-12 02:17:08

问题


I'm trying to figure out how to send a notification once a day at a specific time (say 8am) without user input and using the new UNMutableNotificationContent instead of the deprecated UILocalNotification and not using user input to trigger it but rather a time. All of explanations If found are old and don't include ios 10 and swift 3.

What I have so far.

Authorized Notifications in ViewController.swift:

override func viewDidLoad() {

        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in
        })
    }

Notification.swift

let localNotification = UNMutableNotificationContent()
//    localNotification.fireDate = dateFire
    localNotification.title = "title"
    localNotification.body = "body"
    localNotification.badge = 1
    localNotification.sound = UNNotificationSound.default()

I know I have to set a trigger and request among other things but im not sure exactly how to get it to work.


回答1:


Can you please check out this tutorial - Hacking with swift Notification center

What you need is the date components part.

 func scheduleLocal() {
      let center = UNUserNotificationCenter.current()

    let localNotification = UNMutableNotificationContent()
    localNotification.title = "title"
    localNotification.body = "body"
    localNotification.badge = 1
    localNotification.sound = UNNotificationSound.default()

        var dateComponents = DateComponents()
        dateComponents.hour = 10
        dateComponents.minute = 30
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

        let request = UNNotificationRequest(identifier: UUID().uuidString, content: localNotification, trigger: trigger)
        center.add(request)
    }

Check out the tutorial for more. It is written in swift 3 iOS 10. Here's github repo for the same.




回答2:


First step:

import UserNotifications

And to judgement whether the user allows notification

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
granted, error in
    if granted {
        // determine whether the user allows notification
    }
}

Second step:

Create notification

// 1. create notification's content
let content = UNMutableNotificationContent()
content.title = "Time Interval Notification"
content.body = "My first notification"

// 2. create trigger
//custom your time in here
var components = DateComponents.init()
components.hour = 8
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)

// 3. send request identifier
let requestIdentifier = "com.xxx.usernotification.myFirstNotification"

// 4. create send request
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)

// add request to send center
UNUserNotificationCenter.current().add(request) { error in
    if error == nil {
        print("Time Interval Notification scheduled: \(requestIdentifier)")
    }
}

You can find more information at Apple Documentation



来源:https://stackoverflow.com/questions/41787351/daily-notifcations-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!