11
votes

Can we have a silent local notification in IOS app. Which does some data processing in the background with out the user interacting with it.

What I want to do is create a silent local notification which fires every 8 am in morning and after the user receives it I want to do some data processing and recreate a new one which the user can see with the new data I processed after I saw the first silent local notification.

I am trying to avoid using push notification as much as I can.

1
When a local notification is fired, you won't get any control of the app. It is just something that is visually shown to the user. - Shamas S
Adding to above, only remote notifications (push) will allow you to do what you want to do. - Ollie
Local and push notifications both work the same way in that sense. they don't do anything unless you open them. - Kex
I am looking for the it too. So it is not possible? - ricardo
No it is not possible with local notifications. You have to use remote notification for such cases. - Helen Araya

1 Answers

3
votes

You can receive silent notifications in the background on iOS, but you will need a server to actually send the notifications.

To do this you enable the Remote notifications background mode in your target's Capabilities tab:

Screenshot of Background Modes settings

Then you register for push notifications in application:didFinishLaunchingWithOptions: with

UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeNone categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

Pending the user allowing your app to send push notifications, you will receive the device token:

 -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;

If something goes wrong, the failure handler will be called:

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:

You send the deviceToken to your server and tell it to send a silent push notification to that deviceToken at the device's local time of 8AM.

That device will have the following app delegate method called:

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

and you will be able to do your data processing.

Easy!

Don't forget to call the completion handler when you're done!