iOS Aurora Push Integration

Recently, the project integrates the push function, and I implement the overall integration steps and push function.

Function implementation: @In the login state, the message can be pushed successfully, and when the login state is logged out, the message cannot be pushed.

@ When the application is suspended in the background, it can be pushed and enter the corresponding interface

@ When the application is in the foreground, regardless of any interface, it can be pushed to and enter the corresponding interface--sound settings

@The application is killed, but you can still receive messages when you are logged in

You need to set the alias when you log in, and clear the alias when you log out

1. Create an application on the JPush management Portal and upload the APNs certificate or authenticate with the APNs Auth Key

2. Add the header file to AppDelegate.m

  1. #import "JPUSHService.h"// The header file required to introduce the JPush function
  2. #ifdef NSFoundationVersionNumber_iOS_9_x_Max// Header files required for iOS10 to register APNs
  3. #import <UserNotifications/UserNotifications.h>
  4. #endif
  5. //#import <AdSupport/AdSupport.h>// The header file that needs to be imported if you need to use the idfa function (optional)
  6. #define JPushAppKey @"9961aac66c9ee59685fb1d4e"//Pushed appkey
  7. #define JPushChannel  @"App Store"
  8. #ifdef DEBUG//0 (default) means that the development certificate is used, and 1 means that the application is published with the production certificate.
  9. #define isProduction     NO
  10. #else
  11. #define isProduction     YES
  12. #endif

3. Initialize APNs (JPush registration, initialization is implemented in this proxy method, push when the app is not open, will also use this proxy method). Register APNs successfully and report DeviceToken to Jiguang server. Set alias, submit to get the register_id to the backend

  1. // iOS 10 Support Push Play - Play Sound Settings
  2. - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  3.     // Required push message received
  4.     NSDictionary * userInfo = notification.request.content.userInfo;
  5.     if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
  6.         [JPUSHService handleRemoteNotification:userInfo];
  7.         // Add various requirements. . . . .
  8.         //Assemble and play sound effects
  9.         SystemSoundID soundID = 1000;
  10.         //NSString *path = [[NSBundle mainBundle] pathForResource:@"video_new" ofType:@"caf"];
  11.         NSURL *url = [[NSBundle mainBundle] URLForResource:@"video_new" withExtension:@"caf"];
  12.         if (url) {
  13.             OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)url,&soundID);
  14.             if (error != kAudioServicesNoError) {//When the sound is obtained, an error occurs
  15.                 soundID = 1000;
  16.             }
  17.         }
  18.         AudioServicesPlaySystemSound(soundID);
  19.         AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动
  20.     }
  21.     completionHandler(UNNotificationPresentationOptionAlert);
  22.     // Need to execute this method, choose whether to remind the user, there are three types of Badge, Sound, Alert can choose to set
  23. }
  • //Register APNs successfully and report DeviceToken
  • - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  •     // Required - Register DeviceToken
  •     [JPUSHService registerDeviceToken:deviceToken];
  •     NSLog(@"√√√√√%@",[JPUSHService registrationID]);//The same device uninstalls and reinstalls the login is different
  •     //Obtain the registered register_id, this value is generally passed to the background for the push mark, and stored first
  •     _registerid = [JPUSHService registrationID];
  •     
  •     //Submit register_id to the background (this method is provided for the background)
  •     if (ApplicationDelegate.isLogin) {
  •         if ([_registerid length]) {
  •             [self giveRegisterId:[JPUSHService registrationID]];
  •         }
  •     }
  •     //Set the alias and submit the obtained register_id to the background
  •     // This is the method provided by Jiguang, USER_INFO.userID is the user's id, you can set it according to the account or other, as long as it is unique
  •     NSSet *set2 = [[NSSet alloc] initWithObjects:_registerid, nil];
  •     [JPUSHService setTags:set2 alias:_registerid fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
  •         NSLog(@"Set result:%i user alias:%@",iResCode,@"USER_INFO.userID");
  •     }];
  • }

4. In the launch login section, clear the alias, so that the push message cannot be obtained from the launch login status

  1. // delete the pushed alias
  2. [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
  3. NSLog(@"rescode: %ld, \ntags: %@, \nalias: %@\n", (long)iResCode, @"tag" , iAlias);
  4. } seq:0];

 

Extension: Directed push
in Jiguang push In Jiguang push, broadcast push is not used, so how to achieve directional push is a problem that will inevitably arise for developers and needs. There can be two unique values ​​in Jiguang push:
(1) Register Jpush The registrationID generated after success, this registrationID is unique to mark the device, you found that when you start multiple times and register Jpush, this value is unchanged; on the same device, change the user login, this value still does not change. Change; finally, you delete the application, and then start the registration Jpush when downloading, this value remains unchanged. This can be directed to push to a certain device. If you can upload this value to your own server and bind something to this value, can you do more things?
(2) alias: Anyone who knows Jiguang push knows that this is an alias setting. The official document states that this value is not unique, but it is recommended that developers use it as the only mark for users. I think this is the best value as the only value, and it's great when you want to target a user for push, or call him back to our application. You can set it to userId, at this time, you can know which user to send the push to.



Link: https://www.jianshu.com/p/6cca682a2892

Link: https://www.jianshu.com/p/6a6166aadfe7

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325336885&siteId=291194637