
Push notifications are a good way to interact with users, get them involved and return to applications (who hasn't woken up at night from a not received message at the wrong time?). However, there is one more interesting function in push, which not everyone knows about.
Imagine that your messenger, so loved by the user, goes into the background and “falls asleep” there. How can he take a call while not connected to the server? The answer lies precisely in the push - the message “wakes up” the application, it goes into active mode and can already take a call.
In iOS, there are several ways to awaken an application and give it control over what is happening.
There is
UILocalNotification - pending notifications tied to a specific point in the future. Since we do not know when the call will come, it is impossible to work with
UILocalNotification for organizing newsgroups.
')
There is a background
fetch in the background, described by
UIBackgroudnModes . It is also tied to time and can wake up an application to download and update data at the right time. Since we cannot specify the exact time of the call, the background wake-up cannot work with calls either.
You can wake the application by remote notifications. If you make your project, this option is enabled in the “Capabilities” tab of the project in Xcode, there is a section “Background Modes” and the option “Remote notifications” (you can also enable the
UIBackgroundModes key with the value “remote-notification” in
Info.plist ) .

In the notification itself, you need to make the content-available key with a value of 1:
{ "aps" : { "content-available" : 1 }, "content-id" : 42 }
only then will the application wake up (or start) and call the
application: didReceiveRemoteNotification: fetchCompletionHandler method . Just in it you can already implement the necessary functionality.
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"Remote Notification userInfo is %@", userInfo); NSNumber *contentID = userInfo[@"content-id"];
In this handler, you can download the necessary content, connect to the server and, for example, check for calls (for working with push, I recommend the article “Notifications in iOS 10” from e-Legion, which discusses the issues in more detail).
Apple for VoIP applications recommends waking up just through
UIBackgroundModes , but, of course, not by time, but by listening to incoming traffic. The messenger in the background monitors a specific socket and when traffic appears in it, it calls the handler that starts the necessary procedures.
Voximplant pushes for iOS are based on the VoIP version. To work you need:
1. Subscribe to notifications when the application starts:
PKPushRegistry *voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()]; voipRegistry.delegate = self; voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
2. Process incoming messages and transfer the incoming token to the SDK:
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(PKPushType)type { // 'self.sdk' is an instance of the 'VoxImplantSDK' [self.sdk registerPushNotificationsToken:credentials.token]; }
3. After receiving the push, if the application is not connected to the Voximplant cloud, you need to reconnect and receive an incoming call:
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type { NSDictionary* custom = [[payload.dictionaryPayload objectForKey:@"aps"] objectForKey:@"voximplant"]; if (custom) { // This call notifies Voximplant cloud that notification is successfully received. [self.sdk handlePushNotification:payload.dictionaryPayload]; } }
Pushi is a powerful tool for remote communication with the application, and not just a tool for showing the user information that outsiders have plundered his farm. This is the ability to receive information in the background, "enable" the application by call, update user data. Ultimately, all this is the means of convenient work and user involvement in the work of the application.
Illustration to Kata: www.davidrevoy.com