我正在尝试安排附加了一些自定义数据的本地通知,然后当用户通过单击通知打开应用程序时,我会读取该数据.
我安排这样的本地通知:
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];
Run Code Online (Sandbox Code Playgroud)
然后在处理通知的代码中我这样做:
- (void)onAppDidBecomeActive:(NSNotification*)notification
{
NSLog(@"Object = %@", [notification object]);
NSDictionary* userInfo = [notification userInfo];
NSLog(@"UserInfo = %@", userInfo);
NSString* beaconUUID = [userInfo objectForKey:@"beaconUUID"];
NSLog(@"Beacon UUID = %@", beaconUUID);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我尝试读取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)
Run Code Online (Sandbox Code Playgroud)
这表明字典值是用户信息的一部分.任何人都有任何想法我做错了什么?
你安排了一个UILocalNotification但是你正在从a检索你的用户信息NSNotification,这两个是不一样的.
你应该做这个:
UILocalNotification *localNotif = [notification object];
NSDictionary *userInfo = [localNotif userInfo];
NSString* beaconUUID = [userInfo objectForKey:@"beaconUUID"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3721 次 |
| 最近记录: |