前期工作:
1、一部苹果设备,iphone或者itouch
2、苹果开发者
3、在https://www.jpush.cn/注册jpush
证书
1、在开发者中心申请AppID,下载生成的证书,双击安装后,在到钥匙串导出。
2、在设备上安装provisioning prodfile,选择刚才生成的AppID,选择设备后下载双击安装到手机,这样我们就能获取设备的devoiceToken了。
3、下载jpush的SDK并导入到工程。
4、创建PushConfig.plist文件,里面有三个key-value如下
{
"APS_FOR_PRODUCTION = "
0
";
"CHANNEL"
=
"Publish channel"
;
"APP_KEY"
=
"AppKey copied from JPush Portal application"
;//你在jpush上生成的appkey
}
|
代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
//简历连接成功通知
[defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kAPNetworkDidSetupNotification object:nil];
//关闭连接成功
[defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kAPNetworkDidCloseNotification object:nil];
//注册成功通知
[defaultCenter addObserver:self selector:@selector(networkDidRegister:) name:kAPNetworkDidRegisterNotification object:nil];
//登录成功通知
[defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kAPNetworkDidLoginNotification object:nil];
//接收到非APNS消息通知
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kAPNetworkDidReceiveMessageNotification object:nil];
[self.window makeKeyAndVisible];
//设置通知类型
[APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
//初始化APService
[APService setupWithOption:launchOptions];
return YES;
}
//获得设备的代理,将设备的token发送到jpush服务器
-
(
void
)
application:
(
UIApplication
*
)
application
didRegisterForRemoteNotificationsWithDeviceToken:
(
NSData
*
)
deviceToken
{
[
APService
registerDeviceToken
:
deviceToken
];
}
//将收到的apns推送进行处理
-
(
void
)
application
:
(
UIApplication
*
)
application
didReceiveRemoteNotification
:
(
NSDictionary
*
)
userInfo
{
[
APService
handleRemoteNotification
:
userInfo
];
}
通过上述配置你可以收到两次推送通知,一次apns的,另外一个不是,但两个消息内容是一致的,进行推送的时候需要到jpush上进行推送。另外在jpush上进行app注册的时候需要用到通过AppID生成的证书。