iOS / Obj-C - 没有计时器的Fire本地通知? [英] iOS/Obj-C - Fire local notification without timer?

查看:88
本文介绍了iOS / Obj-C - 没有计时器的Fire本地通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的iOS应用程序中设置一些本地通知,但是我似乎在实现这些时发现的每个教程似乎只允许我根据计时器发出通知(请参阅下面的示例代码)?是否可以在将新数据加载到UITableView时触发本地通知?对于一般性问题很抱歉,但我似乎无法找到关于此的大量文档。如果我的用户点击屏幕时只抓取数据,我也不确定如何才能做到这一点?在ViewController的viewDidLoad中抓取/更新例如数据?

I want to setup some local notifications in my iOS app, however every tutorial I seem to find on implementing these seems to only allow me to fire a notification based on a timer (see sample code below)? Is it possible to fire a local notification for example when new data is loaded to a UITableView? Sorry for the general question, but I can't seem to find a whole lot of documentation on this. I'm also not sure how I could do this if data is only grabbed when my user hits a screen? E.g. data is grabbed/updated in viewDidLoad of ViewController?

-(void)viewDidLoad {

        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = dateTime;
        localNotification.alertBody = [NSString stringWithFormat:@"Alert Fired at %@", dateTime];
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.applicationIconBadgeNumber = 1;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    }


推荐答案

您应该考虑从iOS 10开始不推荐使用 UILocalNotification 类的事实。从iOS 10开始,本地和远程通知之间没有区别。

First of all you should consider the fact that UILocalNotification class that you are using is deprecated starting from iOS 10. Since iOS 10 there is no distinction between local and remote notifications.

UserNotifications 框架(iOS 10.0 +,Swift)中| Objective-C)没有像以前的一些创建 UNNotification 的实例Apple的类似框架/ API的实现。相反, UNNotificationCenter 的实例负责创建通知对象,并在收到新通知时以及显示默认UI之前调用委托方法。有关符合<$ c的要求的详细信息,请参阅 Apple的文档。 $ c> UNUserNotificationCenterDelegate 协议。 我经常最终使用 AppDelegate 作为符合该协议的类

In UserNotifications framework (iOS 10.0+, Swift | Objective-C) one does not create instances of UNNotification like in some of the previous implementations of similar frameworks / API's by Apple. Instead, an instance of UNNotificationCenter is responsible for creating notification objects and calls delegate methods when new notifications are received and before the default UI will be displayed. See Apple's documentation for more details on the requirements for conforming UNUserNotificationCenterDelegate protocol. I often end up using AppDelegate as a class conforming to that protocol

现在返回问题。要首先从应用程序中启动通知创建,您应该创建一个触发器(在您的情况下,您可能希望使用 UNTime Interval Notification Trigger ),然后创建通知请求,最后将请求添加到通知中心单例,可以通过调用 UNUserNotificationCenter.current()来访问。

Now back to the question. To initiate a creation of the notification from within the app first you should create a trigger (in your case you might want to use UNTime​Interval​Notification​Trigger), then create a notification request and finally add the request to the notification center singleton that can be accessed by calling UNUserNotificationCenter.current().

let content = UNMutableNotificationContent()
content.title = "Alert Fired"

创建一个触发器,用于确定UNUserNotificationCenter将调用通知的时刻

Create a trigger that will determine the moment when UNUserNotificationCenter will invoke the notification

let trigger = UNTime​Interval​Notification​Trigger(timeInterval: 0, repeats: false)

创建通知请求。您只创建那些来调用本地通知,因为iOS负责为传入的推送通知创建请求对象。

Create a notification request. You only create those to invoke local notifications, since iOS is responsible for creating request objects for incoming push notifications.

let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)

现在我们必须添加我们对与我们的应用相关联的当前通知中心的请求。

Now we will have to add our request to the current notification center associated with our app.

let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)

在我们的情况下,通知将由UNUserNotificationCenter立即触发。

In our case the notification will be triggered immediately by the UNUserNotificationCenter.

这篇关于iOS / Obj-C - 没有计时器的Fire本地通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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