Data & networkingHistorical technical note

Remote notifications with JPush: a detailed integration guide

Prepares remote notifications through App ID configuration, push capabilities and certificate requests, then integrates the JPush SDK. Development and production settings, certificate export and client callbacks document an early push-notification setup workflow.

Original byline:WzxJiang

Originally credited to WzxJiang; edited and archived by Bison.

Source listed in the original

English translation of the Chinese original, translated on . The article body uses automated translation; app descriptions, historical reading notes and video subtitles receive separate editorial checks. Original publication dates, attribution, code and version notes are retained.

Editor: Bison
Contributor: WzxJiang

Yesterday, our company project needed remote notifications. Implementing everything ourselves would be troublesome, so we used JPush. Below, I explain how to create push certificates and use JPush for remote notifications.

1

First, let's download the push certificates. These initial steps are simple; skip ahead if you already know them. Jianshu does not support jumping within the page.

Most online push-certificate tutorials are several years old. Downloading a certificate is now straightforward.

First, open Apple Developer Center

  • Click Member Center.

1

  • After signing in, click Certificates,Identifiers & Profiles.

1

  • Click either one.

1

  • Create an App ID.

1

  • The App ID name can be anything, but pay close attention to Bundle ID: the app project receiving notifications must use exactly this Bundle ID.

1

  • Under App Services, select Push Notifications now or change it later, then continue.

1

  • Submit. I had already selected Push Notifications, so it is orange here; otherwise it should be gray.

1

  • Then click the App ID you just created and choose Edit.

1

1

  • Create development and production certificates.

1

Here is the distinction between environments: an app compiled and installed through Xcode uses development; an app downloaded through a QR code or the App Store uses production.

– Clicking Continue here asks you to upload a certificate.

1

Now let's create this certificate.

  • Open the Mac's built-in Keychain Access: Launchpad → Other → Keychain Access.

  • After opening it, use Certificate Assistant at the upper left and select Request a Certificate From a Certificate Authority.

1

  • Be sure to save it to disk.

1

  • Then save it anywhere you like.

  • Upload the certificate you saved earlier.

1

  • Download it and click Done to return to the initial screen. Select your App ID again and click Edit. The development and production push-certificate processes are the same; follow the steps to create and download both certificates.

1

  • Push Notifications should now be green. 1

  • Open Keychain Access; you will find these two additional certificates.

1

  • Right-click each one and export it.

1

  • Set a password

1

After export succeeds, the files appear. Give them suitable names, preferably in English.

That completes the push certificates. Next, let's use JPush for remote notifications.

Using JPush for remote notifications

  • Register for JPush on its official website.

  • Create an application.

  • Import both certificates in the application settings. Mine are already verified; otherwise, buttons for uploading the certificates appear. Bundle ID is filled automatically after import. Upload the certificates one at a time, because uploading together may cause verification to fail.

1

– Then download SDK

– Import the SDK's Lib folder into your project and remember to select Copy.

1

  • Import the following libraries in Build Phases.

1

  • Modify Capabilities

Enable remote notifications.

1

Enable Background Modes.

1

Change Bundle ID and select Team.

1

Next comes the code.

Import the header in Appdelegate.m: #import “JPUSHService.h”.

Add the following code in didFinishLaunchingWithOptions, replacing the relevant fields with your own information.

// Required
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    //可以添加自定义categories
    [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound |UIUserNotificationTypeAlert)categories:nil];
} else {
    //categories 必须为nil
    [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)categories:nil];
}

[JPUSHService setupWithOption:launchOptions appKey:你的appKey(字符串),在极光的应用详情中可以看到" channel:@""(这个不填就行) apsForProduction:这个值生产环境为YES,开发环境为NO(BOOL值)];


Create

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken


method to register the device; remove this.

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //这个方法是设置别名和tag 可省
    // [JPUSHService setTags:nil alias:@"WzxJiang" fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
    //        NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags , iAlias);
    //   }];
    // Required
    [JPUSHService registerDeviceToken:deviceToken];
}


Create

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler:


Handling notifications received while the App is in the background

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    //iOS 7及之后才能用,现在没人适配iOS6了吧...
    // IOS 7 Support Required 
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}


Clear the badge in applicationWillEnterForeground, when the App is about to enter the foreground.

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"进入前台");
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}


Create

didFailToRegisterForRemoteNotificationsWithError


method to handle errors receiving push notifications, which generally should not occur.

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}


  • The code is ready. Send a notification now; this must be tested on a physical device.

  • In JPush, click Send Notification and ensure the development environment is selected.

1

  • Then you should receive the notification.
    1

That concludes the tutorial. Leave a comment if you have questions or want to explore further!
My GItHub


Recommended reading: Bison's guide to iOS multithreading, part 3