I have an iOS application in which I am using the push notification to notify the user when the correct answer is posted. If the application is open and if I click on the notification it goes to the specified controller. But if the application is closed and if I receive the notification it does not goes to the specified controller.
-(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];
}
For going to the controller :
-(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];
}