Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silent Local Notification IOS

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.

like image 310
Helen Araya Avatar asked Apr 02 '15 17:04

Helen Araya


People also ask

What is local notification in iOS?

Local notifications reach users whether your app is running in the foreground or the background and require no external infrastructure to send, which is why they are aptly termed “local.” These notifications simply require that a user's device is on in order to be received.

What is silent notification iOS?

Silent notifications are notifications that do not make any sound or alert the user when it arrives, but it gets displayed only in the notification center/notification tray.

Will iOS awake my app when I receive silent push notification?

Silent remote notifications can wake your app from a “Suspended” or “Not Running” state to update content or run certain tasks without notifying your users.


1 Answers

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!

like image 131
Rhythmic Fistman Avatar answered Nov 10 '22 04:11

Rhythmic Fistman