iOS如何设置闹钟并安排工作/通知 [英] iOS how to set Alarm and schedule work/notifications

查看:173
本文介绍了iOS如何设置闹钟并安排工作/通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是iOS新手,我是android开发人员。但是现在我正在开发iOS应用,它只是android的iOS复制。让我告诉你我想要在应用程序中做什么:

Hi I am new to iOS basically I am android developer. But Right now I am working on iOS app,It is simply an iOS replica of android. Let me tell you about what I want in app:


我们的应用程序具有警报功能,该警报将提醒我们的客户在特定日期这个会议。例如,如果用户在上午9:00将警报设置为2019年1月1日,则必须在该日期和时间将该会议通知给用户。

Our app features alarm, that will remind our client that on a specific date you have this meeting. For example, if user sets alarm for 1-jan-2019 at time 9:00AM then on that day and time user must be notified of this meeting.

我读了很多书,发现在iOS中我们无法做到这一点,因为当应用程序在后台运行时,它无法运行其代码拥有?所以我有2个基本问题:

I have read alot and found that in iOS we can not do this since when app is in background it can not run code of his own? So I have 2 basic questions:

我想要什么:


  1. 首先如何安排警报

  1. First of all how to schedule an Alarm

如果设置了警报且应用程序在后台/终止,则如何生成通知以及当用户单击通知时他有特定的看法?

If alarm is set and app is in background/terminated then how to generate notification and when user click on notification take him to specific view?

我知道这是3个主要部分需要太多编码。但是我只想要方向。给我链接代码块。我正在使用xcode 9.2和swift 4.0。预先感谢...

I know these are 3 main and major part that required too much coding. But I just want directions. Give me link of chunks of code. I am using xcode 9.2 and swift 4.0. Thanks in advance ...

推荐答案

您必须安排本地通知,该通知现在可在 UNUserNotificationCenter中使用

You many have to schedule local notification which is now available in UNUserNotificationCenter.

因此,


  1. 用于计划a从您的应用本地通知,请遵循此 doc

  2. 对于处理通知和与通知有关的操作,请遵循以下 doc

  1. For Scheduling a Notification Locally from Your App, follow this doc.
  2. For Handling Notifications and Notification-Related Actions, follow this doc.

要在 AppDelegate 或要处理 UNUserNotificationCenter 委托方法,添加以下代码:

To Handle Notifications in your AppDelegate or where you want to handle UNUserNotificationCenter delegate method, add below code:

class AppDelegate:NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate{

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {


    let center = UNUserNotificationCenter.current()
    center.delegate = self // Don't forgot to set delegate

    //To get permissions from user:
    let options: UNAuthorizationOptions = [.alert, .sound, .badge];
    center.requestAuthorization(options: options) {
        (granted, error) in
        if !granted {
            print("Something went wrong")
        }
    }

    return true
}
}

这篇关于iOS如何设置闹钟并安排工作/通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆