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:
我正在处理推送通知,我收到的数据是JSON格式的。如何解析JSON数据,该数据显示在下面的通知中心:
2 个解决方案
#1
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.
应用:didReceiveRemoteNotification:fetchCompletionHandler:],但是您仍然需要在Info.plist中向支持的uibackgroundmode列表中添加“远程通知”。
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
选择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
}
}
#2
1
If didReceiveRemoteNotification method is not called in background mode,please follow the below steps
如果在后台模式中没有调用didReceiveRemoteNotification方法,请遵循以下步骤
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;
}
#1
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.
应用:didReceiveRemoteNotification:fetchCompletionHandler:],但是您仍然需要在Info.plist中向支持的uibackgroundmode列表中添加“远程通知”。
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
选择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
}
}
#2
1
If didReceiveRemoteNotification method is not called in background mode,please follow the below steps
如果在后台模式中没有调用didReceiveRemoteNotification方法,请遵循以下步骤
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;
}