我有一个iOS应用程序,我在其中使用推送通知在发布正确答案时通知用户。如果应用程序已打开,如果我单击通知,它将转到指定的控制器。但是,如果应用程序已关闭,并且我收到通知,则它不会转到指定的控制器。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"User Info %@",userInfo);
NSString* alertValue = [[userInfo valueForKey:@"aps"] valueForKey:@"badge"];
NSLog(@"my message-- %@",alertValue);
int badgeValue= [alertValue intValue];
// NSNumber *identifierString = [[[userInfo valueForKey:@"aps"]valueForKey:@"details"]valueForKey:@"identifire"];
// NSLog(@"identifierString %@",identifierString);
NSString *alertMessage = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
NSLog(@"ALertMessage %@",alertMessage);
if ([alertMessage isEqualToString:@"New answer added."]) {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeValue];
// [[NSUserDefaults standardUserDefaults] setObject:[[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]valueForKey:@"question"]forKey:@"notificationQuestion"];
// [[NSUserDefaults standardUserDefaults] setObject:[[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]valueForKey:@"user_image"]forKey:@"notificationImage"];
// NSLog(@"Image %@",[[NSUserDefaults standardUserDefaults]valueForKey:@"notificationQuestion"]);
NSLog(@"User Information %@",userInfo);
NSLog(@"User Details %@",[[userInfo valueForKey:@"aps"] valueForKey:@"details"]);
// NSLog(@"User Details 1%@",[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]);
// NSLog(@"User Details 1%@",[[[[userInfo valueForKey:@"aps"] valueForKey:@"details"] objectAtIndex:0]valueForKey:@"question"]);
pushDictonary = [[userInfo valueForKey:@"aps"] valueForKey:@"details"];
}
//NSArray *pushDetails = [[userInfo valueForKey:@"aps"] valueForKey:@"details"];
// NSLog(@"Push Details %@",pushDetails);
if (application.applicationState == UIApplicationStateActive ) {
// UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// localNotification.userInfo = userInfo;
// localNotification.soundName = UILocalNotificationDefaultSoundName;
// localNotification.alertBody = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
// localNotification.fireDate = [NSDate date];
if ([alertMessage isEqualToString:@"New answer added."])
{
[self createNotificationViewwithUserDictionary:userInfo];
}
}
else if (application.applicationState == UIApplicationStateBackground)
{
NSLog(@"YES");
}
else if (application.applicationState == UIApplicationStateInactive)
{
NSLog(@"YES");
if ([alertMessage isEqualToString:@"New answer added."])
[self pushdetailsViewController];
}
转到控制器:
-(void)pushdetailsViewController
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
QPYourQuestionController *controller = (QPYourQuestionController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"yourQuestion"];
[[QPCommonClass initializeUserDefaults]setObject:[pushDictonary valueForKey:@"question_id"] forKey:@"currentquestionID"];
NSLog(@"Dictionary %@",pushDictonary);
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController pushViewController:controller animated:YES];
}
答案 0 :(得分:2)
对于关闭应用程序的情况,远程通知代理
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
未被称为
在这种情况下,调用将转到以下方法:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
因此,您可以执行此操作以将用户带到您想要的VC
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
// Do whatever you want
}
}
答案 1 :(得分:1)
将此代码添加到您的应用委托文件
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//you will get notification data in userInfo dict
UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
notificationViewController * notificationVC = [mainStoryboard instantiateViewControllerWithIdentifier: @"notificationViewController"];//set your controller here
[(UINavigationController *)self.window.rootViewController pushViewController:notificationVC animated:YES];
completionHandler(UIBackgroundFetchResultNewData);
}