我正在尝试安排一个本地通知,并附加一些自定义数据,当用户通过点击通知打开应用程序时,我将读取这些数据。
我是这样安排本地通知的:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:10];
notification.alertBody = @"Hello World!";
notification.userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:
@"123123123123", @"beaconUUID",
[NSNumber numberWithInt:10], @"beaconMajor",
[NSNumber numberWithInt:20], @"beaconMinor",
nil];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
然后在处理通知的代码中,我这样做:
- (void)onAppDidBecomeActive:(NSNotification*)notification
{
NSLog(@"Object = %@", [notification object]);
NSDictionary* userInfo = [notification userInfo];
NSLog(@"UserInfo = %@", userInfo);
NSString* beaconUUID = [userInfo objectForKey:@"beaconUUID"];
NSLog(@"Beacon UUID = %@", beaconUUID);
}
我的问题是,当我尝试读取userInfo时,它总是返回null。如何从NSNotification对象中读取userInfo字典?
上面代码示例中的三个NSLog调用会在控制台中打印以下内容:
2015-02-04 14:11:52.690 BeaconPlugin[17050:724150] Object = <UIConcreteLocalNotification: 0x7ff1d37199a0>{
fire date = Wednesday, February 4, 2015 at 2:11:51 PM Central European Standard Time,
time zone = (null),
repeat interval = 0,
repeat count = UILocalNotificationInfiniteRepeatCount,
next fire date = (null),
user info = {
beaconMajor = 10;
beaconMinor = 20;
beaconUUID = 123123123123;
}}
2015-02-04 14:11:52.691 BeaconPlugin[17050:724150] UserInfo = (null)
2015-02-04 14:11:52.691 BeaconPlugin[17050:724150] Beacon UUID = (null)
这表明字典值是用户信息的一部分。有人有什么想法,我做错了什么吗?
- (void)applicationDidBecomeActive:(UIApplication *)application
。无论如何,你的应用程序应该调用application:DidReceiveLocalNotification:(UILocalNotification *)notification
,并且你可以像从那个方法中一样访问它。 - mbm29414