检查iOS推送通知有效负载

时间:2014-11-04 20:11:58

标签: ios push-notification apple-push-notifications payload

我试图找出如何检查推送通知的有效负载,以便确定当用户从通知中打开应用时打开哪个视图。例如,如果通知说" x:test"当点击通知并且通知显示" y:test"视图你会打开。

编辑:我想我应该澄清一下我不确定的部分。

我在didFinishLaunchingWithOptions中有这个:

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if (notification)
    {
        // check payload then load appropriate view controller
    }

如何检查某些文本的有效负载以确定要加载的相应视图控制器?

2 个答案:

答案 0 :(得分:1)

在app delegate

中的两个地方处理接收推送通知
    当您的应用从推送通知启动时,
  • - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions。在这种情况下,您的推送通知数据包含在[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]

  • 中 当您的应用在接收通知时正在运行时,
  • - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo。在这种情况下,userInfo是您的推送通知数据。

答案 1 :(得分:1)

这是我过去项目的代码。通知显示在这样的设备中" Kostas:想要将您添加为朋友"。

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

NSDictionary *remoteNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (remoteNotif) {

    NSString *name = [[NSString alloc] init];
    NSString *message = [[NSString alloc] init];
    // 'wants to add you as friend.'
    NSString* alertValue = [[remoteNotif valueForKey:@"aps"] valueForKey:@"alert"];

    NSMutableArray* parts = [NSMutableArray arrayWithArray:[alertValue componentsSeparatedByString:@": "]];
    name = [parts objectAtIndex:0];
    [parts removeObjectAtIndex:0];
    message = [parts componentsJoinedByString:@": "];

    if ([message isEqualToString:@": wants to add you as friend."]) {
        UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
        // tabController.delegate = self;
        tabController.selectedIndex = 1;
    }
    else{
        UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
        // tabController.delegate = self;
        tabController.selectedIndex = 2;

        [self addMessageFromRemoteNotification:remoteNotif updateUI:NO];
    }
相关问题