1

在我的项目中,我想通过推送通知显示事件和优惠,但问题是,我能够显示事件或优惠,而不是两者。有什么方法可以识别推送通知的消息。这是代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSString *message = nil;
    id alert = [userInfo objectForKey:@"alert"];

    if ([alert isKindOfClass:[NSString class]]) {
        message = alert;
    } else if ([alert isKindOfClass:[NSDictionary class]]) {
        message = [alert objectForKey:@"body"];
    }
    if (alert) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title"
                                                            message:@"AThe message." delegate:self
                                                  cancelButtonTitle:@"button 1"
                                                  otherButtonTitles:@"button", nil];
        [alertView show];
    }

    NSString *contentsInfo = [userInfo objectForKey:@"contTag"];
    NSLog(@"Received contents info : %@", contentsInfo);

    NSString *nibName = [AppDelegate fetchNibWithViewControllerName:@"EventsViewController"];

    EventsViewController *evc = [[EventsViewController alloc] initWithNibName:nibName bundle:nil];

    evc.newEvent = YES;

    [self.navigationController pushViewController:evc animated:YES];

    [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
}
4

1 回答 1

3

alert总是一个NSDictionary有两个键的:body 和 show-view。前者的值是警报消息,后者是Boolean(falsetrue)。如果false,则不显示警报的查看按钮。默认是显示 View 按钮,如果用户点击它,就会启动应用程序。

检查文档

要识别消息的类型,您可以提供其他字段,如此处所述

例子:

{
   "aps":{
      "badge":1,
      "alert":"This is my special message!",
      "mycustomvar1":"123456",
      "mycustomvar2":"some text",
      "myspecialtext":"This is the best!",
      "url":"http://www.mywebsite.com"
   }
}
于 2012-09-06T06:53:01.227 回答