Explanation of APNS push processing functions in iOS
Compared with Android, iOS is undoubtedly a better practice in push. APNS (Apple Push Notification Service) is a message Push Service provided by Apple. The principle is that the information to be pushed to the user by a third-party application is pushed to the Apple Server, which then pushes the information to the user's mobile phone through a unified system interface. If you are not familiar with this, refer to this article: step by step to teach you how to push ios
This article focuses on how to process push information on the App. It mainly involves several important functions, all of which are in the AppDelegate class:
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
Anyone who has developed iOS will be familiar with this function. This function is called at the end of the program and is about to run. Usually some initialization work can be processed in this function. Similarly, the initialization operation of the push must be completed in this part. This part of work is mainly divided into two parts: registration of push type:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationAlert];
This line of code tells the system that the push message type registered by this program usually includes badge, sound, and alert notifications. The push message when the processing program is not started: if the program is running or the program is in the background, the work of processing the PUSH message at this time is:
-(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo or:
-(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo fetchCompletionHandler :( void (^) (UIBackgroundFetchResult) completionHandler
. However, if the program has not been started when the user clicks push notification, the above two functions will not receive the user's push notification. At this time, the application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) The launchOptions function is used for processing. The information about push messages is stored in the launchOptions dictionary. See the following code for details:
NSDictionary* pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"]; if (pushInfo) { NSDictionary *apsInfo = [pushInfo objectForKey:@"aps"]; if(apsInfo) { //your code here } }
-(Void) application :( UIApplication *) applicationdidregisterforremotenotifswswithdevicetoken :( NSData *) pToken &-(void) application :( UIApplication *) handle :( NSError *) in order for the device to receive the PUSH message, the device token must be sent to the Apple Server. This token is equivalent to the device identifier, and each Apple device has a unique token, the apple server uses this token to find the corresponding device and send the corresponding message. These two functions are called after the token is successfully transferred or fails. You can perform some corresponding processing in the corresponding functions.
-(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo
-(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo and
-(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo fetchCompletionHandler :( void (^) (UIBackgroundFetchResult) completionHandler
It is the processing function that receives the PUSH message when the program is running (whether in the foreground or in the background. We recommend that you use
-(Void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo fetchCompletionHandler :( void (^) (UIBackgroundFetchResult) completionHandler
Because the former cannot receive the push information when the program is in the background (tested-(void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo can actually receive the push information, I don't know what is going on. I hope you can solve the problem ). The other is-(void) application :( UIApplication *) application didReceiveRemoteNotification :( NSDictionary *) userInfo fetchCompletionHandler :( void (^) (UIBackgroundFetchResult) completionHandler. According to the documents provided by Apple, the system provides 30 seconds to process the pushed messages, and then runs the CompletionHandler program block.
This problem is often encountered when processing such push messages (that is, the push messages are received after the program is started, is the current push message received when the current program is running in the foreground or is the program running in the background, the user clicks the corresponding item in the system Message notification bar to enter the program to receive it sometimes? This is actually very simple. You can solve it with the following code:
Void application :( UIApplication *) application didReceiveRemoteNotification: NSDictionary) userInfo fetchCompletionHandler :( (^) UIBackgroundFetchResult) completionHandler {if (application. applicationState = UIApplicationStateActive) {NSLog (@ "active"); // The program is currently in the foreground} else if (application. applicationState = UIApplicationStateInactive) {NSLog (@ "inactive"); // The program is in the background }}
For the userInfo structure, refer to the official structure of Apple:
{ |
"aps" : { |
"alert" : "You got your emails.", |
"badge" : 9, |
"sound" : "bingbong.aiff" |
}, |
"acme1" : "bar", |
"acme2" : 42 |
} |
That is, the key aps corresponds to a dictionary containing the specific information of the push message. It depends on the push type we registered. The remaining keys are user-defined.