didReceiveRemoteNotification not called in Background Mode

◇◆丶佛笑我妖孽 提交于 2019-11-27 08:09:17

问题


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:


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/31967093/didreceiveremotenotification-not-called-in-background-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!