iOS篇
- 导入框架
- Cocoapods 导入
pod 'JPush', '3.1.0'
- 手动导入(比较麻烦,不是很推荐)
- 在极光官网下载最新 SDK
- 将 SDK 包解压,在 Xcode 中选择 “Add files to 'Your project name'...”,将解压后的 lib 子文件夹(包含 JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a )添加到你的工程目录中。
3.添加 Framework- CFNetwork.framework
- CoreFoundation.framework
- CoreTelephony.framework
- SystemConfiguration.framework
- CoreGraphics.framework
- Foundation.framework
- UIKit.framework
- Security.framework
- libz.tbd
- UserNotifications.framework
- libresolv.tbd
注意: 如果集成 JPush 3.0.1 及以上版本, 且同时集成极光其他 SDK(如:JMessage 3.0.0 及以上版本)
1. Cocoapods 导入,建议都更新为线上最新版本,来避免 JCore 版本不一致导致的冲突。
2. 手动导入,在工程中只需保留一个最新版本的 jcore-ios-x.x.x.a 静态库文件。
-
开启Target 的 Capabilities->Push Notifications 选项,如图:
1.jpg - 在AppDelegate.m里引入
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
- 初始化
请将以下代码添加到didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里
JpushAppKey是你申请的key
这边是大家经常会犯错的地方,apsForProduction:NO表示采用的是开发证书,YES表示采用生产证书发布应用
//初始化APNs
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
//初始化JPush
#ifdef DEBUG
[JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:NO];
#else
[JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:YES];
#endif
- 为 AppDelegate 添加 Delegate(JPUSHRegisterDelegate) 并添加回调方法
@interface AppDelegate ()<JPUSHRegisterDelegate>
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
@end
- 添加处理 APNs 通知回调方法
#pragma mark- JPUSHRegisterDelegate
// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//从通知界面直接进入应用
}else{
//从通知设置界面进入应用
}
}
// 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 iOS 6
[JPUSHService handleRemoteNotification:userInfo];
}
- 上传registrationID给服务器端(也可以设置别名,设置别名也在这里写)
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
ZPLog(@"------>registrationID: %@",[JPUSHService registrationID]);
//上传极光registrationID给服务器
}];
常见收不到通知的原因
- 环境不对,先检查客户端的方法
初始化代码中 apsForProduction:isProduction 的值需要与客户端的证书环境保持一致 - 后台Api推送的话再去看后台的,后台也有一个配置生产还是测试环境的属性.
- 前后端的appkey不一致
网友评论