iOS messaging remote Push notifications
- 2020-12-13 19:08:09
- OfStack
Examples of this paper share iOS message push and iOS remote notification codes for your reference, the specific content is as follows
Being pushed
/*
To develop a program that tests the message mechanism, you must test it with a real machine
Types of push messages
UIRemoteNotificationTypeNone Do not receive push messages
UIRemoteNotificationTypeBadge Receive icon number
UIRemoteNotificationTypeSound Receiving audio
UIRemoteNotificationTypeAlert Receive message text
UIRemoteNotificationTypeNewsstandContentAvailability Receive subscription messages
You want to listen to the registered deviceToken It needs to be done at Apple's developer center 1 Some setup work will do.
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Set the application to receive APNS Push message
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - To obtain DeviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"%@", deviceToken);
// 1. Take the previous from the system preference token
NSData *oldToken = [[NSUserDefaults standardUserDefaults]objectForKey:@"deviceToken"];
// 2. The old and new token To compare the
if (![oldToken isEqualToData:deviceToken]) {
// 3. If you don't 1 , save token To system preference
[[NSUserDefaults standardUserDefaults]setObject:deviceToken forKey:@"deviceToken"];
// 4. use post Request transfer old and new token To the server
// 1) url
// specific URL Address and POST The parameters and format in the request are provided by the company's back-end programmers
// 2) request POST body (Both old and new token The data)
// 3) connection The asynchronous
}
}
Remote notifications
/**
Remote push message must run on real machine!
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// You need to tell Apple's servers that the current application needs to receive remote notifications
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
return YES;
}
#pragma mark - Get the token of the device (token)
// Received the device code returned by Apple
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// The first 1 Second run gets to DeviceToken It will take a long time!
NSLog(@"%@", deviceToken);
// will deviceToken Converts to a string for later use
NSString *token = [deviceToken description];
NSLog(@"description %@", token);
// =======================================================
// if DeviceToken The server needs to be notified of a change
// Each time a fetch from the server is recorded DeviceToken
// The comparison is made when the second fetch is obtained
// Retrieves the currently saved from preferences Token
NSString *oldToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"];
// when Token When changes occur, commit to the server to save the new one Token
// if (![oldToken isEqualToString:token]) {
//
// // will deviceToken through Post Request, submit to their own server can!
// // send Post request
// NSURL *url = [NSURL URLWithString:@" The url of the company's backend server "];
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.f];
//
// request.HTTPMethod = @"POST";
// request.HTTPBody = @" The converted device ID And other information [ Before the Token]";
//
// // SQL: update t_deviceTable set token = newToken where token = oldToken;
//
// // Synchronization: Execution must be completed before proceeding
// // Asynchronous: it is handed directly to other threads without interfering with the work of the main thread, and the user does not feel the delay
// [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// // Secretly transmit user information to the company's servers
// }];
// }
// will Token Save to system preferences
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
}