11

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.

Helen Araya
  • 1,886
  • 3
  • 28
  • 54
  • 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 Apr 02 '15 at 17:18
  • 1
    Adding to above, only remote notifications (push) will allow you to do what you want to do. – Ollie Apr 02 '15 at 18:47
  • Local and push notifications both work the same way in that sense. they don't do anything unless you open them. – Kex Apr 02 '15 at 19:15
  • 1
    I am looking for the it too. So it is not possible? – ricardo Apr 24 '15 at 20:36
  • No it is not possible with local notifications. You have to use remote notification for such cases. – Helen Araya Apr 28 '15 at 19:16
  • Try this answer. This might help. http://stackoverflow.com/questions/37161265/ios-silent-local-push-notifications-objective-c/37162574#37162574 – Ruturaj Desai May 11 '16 at 12:25
  • @RuturajDesai I already solved the issue using remote notification because I wanted my notification to happen even when the app is in background. – Helen Araya May 11 '16 at 14:57

1 Answers1

3

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!

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • When you say a server. It is a non IOS program which sends notification to a device right like scheduled task or some sort? Or can I do everything with in my IOS app, – Helen Araya Apr 02 '15 at 20:45
  • 1
    It is a non iOS program, e.g. running as a cron job on EC2 or something similar. – Rhythmic Fistman Apr 02 '15 at 20:46
  • 1
    So you are using remote notification. But I am trying to get away from remote notification and my question is about local notifications. – Helen Araya Apr 09 '15 at 21:26
  • 1
    Sorry, my mistake. Local notifications can't be silent. – Rhythmic Fistman Apr 09 '15 at 23:20
  • 1
    I really like this answer because, I was hoping I could do a local notification silently. However, you can't, so with this answer I was able to see exactly how I can get the silent notifications that I need. Thanks again! – Andrew Paul Simmons Jul 13 '15 at 16:52
  • @RhythmicFistman can you give me the source where it says silent local notification is not possible? – AndaluZ Jul 06 '17 at 08:13
  • I don't remember where I read that, but it was a long time ago and notifications changed a lot in iOS 10, you should have a look at the documentation. – Rhythmic Fistman Jul 06 '17 at 08:40
  • And how do we show a local notification when we've received silent push notifications with only json data payloads while the app is in the background? – Ryan Barrett Jan 19 '20 at 09:08