Reputation: 2273
Is it possible to in iOS to get a push from the server and then to run a method without displaying a notification at all? For example, to check if now is the configured time and if it is to dispaly a notification and if not to send the server a non availble message.
Upvotes: 0
Views: 162
Reputation: 4965
Yes, it is possible:
Implement didReceiveRemoteNotification:fetchCompletionHandler
:
Make sure to register for remote notifications, see documentation here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
Also make sure to edit Info.plist
and check the "Enable Background Modes" and "Remote notifications" check boxes:
Additionally, you need to add "content-available":1
to your push notification payload, otherwise the app won't be woken if it's in the background (see documentation here):
For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app
So payload should at least look like this:
{
"aps" = {
"content-available" : 1,
"sound" : ""
}
}
Just leave the sound property empty and omit the alert/text property and your notification will be silent.
Unfortunately, the app won't be woken up, if it's not running at all (force-quit), see this answer.
Upvotes: 0
Reputation: 57184
Yes, what you are looking for are silent push notification
s which got introduced in iOS 7. Take a look at the docs regarding push notifications which state:
The aps dictionary can also contain the
content-available
property. Thecontent-available
property with a value of1
lets the remote notification act as a “silent” notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.
and
content-available
-number
- Provide this key with a value of 1 to indicate that new content is available. Including this key and value means that when your app is launched in the background or resumed,application:didReceiveRemoteNotification:fetchCompletionHandler:
is called. (Newsstand apps are guaranteed to be able to receive at least one push with this key per 24-hour window.)
You can then react accordingly and eventually actually trigger another notification if you feel like the user needs to be informed about something (not 100% sure about that one though).
A little more in-depth information and code can be found in this objc.io post.
Upvotes: 1