简体   繁体   中英

Identifying the Push Notification Message

In my project I want to show the events and offers through push notification, but the problem is, I'm able to show the events or offers, not both. Is there any way to identify the message of Push notification. Here's the code:

- (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];
}

alert is always an NSDictionary with two keys: body and show-view. The value of the former is the alert message and the latter is a Boolean ( false or true ). If false , the alert's View button is not shown. The default is to show the View button which, if the user taps it, launches the application.

check the docs

To identify type of the message you can provide additional fields, as described here

Example:

{
   "aps":{
      "badge":1,
      "alert":"This is my special message!",
      "mycustomvar1":"123456",
      "mycustomvar2":"some text",
      "myspecialtext":"This is the best!",
      "url":"http://www.mywebsite.com"
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM