# 史上最全远程推送详解之极光推送

语言：zh-CN
原文：[史上最全远程推送详解之极光推送](<https://allluckly.cn/%E6%8A%95%E7%A8%BF/tuogao28>)
中文：[中文 HTML](<https://allluckly.cn/%E6%8A%95%E7%A8%BF/tuogao28>)
English: [English HTML](<https://allluckly.cn/en/journal/2016-03-03-remote-notifications-with-jpush-a-detailed-integration-guide/>)
发布于: 2016-03-03T00:00:00+08:00
内容更新于: 2016-03-03T00:00:00+08:00
署名：WzxJiang

按 App ID、推送能力与证书申请顺序准备远程通知，再接入极光推送 SDK，说明开发和生产配置、证书导出及客户端回调代码，记录早期推送接入流程。

原文署名WzxJiang，Bison 编辑收录。

[原文所列来源](<http://www.jianshu.com/p/469bf2a7b16f>)

**阅读前** 这是 2015–2017 年间的技术记录，本次整理未重新运行验证示例。原文发表于 2016 年，使用当时的开发者中心界面、证书流程与极光 SDK。

编辑：[Bison](<https://allluckly.cn/>)  
 投稿：[WzxJiang](<http://www.jianshu.com/p/469bf2a7b16f>)

昨天公司项目要加入远程推送功能，自己做显然会很麻烦，所以用了极光的远程推送，下面我会讲如何制作推送证书并使用极光推送进行远程推送.

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/1.jpg>)

> 先讲讲怎么下载推送证书吧(前面的很简单要是知道的可以直接往下滑，简书竟然不能页内跳转…)

网上下载推送证书的教程一般都是几年前的，现在下载推送证书很简单

首先进入[苹果开发者中心](<https://developer.apple.com>)

- 点击Member Center

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/2.png>)

- 登录后点击Certificates,Identifiers & Profiles

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/3.png>)

- 任意点击一个

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/4.png>)

- 创建App ID

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/5.png>)

- App ID的名字可以随便取，Bundle ID一定要注意，要推送的App项目中的Bundle ID一定要是这个Bundle ID

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/6.png>)

- App Services可以现在勾上Push Notifications，也可以后面再改，然后点继续

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/7.png>)

- 提交(我之前勾选了Push Notifications，所有它现在是橙色的，不然应该是灰色)

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/8.png>)

- 出来后点击你刚创建的App ID，然后点Edit

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/9.png>)

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/10.png>)

- 创建开发环境和生产环境的证书

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/11.png>)

这里讲一下什么是开发环境和生产环境:你用Xcode编译安装的就是开发环境，用二维码或者App Store下载的就是生产环境.

－ 这里点继续后会发现需要你上传一个证书

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/12.png>)

## 现在我们来制作这个证书

- 打开MAC自带的钥匙串访问（Launchpad-\>其他-\>钥匙串访问）
- 点开后左上角打开证书助手,从证书颁发机构请求证书

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/13.png>)

- 注意一定要存储到磁盘

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/14.png>)

- 然后存储就行了，存在哪都可以
- 把刚才存储的证书上传了

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/15.png>)

- 下载下来，点击Done，又回跳回到开始的界面，然后还是选择你创建的App ID然后Edit，开发环境和生产环境推送证书的创建流程是一样的，自己按着步骤就能把证书全部创建并下载成功.

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/16.png>)

- 这时候Push Notifications应该是绿的了 ![1](<https://allluckly.cn/images/blog/tuogao/tougao28/17.png>)
- 打开钥匙串访问，你会发现你多了这两个证书

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/18.png>)

- 右键分别导出它们

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/19.png>)

- 设置密码

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/20.png>)

导出成功后，你就能看到它们了，记得命好名（最好用英文）

## 推送证书的制作到这就结束了，下面讲怎么使用极光推送进行远程推送

> 怎么使用极光推送进行远程推送

- 在极光官网注册极光推送
- 创建一个应用
- 在应用配置中导入两个证书（我这是已经验证了的，不然就是让你上传证书的按钮）Bundle ID是导入证书后自动出现的，证书最好一个一个上传不然可能会出现验证失败的问题.

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/21.png>)

－ 然后下载[SDK](<http://docs.jpush.io/resources/#ios-sdk>)

－ 把SDK中的Lib文件夹导入你的项目中，记得勾选Copy

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/22.png>)

- 在Build Phases中导入以下库

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/23.png>)

- 修改Capabilities

打开远程推送

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/24.png>)

打开Background Modes

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/25.png>)

修改Bundle ID 以及选择Team

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/26.png>)

## 下面便是代码部分

> 在Appdelegate.m中导入头文件#import “JPUSHService.h”

> 在didFinishLaunchingWithOptions方法中写入以下代码，有的地方要按你自己的信息填

```
// 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值)];
```

创建

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

方法中注册设备(去掉这个)

```
- (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];
}
```

创建

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

App在后台时收到推送时的处理

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

在applicationWillEnterForeground方法（App即将进入前台）中将小红点清除

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

创建

```
didFailToRegisterForRemoteNotificationsWithError
```

方法，处理接收推送错误的情况(一般不会…)

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

- 代码都已经弄好了，现在推送以下，(注意只能是真机调试)
- 在极光上点击发送通知,注意是开发环境

![1](<https://allluckly.cn/images/blog/tuogao/tougao28/27.png>)

- 然后你就能收到推送了  
   ![1](<https://allluckly.cn/images/blog/tuogao/tougao28/1.jpg>)

好了，教程到这就结束了，要是有疑问或者想了解更深入点的知识，欢迎评论!  
 [我的GItHub](<https://github.com/Wzxhaha>)

---

好文推荐：[Bison眼中的iOS开发多线程是这样的（三）](<https://allluckly.cn/%E5%A4%9A%E7%BA%BF%E7%A8%8B/duoxiancheng03>)

