SoFunction
Updated on 2025-04-09

iOS message remote push notification

This article shares iOS message push and iOS remote notification codes for your reference. The specific content is as follows

Message push

/*
  To develop a program that tests the message mechanism, you must use a real machine to test it
 
  Types of push messages
  UIRemoteNotificationTypeNone does not receive push messages
  UIRemoteNotificationTypeBadge Receives icon number
  UIRemoteNotificationTypeSound Receives audio
  UIRemoteNotificationTypeAlert Receive message text
  UIRemoteNotificationTypeNewsstandContentAvailability Receive subscription messages
 
  To listen to the registered deviceToken, you need to do some setup work in Apple's developer center.
  */

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Set the application to receive messages pushed by APNS  [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
  
   = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   = [UIColor whiteColor];
  [ makeKeyAndVisible];
  
  return YES;
}

#pragma mark - Get DeviceToken- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  NSLog(@"%@", deviceToken);
  // 1. Get the previous token from system preference  NSData *oldToken = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"];
  // 2. Comparison between old and new tokens  if (![oldToken isEqualToData:deviceToken]) {
    // 3. If inconsistent, save token to system preference    [[NSUserDefaults standardUserDefaults]setObject:deviceToken forKey:@"deviceToken"];
    
    // 4. Use post request to transfer new and old tokens to the server    // 1) url
    // The specific URL address and parameters and formats in the POST request are provided by the company's backend programmer    // 2) request POST body (including data of old and new tokens)    // 3) Asynchronous connection  }
}

Remote notification

/**
  Remote message push must run on the real machine!
  */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // You need to tell Apple's server that the current application needs to receive remote notifications  [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
  
  return YES;
}

#pragma mark - Get the device's code (token)// Receive the device code returned by Apple- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  // It will take a longer time to get the DeviceToken in the first run!  NSLog(@"%@", deviceToken);
  
  // Convert deviceToken into a string for subsequent use  NSString *token = [deviceToken description];
  NSLog(@"description %@", token);
  
  // =======================================================
  // If DeviceToken changes, the server needs to be notified  // Every time I record the deviceToken obtained from the server  // Compare when getting it again  // Remove the currently saved Token from the preferences  NSString *oldToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"];
  
  // When the token changes, submit it to the server to save the new token//  if (![oldToken isEqualToString:token]) {
//    
// // Submit deviceToken to your own server via Post!// // Send Post request// NSURL *url = [NSURL URLWithString:@"Company Backend Server URL"];//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:];
//    
//     = @"POST";
// = @"Converted device ID and other information [previous Token]";//    
//    // SQL: update t_deviceTable set token = newToken where token = oldToken;
//    
// // Synchronization: Must be completed before continuing// // Asynchronous: directly handed over to other threads to work, without interfering with the main thread's work, and the user does not feel the delay//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// // Secretly transfer user information to the company's server//    }];
//  }
  
  // Save the token to system preferences  [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.