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.
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.
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.
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.
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:
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With