推送通知重定向到相应的viewcontroller

时间:2014-10-06 10:38:23

标签: ios objective-c iphone ios8

我在我的应用程序中使用推送通知。我的问题是,如果用户从应用程序收到推送通知,用户应该能够点击该通知横幅并且可以 带到显示通知的活动页面。

任何人都可以帮我解决这个问题。我是推送通知的新手。

3 个答案:

答案 0 :(得分:1)

//在AppDelegate.m中使用此代码

-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
 {
          //code for navigate to viewcontroller
          ActivityViewController *objActivity = [[ActivityViewController alloc]    initWithNibName:@"ActivityViewController" bundle:nil];
    [self.navigationcontroller pushViewController: objActivity animated:YES];

   }

答案 1 :(得分:1)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{




 NSString *aPayload=[userInfo objectForKey:@"payload"];

NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [aPayload  dataUsingEncoding:NSUTF8StringEncoding]
                                options: NSJSONReadingMutableContainers
                                  error: NULL];

if ([[JSON objectForKey:@"activity"]isEqualToString:@"encore"]) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Piggyback"
                                                    message: [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
                                                   delegate: nil
                                          cancelButtonTitle:@"DONE"
                                          otherButtonTitles: nil];
           self.likeBedge=self.likeBedge+1;
   [alert show];

   [[NSNotificationCenter defaultCenter] postNotificationName:@"likeComment" object:self userInfo:userInfo];
}


else if ([[JSON objectForKey:@"activity"]isEqualToString:@"like"]||[[JSON objectForKey:@"activity"]isEqualToString:@"comment"]){
    self.likeBedge=self.likeBedge+1;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"likeComment" object:self userInfo:userInfo];
}



else if ([[JSON objectForKey:@"activity"]isEqualToString:@"started playing"]){
    self.postBedge=self.postBedge+1;
    NSLog(@"%d",self.postBedge);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"started playing" object:self userInfo:userInfo];
}



else if ([[JSON objectForKey:@"activity"]isEqualToString:@"stopped playing"]){
     [[NSNotificationCenter defaultCenter] postNotificationName:@"stopped playing" object:self userInfo:userInfo];
}



else if ([[JSON objectForKey:@"activity"]isEqualToString:@"@user play"]||[[JSON objectForKey:@"activity"]isEqualToString:@"@user comment"]){
     [[NSNotificationCenter defaultCenter] postNotificationName:@"likeComment" object:self userInfo:userInfo];
}


return;

}

我已经完成了以上代码,但当应用程序处于后台时,如果通知到了用户点击横幅,它应该重定向到活动页面

答案 2 :(得分:0)

因此,当应用程序进入后台并且您收到通知时,您将在appdelegate.m文件中调用的方法是

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // Here present the class you want 
}