如何在设定的时间获得推送通知? (Swift 3)
问题描述:
我正在制作一个应用程序,假设用户每天都会在某个特定时间报导新闻。它通过一个从数组中调用它的函数获取新闻文本。我的问题是:如何让我的应用程序调用该函数,然后每天给我发送一个带有信息文本的推送通知,比如说,凌晨4点?如何在设定的时间获得推送通知? (Swift 3)
感谢大家的回答!祝你有美好的一天!
答
下面是一些代码,我以前用过,也不是百分百你在找什么,但如果你想使用我希望你需要 修改它以每天发送
import UIKit
import UserNotifications
class ViewController: UIViewController, UNUserNotificationCenterDelegate {
var isGrantedNotificationAccess:Bool = false
@IBAction func send10SecNotification(_ sender: UIButton) {
if isGrantedNotificationAccess{
//add notification code here
//Set the content of the notification
let content = UNMutableNotificationContent()
content.title = "10 Second Notification Demo"
content.subtitle = "From MakeAppPie.com"
content.body = "Notification after 10 seconds - Your pizza is Ready!!"
//Set the trigger of the notification -- here a timer.
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 10.0,
repeats: true)
//Set the request for the notification from the above
let request = UNNotificationRequest(
identifier: "10.second.message",
content: content,
trigger: trigger
)
//Add the notification to the currnet notification center
UNUserNotificationCenter.current().add(
request, withCompletionHandler: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound,.badge],
completionHandler: { (granted,error) in
self.isGrantedNotificationAccess = granted
}
)
}}
+0
非常感谢你! – Whazzup
对您有用推送通知,那么你需要在某个服务器上运行一个进程。您可以安排在特定时间发送本地通知,但测试是在通知安排时设定的,而不是在通知发送时设置。 – Paulw11