IOS 8 Silent Push Notification does not fire didReceiveRemoteNotification method when app is not connected to xcode
I have seen too many questions about silent push notification not working if the device is not connected to xcode, but I could not find a real answer. I am using Silent APN to start a process in the background and then trigger a local Push notification
-
The server sends this information:
"_metadata" = { bagde = 1; pushText = "semeone has sent you a message!!"; sender = "semeone"; }; aps = { "content-available" = 1; };
And _metadata is the configured information to trigger local notification, I have not included the icon, pushText .. in the aps because I have this silent push notification.
-
The client should receive information in the didReceiveRemoteNotification file,
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if(application.applicationState != UIApplicationStateActive ){ if([userInfo[@"aps"][@"content-available"] intValue]== 1) //it 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 NOT the silent notification "); completionHandler(UIBackgroundFetchResultNoData); return; } } else { if(preLoadPNTask){ NSLog(@"End Background task "); [[UIApplication sharedApplication] endBackgroundTask:preLoadPNTask]; preLoadPNTask = 0; } completionHandler(UIBackgroundFetchResultNewData); } }
It works fine when the device connects to xcode, but when it doesn't, the didReceiveRemoteNotification function doesn't fire :(
Any ideas?
Thanks in advance!
source to share
What I ended up with is a USB cable caused me some problems, apparently every time I connected to an iphone device I said "this accessory may not be supported" but it continues to work fine so I replacing it with a new one, but that doesn't solve my problem, but it might be part of it. so I looked at the code and made some changes, after getting 2 o quieter push notifications preLoadPNTask (UIBackgroundTaskIdentifier) was creating many times, so I added confirmation before it starts,
if(!preLoadPNTask){
//start a background task
UIBackgroundTaskIdentifier preLoadPNTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"Background task to start a process ");
}];
}
I hope this helps you Relations
source to share
In ios 8, you need to follow these steps to run makeReceiveRemoteNotification: method
- Select the "Target Project Availability" tab
- Select "Background Modes".
- It will add the key (required backgrounds) to your info.plist project
After these modifications, when you receive the apple button press notification and if the app is already in the background, then the ReceiveRemoteNotification function will be activated.
source to share
Perhaps because in iOS 8 you have to ask for push notifications in a different way. Try the following:
-(void) registerForPushNotifications {
UIApplication* application=[UIApplication sharedApplication] ;
// Register for Push Notitications, if running iOS 8
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
// Register for Push Notifications before iOS 8
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
}
source to share