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

查看:27
本文介绍了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:

我们的应用程序具有警报功能,会提醒我们的客户在特定日期您有此会议.例如,如果用户在 2019 年 1 月 1 日上午 9 点设置闹钟,则必须在当天和时间通知用户此会议.

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 中我们不能这样做,因为当应用程序在后台时它不能运行他自己的代码?所以我有两个基本问题:

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. 要从您的应用在本地安排通知,请遵循此doc.莉>
  2. 对于处理通知和通知相关操作,请遵循此文档.

要在您的 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天全站免登陆