【问题标题】:iOS local notification userInfo always nulliOS 本地通知 userInfo 始终为空
【发布时间】:2015-02-04 13:25:57
【问题描述】:

我正在尝试安排带有一些自定义数据的本地通知,然后我会在用户通过单击通知打开应用程序时读取这些数据。

我这样安排本地通知:

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,您将能够像通过该方法一样访问它。
  • 我正在为 Phonegap 开发一个插件,这是我从 appDelegate.m 调用的函数。这样我就可以在不编辑应用委托文件的情况下创建插件。

标签: ios objective-c


【解决方案1】:

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

你应该这样做:

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

【讨论】:

    【解决方案2】:

    当我从 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
    

    【讨论】:

      猜你喜欢
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多