如何在应用程序在 Objective-C 中运行时获取本地通知 [英] How to get local notification when app is running in Objective-C

查看:29
本文介绍了如何在应用程序在 Objective-C 中运行时获取本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 新手,我遇到了在应用程序运行时显示本地通知的问题.我的代码是这样的

UILocalNotification* localNotification = [[UILocalNotification alloc] init];localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];//在此处输入以秒为单位的时间.localNotification.alertBody = @"消息听到";localNotification.timeZone = [NSTimeZone defaultTimeZone];localNotification.repeatInterval = NSCalendarUnitMinute;//这里重复说明.localNotification.soundName = UILocalNotificationDefaultSoundName;[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

我在单击按钮时编写了此代码,但没有显示通知.提前致谢!

解决方案

不,如果你使用上面的代码,它不会显示.首先我想问一些问题.

如果您使用 iOS 10,您上面的代码是否有效?

你在你的类中在哪里调用上面的代码行或者你应用哪种方法?

iOS 10 有名为 UNUserNotification 的新框架.我们必须导入 UNUserNotificationCenterDelegate 以在应用程序运行时显示 UILocalNotification.

<块引用>

UNUserNotificationCenterDelegate 协议定义了用于响应可操作的通知并接收通知当您的应用在前台时.你分配你的委托对象到共享 UNUserNotificationCenter 的委托属性目的.用户通知中心对象调用您的方法在适当的时候委派传递信息.

然后当通知到达时它会调用 willPresentNotification

<块引用>

如果您的应用在通知到达时处于前台,则通知中心调用以下方法

- (void)userNotificationCenter:(UNUserNotificationCenter *)centerwillPresentNotification:(UNNotification *)通知withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;

<块引用>

以上方法发送通知直接到您的应用程序.如果你实现这个方法,你可以采取处理通知和更新所需的任何操作你的应用程序.完成后,执行 completionHandler 块并指定您希望系统如何提醒用户(如果有的话).如果你的委托未实现此方法,系统将警报静音如果您已将 UNNotificationPresentationOptionNone 选项传递给completionHandler 块.如果您根本不提供委托对于 UNUserNotificationCenter 对象,系统使用通知的原始选项来提醒用户.

如何显示通知?

为了实现这一点,我们有 3 个选择

声音徽章警报

UNNotificationPresentationOptions 帮助我们实现这一点

以上是理解和执行的好方法.

我也更喜欢一些答案.他们是

在应用运行时显示iOS通知

当您的应用打开并处于前台时显示股票 iOS 通知横幅?

获取要显示的本地通知当应用程序在前台时

应用在前台时的通知

iOS 10 中的通知

本地通知

didReceiveRemoteNotification 未调用,iOS 10

I am new in iOS and I am facing problem regarding to show local notification when app is running. My code is like this

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.
localNotification.alertBody = @"Message hear";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

I write this code on the button click but it is not showing me notification. Thanks in Advance!

解决方案

No it does not show if you use above code.First of all I want to ask some questions.

Does your above code work if you use iOS 10?

Where do you call above lines of code in your class or in which method do you apply?

iOS 10 has new framework called UNUserNotification.We have to import the UNUserNotificationCenterDelegate for showing UILocalNotification when app is running.

The UNUserNotificationCenterDelegate protocol defines methods for responding to actionable notifications and receiving notifications while your app is in the foreground. You assign your delegate object to the delegate property of the shared UNUserNotificationCenter object. The user notification center object calls methods of your delegate at appropriate times to deliver information.

Then when notification arrives it calls willPresentNotification

If your app is in the foreground when a notification arrives, the notification center calls below method

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
   willPresentNotification:(UNNotification *)notification 
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;

Above method delivers the notification directly to your app. If you implement this method, you can take whatever actions are necessary to process the notification and update your app. When you finish, execute the completionHandler block and specify how you want the system to alert the user, if at all. If your delegate does not implement this method, the system silences alerts as if you had passed the UNNotificationPresentationOptionNone option to the completionHandler block. If you do not provide a delegate at all for the UNUserNotificationCenter object, the system uses the notification’s original options to alert the user.

How can you show the notification?

For achieving this we have 3 options

Sound
Badge
Alert 

UNNotificationPresentationOptions help us to implement this

Above is the good one to understand and do.

I preferred some answers also. They are

Show iOS notification when app is running

Displaying a stock iOS notification banner when your app is open and in the foreground?

Getting local notifications to show while app is in foreground

Notification while app is in foreground

Notification in iOS 10

Local Notification

didReceiveRemoteNotification not called , iOS 10

这篇关于如何在应用程序在 Objective-C 中运行时获取本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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