【问题标题】:Execute custom method on UILocalNotification fire in iOS在 iOS 中的 UILocalNotification 上执行自定义方法
【发布时间】:2015-02-06 13:41:18
【问题描述】:

我正在我的应用程序中处理警报模块。我开发了代码,该代码从用户那里获取日期和时间输入,并在我的应用程序中的特定时间设置事件。 我还将信息存储到我的本地数据库中,以显示用户设置的警报列表。

现在我想在执行特定警报时从数据库中删除条目(当 UILocalNotification 显示到应用程序中时,我想调用数据库方法从数据库中删除该条目)

我是这样设置通知的

NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
int notificationCount = [preferences integerForKey:@"notification_count"];
notificationCount++;
NSDate* final_date = [calendar dateFromComponents:final_Components];
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = final_date;
localNotification.alertBody=titleTextField.text;localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 1;
NSDictionary* userInfoDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:notificationCount] forKey:@"notification_id"]; localNotification.userInfo = userInfoDic;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[preferences setInteger:notificationCount forKey:@"notification_count"];

我使用了委托 didReceiveLocalNotification

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSString *itemName = [notification.userInfo objectForKey:@"notification_id"];
NSLog(@"userInfo is %@",itemName);
databaseObject.deleteNotification(itemName)
}

我的问题是“didReceiveLocalNotification”仅在 2 种情况下调用

1) 用户使用应用时(前台应用)。

2) 当通知显示并且用户点击通知时

但是在第三种情况下,当应用程序处于后台模式并显示通知并且用户不单击通知或第二种情况是打开应用程序时直接单击应用程序图标或用户清除通知时,不会调用 didReceiveLocalNotification 委托。 .

有什么方法可以在所有情况下检测通知触发,或者通过使用任何其他方法来检测通知已触发,然后我将执行我的删除方法。

感谢任何帮助 谢谢你

【问题讨论】:

  • 抱歉,UILocalNotification 不是这样工作的。当应用程序在后台时,您不能使用它来执行代码,您必须找到另一种方法来实现这一点。后台执行在 iOS 上是一个棘手的问题,不幸的是它的使用仅限于某些场景
  • @nburk 是否可以在 applicationWillEnterForeground 委托中检查过去执行的 UILocalnotification?
  • 我遇到了一些我想做的计划。最好的解决方案似乎是做一个数据推送通知,它允许进行一些处理,但是很难找到一个以合理的价格提供预定推送的服务,并且您可以从应用程序中进行配置。

标签: ios objective-c iphone notifications


【解决方案1】:

是的,您是对的,如果在您的应用暂停/终止并且用户没有点击通知来启动/激活您的应用时触发您的通知,您将不知道通知的信息。

这样做的唯一方法是计算您的应用每次运行的时间并重新安排通知并更新您的数据库。

毫秒计算:

NSInteger myMillisecond;  //assume it exists
const NSTimeInterval oneSecondAsMilliseconds = 1000.0;
NSTimeInterval myTimeInterval = myMillisecond/oneSecondAsMilliseconds;
NSDate *currentDate = [NSDate date];
NSTimeInterval currentTimeStamp = [currentDate timeIntervalSince1970];
if (myTimeInterval > currentTimeStamp) {
    //myTimeInterval is a later time than now
}

【讨论】:

  • 我之前提出过这个想法,我已经尝试过这个..想法是,当我设置特定时间的警报时,然后将该日期时间转换为毫秒并将其存储到数据库中,并且每次获取警报列表从数据库..我需要将它与毫秒时间和当前时间进行比较...但我不知道如何将其转换为毫秒并与当前日期和时间进行比较?你有什么想法吗?
  • 毫秒时间是否意味着时间戳?
  • 是的,在 android 中我猜是毫秒
  • @Swap-IOS-Android 您可以将毫秒转换为 NSTimeInterval,并将其与 [NSDate timeIntervalSince1970] 进行比较。 NSTimeInterval 是一个double,点后面的数字可以用毫秒。如文档所述, NSTimeInterval 始终以秒为单位指定;它在 10,000 年的范围内产生亚毫秒精度。
  • 在您的答案中添加一些示例代码,我会接受它。谢谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
  • 1970-01-01
  • 2015-06-05
  • 2010-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多