Let’s start with some tutorials that are popular online

  • www.jianshu.com/p/837b764f1…
    • Step by step to teach you to do ios push
  • Blog.csdn.net/showhilllee…
    • Step by step to teach you to do ios push
  • Blog.csdn.net/shenjie1234…
    • Implementation of App Message Push for IOS Development (latest)

After watching the tutorial feel push is actually very simple

  • In fact, it’s not too difficult to do it normally!
  • But I encountered two problems when doing push

Native Push (tutorials online)

  • registered

    if([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) { //IOS8 / / create UIUserNotificationSettings, And set the message of the show class type UIUserNotificationSettings * notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:notiSettings]; }else{ // ios7  
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge                                       |UIRemoteNotificationTypeSound                                      |UIRemoteNotificationTypeAlert)];  
    }  
Copy the code
  • PToken is used in PHP code
// 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken{  
    NSLog(@"---Token--%@", pToken); }}Copy the code
  • # Receive a push message
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{  
      
    NSLog(@"userInfo == %@",userInfo);  
    NSString *message = [[userInfo objectForKey:@"aps"]objectForKey:@"alert"];  
      
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tip" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sure", nil nil];  
      
    [alert show];  
}  
  

Copy the code
  • Failed to register
  

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{  
  
    NSLog(@"Regist fail%@",error); 
Copy the code
  • # After doing this, you will find that after executing the PHP code push, you will not receive the message

    • After repeated failed attempts, the following three steps were performed to successfully get the push message

  • Attached is a screenshot of the push message. I am very excited about the success of the first push

Then talk about the three-way push

  • # Attached is the official demo
- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  // Override point for customization after application launch.
  NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  
  if ([[UIDevice currentDevice].systemVersion floatThe Value] > = 10.0) {#ifdef NSFoundationVersionNumber_iOS_9_x_Max
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
  } else if ([[UIDevice currentDevice].systemVersion floatThe Value] > = 8.0) {/ / you can add custom categories [JPUSHService registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; }else{/ / categories must be nil [JPUSHService registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; } // If you don't need IDFA, AdvertisingIdentifier to nil [JPUSHService setupWithOption: launchOptions appKey: appKey channel: the channel apsForProduction:isProduction advertisingIdentifier:advertisingId]; // The registration ID Block interface was added in version 2.1.9. [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {if(resCode == 0){
      NSLog(@"RegistrationID successfully obtained: %@",registrationID);
      
    }
    else{
      NSLog(@"RegistrationID failed to obtain code: %d",resCode); }}]; [[NSBundle mainBundle] loadNibNamed:@"JpushTabBarViewController"
                                owner:self
                              options:nil];
  self.window.rootViewController = self.rootController;
  [self.window makeKeyAndVisible];
  rootViewController = (RootViewController *)
      [self.rootController.viewControllers objectAtIndex:0];

  return YES;
}

Copy the code
  • It has been proved that the registration of the aurora push to the official website will not be completed, so it will not receive the push message (just change the code below).

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; // If IDFA is not required, AdvertisingIdentifier to nil [JPUSHService setupWithOption: launchOptions appKey: appKey channel: the channel apsForProduction:isProduction advertisingIdentifier:nil];return YES;
}

    

Copy the code