I am working on push notifications and the data I receive is in JSON format. How can I parse the JSON data, which is shown in the Notification Center below:
Asked
Active
Viewed 7,587 times
4

Anbu.Karthik
- 82,064
- 23
- 174
- 143

krishna
- 114
- 2
- 8
-
To parse JSON, check out [NSJSONSerialization](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/index.html#//apple_ref/occ/cl/NSJSONSerialization). – justinpawela Aug 12 '15 at 15:39
-
@krishna - r u checked this – Anbu.Karthik Aug 13 '15 at 08:46
2 Answers
7
if your app in background/foreground mode call this method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
if you used the above method you will face the following error in console
application:didReceiveRemoteNotification:fetchCompletionHandler:], but you still need to add "remote-notification" to the list of your supported UIBackgroundModes in your Info.plist.
To Resolve this issue
follow the image of steps
if your app in foreground mode call this method
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
choice no-2
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
// user tapped notification while app was in background
if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
// go to screen relevant to Notification content
} else {
// App is in UIApplicationStateActive (running in foreground)
// perhaps show an UIAlertView
}
}
Swift
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
var state: UIApplicationState = application.applicationState()
// user tapped notification while app was in background
if state == .Inactive || state == .Background {
// go to screen relevant to Notification content
}
else {
// App is in UIApplicationStateActive (running in foreground)
// perhaps show an UIAlertView
}
}

Anbu.Karthik
- 82,064
- 23
- 174
- 143
-
check the updated answer brother, if u need assistance I hope with you, i am waiting for your reply , – Anbu.Karthik Aug 12 '15 at 15:22
-
Thanks @Anbu.Karthik by changing the format of payload...worked for me. – krishna Aug 14 '15 at 05:10
-
-
@Anbu.Karthik not working, please help to my question https://stackoverflow.com/q/63598194/8822337 – BIS Tech Aug 28 '20 at 07:19
1
If didReceiveRemoteNotification method is not called in background mode,please follow the below steps
First ON the Push Notification and Tick the check box of Remote Notifications of Background Mode in Capabilities of Target
Then
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void
(^)(UIBackgroundFetchResult))completionHandler
{
if( [UIApplication sharedApplication].applicationState == UIApplicationStateInactive )
{
NSLog( @"INACTIVE" );
}
else if( [UIApplication sharedApplication].applicationState == UIApplicationStateBackground )
{
NSLog( @"BACKGROUND" );
}
else
{
NSLog( @"FOREGROUND" );
}
return YES;
}

user3182143
- 9,459
- 3
- 32
- 39
-
the questioner asked the notification center is not called in background mode , please update your answer or else check my answer – Anbu.Karthik Aug 12 '15 at 15:48
-