【问题标题】:How to get a push notification at a set time? (Swift 3)如何在设定的时间获得推送通知? (斯威夫特 3)
【发布时间】:2017-03-31 20:06:07
【问题描述】:

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

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

【问题讨论】:

  • 如果你想使用推送通知,那么你需要在某处的服务器上运行一个进程。您可以安排在特定时间发送本地通知,但测试是在安排通知时设置的,而不是在通知发送时。

标签: ios push-notification swift3 push


【解决方案1】:

这是我之前使用的一些代码,不是百分百你要找的,但希望对你有用 您需要将其修改为每天发送

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
        }
    )
}}

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-05-26
    相关资源
    最近更新 更多