首页 文章

Swift:如何使用动态内容安排本地通知

提问于
浏览
3

我有一个适用于iOS的天气应用程序,我想让用户每天早上在他们选择的时间收到通知,以获取当天的天气预报并显示通知 .

我想避免使用推送通知,我想我可以使用本地通知,除了我看不到从服务器上获取要显示的内容的方法 . 看起来内容必须在安排时设置 . 是对的吗?

这让我觉得我可以注册我的应用程序以使用后台执行来定期获取天气并安排包含最新内容的通知,但这似乎很浪费 .

简而言之,我想告诉iOS在特定时间运行特定功能 . 我缺少一个很好的选择吗?推送通知是完成此类事情的唯一/最佳方式吗?

3 回答

  • 1

    如果要显示 weather forecast ,推送通知是您的最佳选择 .

    更多关于此:https://stackoverflow.com/a/41901767/3901620

  • 1

    您可以安排特定时间的本地通知,以及用户何时看到该通知以及是否希望他可以通过点击该通知来打开您的应用 . 那时,您将能够知道,用户已经点击了通知,因此应用程序已打开,您可以进行网络调用以获取数据并在应用程序内显示 . This will not require any background calls therefor and only make a network call to an action by a user.

    另一种选择:您可以创建应用程序的小部件(如Weather Widget) . 每当用户进入小部件区域时,您将获得代表呼叫并进行网络呼叫以获取最新的天气数据 . 如果用户想要了解更多信息,他只需点按它即可打开您的应用 . 然后,一切都掌握在你手中 .

    您的选择:只要用户在特定日期打开您的应用并为其设置通知,您就可以始终获得动态内容 . 但这不是可以建议的,因为用户可能无法获得更新的数据 .

    推送通知:但是,如果您想通过服务器将动态数据发送到您的应用,则可能不需要这种情况 . 这始终是最好的选择 .

  • 1

    我创造了一个功能 . 在您需要的时候,它将在特定时间调用您的函数 . 我正在创建一个时钟应用程序,所以我需要在用户创建警报时触发本地通知 . 在通知中心委托方法中,您可以处理您的响应并调用您想要的任何方法 .

    class LocalNotificationMethod : NSObject {
    
    static let notificationInstance = LocalNotificationMethod()
    
    let requestIdentifier = "SampleRequest" //identifier is to cancel the notification request
    
     internal func scheduleLocalNotification(titleOfNotification:String, subtitleOfNotification:String, messageOfNotification:String, soundOfNotification:String, dateOfNotification:String) {
    
        if #available(iOS 10.0, *) {
    
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd hh:mm a"
            let date3 = formatter.date(from: dateOfNotification)
    
            let content = UNMutableNotificationContent()
            content.body = NSString.localizedUserNotificationString(forKey: titleOfNotification, arguments: nil)
            content.sound = soundOfNotification.characters.count > 0 ? UNNotificationSound.init(named: soundOfNotification + ".mp3") : UNNotificationSound.default()
    
            let trigger = UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.day, .month, .year, .hour, .minute], from: date3!), repeats: false)
    
            let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)
    
            UNUserNotificationCenter.current().add(request){(error) in
    
                if (error != nil){
    
                    print(error?.localizedDescription)
                } else {
                    print("Successfully Done")
                }
            }
        } else {
            // Fallback on earlier versions
        }
    
    }
    }
    

    在AppDelegate方法中: - 只要用户点击您的通知或者您的通知何时出现,您就可以处理 . 由您决定要做什么 .

    // MARK: - 通知代表

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
        print("Tapped in notification")
    }
    
    //This is key callback to present notification while the app is in foreground
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    
    
        print("Notification being triggered")
        //You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
        //to distinguish between notifications
        if notification.request.identifier == "SampleRequest" {
    
            completionHandler( [.alert,.sound,.badge])
    
        }
    }
    

相关问题