iOS本地通知userInfo始终为null

3

我正在尝试安排一个本地通知,并附加一些自定义数据,当用户通过点击通知打开应用程序时,我将读取这些数据。

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

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)

这表明字典值是用户信息的一部分。有人有什么想法,我做错了什么吗?


1
你从哪里拿到这个方法的?我认为它应该是:- (void)applicationDidBecomeActive:(UIApplication *)application。无论如何,你的应用程序应该调用application:DidReceiveLocalNotification:(UILocalNotification *)notification,并且你可以像从那个方法中一样访问它。 - mbm29414
我正在开发一个Phonegap插件,这是我的函数,它从appDelegate.m中调用。这样我就可以创建一个插件而不必编辑应用程序委托文件。 - Mathyn
2个回答

5
您安排了一个UILocalNotification,但是您正在从NSNotification中检索用户信息,这两个不同。您应该执行以下操作:
UILocalNotification *localNotif = [notification object];
NSDictionary *userInfo = [localNotif userInfo];
NSString* beaconUUID = [userInfo objectForKey:@"beaconUUID"];

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接