A. ANPS principle

  • 1.App registers the message push popbox in the code;
  • 2.iOSANPSServer AcquisitionDevice Token, back to theAPP;
  • 3.APPDevice TokenSent to theOwn Company server;
  • 4.APPSend a message toOwn Company serverLater,Own Company serverThe message to be sent is packaged with the destination iPhone logo and sent toANPS server;
  • 5.ANPS serverIn its own list of devices registered with the Push service, it looks for the device with the corresponding identity and sends the message toEquipment of the APPAnd the Push notification pops up as set.

Ii.APNS Schematic Diagram

Iii. Project integration APNS push process

  • 1. Create a push certificate

    • Create a CRS file from a keystring as shown in the following figure:

  • 2. Create a push certificate

    • Log in to the developer website, log in to the account, create a Development+Distribute certificate, download the certificate, export the P12 certificate from the keystring, and send it to the company server

  • 3. Create code

    • Register APNS push requests

    In – (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (launchOptions NSDictionary *) Add the following code to the method:

//APNS push if (kDeviceSystemVersion >= 10.0) {UNUserNotificationCenter * Center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; UNAuthorizationOptions type = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {if (granted) {dispatch_async(dispatch_get_main_queue()); ^ {[[UIApplication sharedApplication] registerForRemoteNotifications];});} else {NSLog (@ "registered APNS failure");}}]; } else if(kDeviceSystemVersion >= 8.0){UIUserNotificationType type = UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } else { UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:type]; }Copy the code
  • The request pop-up box for the first installation of the program is shown below:

    • Click Permit to receive the Device Token

    After registering the device token successfully, call the following code:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {// after iOS13 NSMutableString *tokenString = [NSMutableString string]; const char *bytes = deviceToken.bytes; int iCount = deviceToken.length; for (int i = 0; i < iCount; i++) { [tokenString appendFormat:@"%02x", bytes[i]&0x000000FF]; } NSLog(@"tokenString = %@", tokenString); [[NSUserDefaults standardUserDefaults] setObject:tokenString forKey:@"DeviceTokenString"]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"autoRemotePush"]; if (! [[NSUserDefaults standardUserDefaults] boolForKey:@"pushToken"] && [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceTokenString"]) { [self pushToken:tokenString]; }}Copy the code
  • Failed to register the device token. Call the following code
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
Copy the code
  • Push DeviceToken to the server of your company and write your own push code
  • 4. Receive the push message
    • IOS10 above notice UNUserNotificationDelegate receive notifications

    The program falls back into the background to receive a push

// After ios10, the application receives push messages in the background - (void)userNotificationCenter (UNUserNotificationCenter *) Center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler  { NSLog(@"%s", __func__); NSDictionary * userInfo = response.notification.request.content.userInfo; NSDictionary *apsDic = [userInfo objectForKey:@"aps"]; UIApplication *application = [UIApplication sharedApplication]; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [self receivedNotification:userInfo application:application]; } else {// Warning: UNUserNotificationCenter Delegate received Call to completionHandler(); // The system requires this method to be executed}Copy the code
  • The iOS10 app receives a push message in the foreground
// whether the ios10 app receives push messages in foreground - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions The options)) completionHandler API_AVAILABLE (ios (10.0)) {NSLog (@ "% s", __func__); NSDictionary * userInfo = notification.request.content.userInfo; NSDictionary *apsDic = [userInfo objectForKey:@"aps"]; UIApplication *application = [UIApplication sharedApplication]; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { [self receivedNotification:userInfo  application:application]; } else {// determine local notification}}Copy the code
  • The following systems in iOS10 receive a push message
/ / ios - push ios10 received messages - (void) application: (UIApplication *) application didReceiveRemoteNotification: (NSDictionary *)userInfo { [self receivedNotification:userInfo application:application]; } / / ios7 click push messages into the - (void) application: (UIApplication *) application didReceiveRemoteNotification: (the userInfo NSDictionary *)  fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { [self receivedNotification:userInfo application:application]; completionHandler(UIBackgroundFetchResultNewData); }Copy the code
  • 5. When the application is killed, click the Push message in the notification bar
If ([[launchOptions allKeys] containsObject: UIApplicationLaunchOptionsRemoteNotificationKey]) {/ / users click on the notification bar push the setup NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; // NSDictionary *apsDic = [remoteNotification objectForKey:@"aps"]; // NSString *message = [apsDic objectForKey:@"alert"]; // NSString *badge = [apsDic objectForKey:@"badge"]; // NSString *sound = [apsDic objectForKey:@"sound"]; / / sound/self receivedNotification: remoteNotification application: application]; NSUserDefaults *notificationDic = [NSUserDefaults standardUserDefaults]; [notificationDic setObject:remoteNotification forKey:@"notification"]; }else{NSLog(@" user is a normally started program "); }Copy the code
  • 6. Handle push messages, code written by myself

Four. Pay attention to instructions

  • Application icon applicationIconBadgeNumber change is determined by the badge of push message, receive message sound determined by the sound of push message

Record these, thanks for reading, if there are any mistakes, feel free to comment!