当我打开通知托盘以查看通知时,UILocalNotification会触发 [英] UILocalNotification fire when i open the notification tray to see the notification

查看:59
本文介绍了当我打开通知托盘以查看通知时,UILocalNotification会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了本地通知并安排了开火日期,但是当应用程序处于后台并且我打开通知托盘以查看通知时,本地通知会自动触发,但是火灾日期仍然存在。是否有任何解决方案解决这个问题

i used the local notification and schedule the fire date but when the app is in background and i open the notification tray to see the notification then the local notification is fire automatically but the fire date is remaining..is there any solution to solve that problem

推荐答案

这听起来像你有两个问题。首先,本地通知已经创建了过去设置的开火日期 - 这就是为什么它会在您打开应用程序时立即显示。

This sounds like you have two issues. First, the local notification has been created with a fire date set in the past - that's why its appearing as soon as you open the app.

其次,您可能正在设置通知的repeatInterval为非零值,这将使其多次出现。

Secondly, you may be setting the notification's repeatInterval to a non-zero value, which will cause it to come up more than once.

请参阅以下代码,以便在下午3点设置本地通知:

See the below code for setting a local notification to fire at 3pm:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = @"This is a test alert";
NSCalendar *currentCalendar = [NSCalendar currentCalendar];

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour: 15];
[comps setMinute: 0];
[comps setSecond: 0];
NSDate *threePM = [currentCalendar dateFromComponents:comps];

// Test if the current time is after three or not:
if(threePM != [threePM earlierDate: [NSDate date]])
{
  comps = [[NSDateComponents alloc] init];
  [comps setDay: 1];
  threePM = [currentCalendar dateByAddingComponents: comps toDate: threePM options: 0];
}

localNotification.fireDate = threePM;
localNotification.repeatInterval = 0;

[[UIApplication sharedApplication] scheduleLocalNotification: localNotification];

这篇关于当我打开通知托盘以查看通知时,UILocalNotification会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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