OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在设定的时间获得推送通知? ( swift 3) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:17
标题: ios - 如何在设定的时间获得推送通知? ( swift 3)

我正在制作一个应用程序,该应用程序假定用户每天在设定的时间了解新闻。它通过从数组调用它的函数获取新闻文本。我的问题是:如何让我的应用程序调用该函数,然后每天凌晨 4 点向我发送带有信息文本的推送通知?

感谢大家的回答!祝你有美好的一天!



Best Answer-推荐答案


这是我以前用过的一些代码。不是你要找的百分之一百,但我希望对你有用。

需要修改为每天发送

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]) { (granted, error) in
            self.isGrantedNotificationAccess = granted
        }
    }
}

关于ios - 如何在设定的时间获得推送通知? ( swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43148864/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4