didReceiveRemoteNotification未在后台模式中调用

时间:2015-08-12 13:45:59

标签: ios objective-c iphone swift apple-push-notifications

我正在处理推送通知,我收到的数据是JSON格式。如何解析JSON数据,如下面的通知中心所示:

I only need the description

2 个答案:

答案 0 :(得分:7)

如果您的应用程序处于后台/前台模式,则调用此方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

如果您使用上述方法,您将在控制台

中遇到以下错误
  

application:didReceiveRemoteNotification:fetchCompletionHandler:],但您仍需要将“remote-notification”添加到Info.plist中支持的UIBackgroundModes列表中。

解决此问题

按照步骤的图像

enter image description here

如果你的应用程序处于前台模式,则调用此方法

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo

选择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
}
}

<强>夫特

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
}
}

答案 1 :(得分:1)

如果在后台模式下未调用didReceiveRemoteNotification方法,请按照以下步骤操作

首先打开推送通知并勾选目标功能的背景模式远程通知复选框

然后

-(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;
}