iOS本地通知userInfo始终为null

时间:2015-02-04 13:25:57

标签: ios objective-c

我正在尝试安排附加了一些自定义数据的本地通知,然后当用户通过点击通知打开应用时,我会阅读该通知。

我安排了这样的本地通知:

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)

这表明字典值是用户信息的一部分。有人知道我做错了吗?

2 个答案:

答案 0 :(得分:5)

您预定了UILocalNotification但是您正在从NSNotification检索您的用户信息,这两个不一样。

你应该这样做:

UILocalNotification *localNotif = [notification object];
NSDictionary *userInfo = [localNotif userInfo];
NSString* beaconUUID = [userInfo objectForKey:@"beaconUUID"];

答案 1 :(得分:1)

当我收到 didReceiveLocalNotification 协议

AppDelegate.m 文件

中的

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState state = [application applicationState];
    NSLog(@"Object = %@", notification);

    NSDictionary* userInfo = [notification userInfo];
    NSLog(@"UserInfo = %@", userInfo);

    NSString* beaconUUID = [[notification userInfo] objectForKey:@"beaconUUID"];
    NSLog(@"Beacon UUID = %@", beaconUUID);

}

见下面的输出控制台

2015-02-04 19:11:12.937 SampleNO[6676:907] Object = <UIConcreteLocalNotification: 0x769e780>{fire date = Wednesday, February 4, 2015, 7:11:09 PM India 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 19:11:14.621 SampleNO[6676:907] UserInfo = {
        beaconMajor = 10;
        beaconMinor = 20;
        beaconUUID = 123123123123;
    }
    2015-02-04 19:11:18.086 SampleNO[6676:907] Beacon UUID = 123123123123