【问题标题】:How To Distinguish Between Local Notifications如何区分本地通知
【发布时间】:2013-06-22 00:31:40
【问题描述】:

我在一个长期存在的应用中实现了本地通知。该应用程序每天 24 小时以自助服务终端模式运行。一个本地通知每天触发一次,另一个每小时触发一次。每天触发一次的通知会删除前一天的所有本地核心数据信息。每小时触发一次的通知是应用的“心跳”,每小时在服务器上创建一次签入。

这是每小时心跳的时间表(在我的主视图控制器中):

- (void)scheduleHeartBeat
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];

UILocalNotification *heartbeat = [[UILocalNotification alloc] init];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];

components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];

[components setDay: day];
[components setMonth: month];
[components setYear: year];
[components setHour: 00];
[components setMinute: 10];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone systemTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];

heartbeat.fireDate = dateToFire;
heartbeat.timeZone = [NSTimeZone systemTimeZone];
heartbeat.repeatInterval = NSHourCalendarUnit;

[[UIApplication sharedApplication] scheduleLocalNotification:heartbeat];
}

我在viewDidLoad中调用了上面的方法。

然后在我的 AppDelegate 中我有以下内容:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{   
application.applicationIconBadgeNumber = 0;
[[OLEngine myEngine] deleteStuffFromYesterday:@"MyObject"];

}

在 didReceiveLocalNotification 中,我需要区分触发了哪个本地通知,因为我不想每小时调用一次方法 deleteStuffFromYesterday - 每天只调用一次。

如何在我的应用委托代码中区分这些计划的本地通知?

【问题讨论】:

    标签: ios notifications uilocalnotification


    【解决方案1】:

    您可以使用userInfo 属性为此存储NSDictionary

    存储信息:

    localNotification.userInfo = @{ @"myCustomType" : @"heartbeat" };
    

    检索信息:

    NSString *myCustomType = localNotification.userInfo[@"myCustomType"];
    

    【讨论】:

    • 你有通知的例子吗?
    • 刚刚添加了一些代码。这只是一本应该熟悉的字典。
    猜你喜欢
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多