集成方法
1、在极光的官方网站中注册应用,获取appKey,并且上传APNS的开发证书和生产证书
打开通知2、打开工程的Push Notification 和 Background Modes(勾选Remote notifications)
3、在application didFinishLaunchingWithOptions里面配置
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 可以添加自定义categories
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
NSSet<UNNotificationCategory *> *categories;
entity.categories = categories;
}
else {
NSSet<UIUserNotificationCategory *> *categories;
entity.categories = categories;
}
[JPUSHService registerForRemoteNotificationTypes:entity.types categories:entity.categories];
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if (resCode == 0) {//获取registerID成功
NSLog(@"%@",registrationID);
}else{
//失败
NSLog(@"%d",resCode);
}
}];
//apsForProduction:NO,开发环境 ; YES,生产环境
[JPUSHService setupWithOption:launchOptions
appKey:JPushAppKey
channel:nil
apsForProduction:NO];
4、推送注册
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
5、实现JPUSHRegisterDelegate方法
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(); // 系统要求执行这个方法
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required,For systems with less than or equal to iOS6
[JPUSHService handleRemoteNotification:userInfo];
}
- 注意:如果在确保证书,配置都正确的情况下,发现不走注册的方法,并且不走协议方法,并且,在工程里还用了环信的SDK,那么,原因一定出在环信重写了didRegisterForRemoteNotificationsWithDeviceToken 这个方法。简单的改正就是把JPush的注册防盗环信重写的这个地方来。
// 环信重写的方法 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
[[EaseMob sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
[JPUSHService registerDeviceToken:deviceToken];
}
网友评论