【问题标题】:NSDictionary returning me empty string in push notificationNSDictionary 在推送通知中返回空字符串
【发布时间】:2016-07-19 21:39:37
【问题描述】:

这是我的有效载荷

{
    aps =     {
        ImageURL = "http://i.forbesimg.com/media/lists/people/cristiano-ronaldo_416x416.jpg";
        alert = testing;
    };
}

这是我的代码

NSDictionary *aps = [[NSDictionary alloc] initWithDictionary:[launchOptions objectForKey:@"aps"]];
NSString *text = [aps objectForKey:@"alert"];
NSString *imageUrl = [aps objectForKey:@"ImageURL"];

此代码在 didRecievePushNotification 中有效,但在 didFinishLaunching 中无效

【问题讨论】:

  • 你确定你的 launchOptions 字典在从这两种方法调用时是一样的吗?尝试记录 launchOptions 并比较输出。
  • 实际上是在我杀死应用程序并发送推送通知时的问题,当时没有调试器

标签: ios objective-c iphone push-notification


【解决方案1】:

该代码应该可以工作,尽管它过于复杂且效率低下。没有理由使用您的 launchOptions 字典的内容创建新字典。

简单地说

NSString *text = ((NSDictionary*)launchOptions[@"aps"])[@"alert"];
NSString *imageURL = ((NSDictionary*)launchOptions[@"aps"])[@"ImageURL"];

或者

NSDictionary *aps = (NSDictionary*)launchOptions[@"aps"];
NSString *text = aps[@"alert"];
NSString *imageURL = aps[@["ImageURL"];

【讨论】:

  • 请注意,在上面的第二个选项中,我没有分配/初始化新字典;我只是提取键“aps”的值并将其转换为正确的类型。
  • 所以记录launchOptionsfpstextimageURL
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 2014-09-30
相关资源
最近更新 更多