Generally when we are using application in user interaction mode then push notification getting called (didReceiveRemoteNotification method) but when the user come out from application, on that time, application is running in background mode so we have to set this method in appdelegate file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive){ // when we tap on notification bar NSDictionary *dict=[userInfo valueForKeyPath:@"aps"]; if([dict[@"notification_type"] isEqualToString:@"1"]){ [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotificationforProduct" object:nil userInfo:userInfo]; } else if([dict[@"notification_type"] isEqualToString:@"2"]){ [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotificationforCategory" object:nil userInfo:userInfo]; } } else if(application.applicationState == UIApplicationStateBackground){ // when application is closed (background called ) } else if([UIApplication sharedApplication].applicationState ==UIApplicationStateActive){ // when application is running and notification come to application (foreground check) } completionHandler(UIBackgroundFetchResultNewData); } steps: 1: project -->Capablities--> Background Modes and select check boxes of "Background Fetch" & "Remote notifications", or go into .plist and select a row & give name "Background Modes" and it will create with an array, set "Item 0" with string "Remote notifications". 2:changes in server side that will be send $payload = '{ "aps" : { "content-available" : 1, // for background "alert" : "'.$message.'", // message to show "badge" : 5, "sound" : "default", // sound "product_name" : "Alice in Wonderland", "product_id" : "450", "notification_type" : "1", // optional } }'; |