I have seen too many questions about the silent push notification does not work if the device is not connected to xcode, but I could not find the real answer. I'm using Silent APN to start a process in background and then fire a local Push notification
Server sends this info:
"_metadata" = { bagde = 1; pushText = "semeone has sent you a message!!"; sender = "semeone"; }; aps = { "content-available" = 1; };
And _metadata is customized info to fire the local notification, I did not included badge, pushText.. in aps because I it is a silent push notification.
Client should get the info in didReceiveRemoteNotification,
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if(application.applicationState != UIApplicationStateActive ){ if([userInfo[@"aps"][@"content-available"] intValue]== 1) //it's the silent notification { //start a background task UIBackgroundTaskIdentifier preLoadPNTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"Background task to start a process "); }]; //end completionHandler regarding to fetchCompletionHandler completionHandler(UIBackgroundFetchResultNewData); // doing my process...... and fire a local notification if(preLoadPNTask){ NSLog(@"End Background task "); [[UIApplication sharedApplication] endBackgroundTask:preLoadPNTask]; preLoadPNTask = 0; } return; } else { NSLog(@"didReceiveRemoteNotification it's NOT the silent notification "); completionHandler(UIBackgroundFetchResultNoData); return; } } else { if(preLoadPNTask){ NSLog(@"End Background task "); [[UIApplication sharedApplication] endBackgroundTask:preLoadPNTask]; preLoadPNTask = 0; } completionHandler(UIBackgroundFetchResultNewData); } }
It works perfectly fine when the device is connecting to xcode, but when it doesn't, the didReceiveRemoteNotification doesn't start :(
Any ideas?
Thank you in advance!!