源代碼中沒有發(fā)現(xiàn)Remote Notification的代碼械馆。
代碼中有發(fā)現(xiàn)JPush相關(guān)的引用嫉柴,但JPush庫(kù)并沒有引用厌杜。考慮可能是之前使用了JPush计螺,但后面刪除了期奔。
BigApp源碼中有JPush相關(guān)的PHP代碼,因此考慮之前是使用JPush來實(shí)現(xiàn)iOS端的推送的危尿。
Todo:
- 調(diào)查JPush的使用
- 調(diào)查JPush相關(guān)的PHP代碼的使用
發(fā)現(xiàn)了一份最新的iOS客戶端源碼呐萌,帶JPush相關(guān)的設(shè)置,fork到https://github.com/Inspirelife96/Discuz_IOS 谊娇。
新代碼肺孤,從工程文件的變化來看罗晕,添加了JPush和SMS。
Diff了一下其他代碼的變化赠堵,修正了發(fā)帖崩潰的代碼小渊,還有就是添加了分享的代碼。
5/24 更新
看來bigApp確實(shí)使用了JPush茫叭,但最新的源碼只是添加了JPush的注冊(cè)代碼酬屉,但并沒有和用戶綁定。
我們需要做的是:
- 向JPush Server注冊(cè)Device Token揍愁。
- 綁定Device Token和用戶的UserName (UserId呐萨?)
- 修改Badge的值(?根據(jù)推送來源分類莽囤?修改會(huì)困難谬擦?)
剩下的交給服務(wù)器端來做,需要根據(jù)UserName和JPushServer通信向指定的Device推送信息朽缎。
接下來就是各種配置了:
https://developer.apple.com官網(wǎng)要做的事:
- 申請(qǐng)app id (例如com.xxxxxxx.bigapp)惨远。記得開Push Notification選項(xiàng)
- 配置Push Notificaiton的開發(fā)和生產(chǎn)證書。導(dǎo)出相應(yīng)的.p12文件话肖。
- 配置項(xiàng)目對(duì)應(yīng)的開發(fā)和生產(chǎn)Provisioning 文件北秽。
極光https://www.jiguang.cn需要做的事:
- 注冊(cè)并添加一個(gè)新的應(yīng)用
- 配置iOS的推送信息。需要用到上面的.p12文件最筒。
具體的流程可以參考這里iOS最新極光推送詳解
客戶端開發(fā)
參考官方文檔https://docs.jiguang.cn/jpush/client/iOS/ios_guide_new/贺氓,稍微做了下整合。
- 暫時(shí)是手動(dòng)添加的是钥,希望以后能改成cocoapods
- iOS8適配掠归,開啟Push Notification 功能。
- iOS9適配悄泥, 開啟jpush.cn http白名單
- iOS10適配虏冻, UserNotifications
代碼
- 添加Delegate
@interface AppDelegate ()<JPUSHRegisterDelegate>
@end
- 添加初始化APNs代碼以及添加初始化JPush代碼,適配iOS10弹囚,封裝在下面這個(gè)函數(shù)并在
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
調(diào)用
- (void)setupJPush:(NSDictionary *)launchOptions {
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
[self registerForRemoteNotification];
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[JPUSHService setupWithOption:launchOptions appKey:@"56acc62e21172eafa0c54091"
channel:@"iOS"
apsForProduction:NO
advertisingIdentifier:nil];
}
/**
* 初始化UNUserNotificationCenter
*/
- (void)registerForRemoteNotification {
// iOS10 兼容
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
// 使用 UNUserNotificationCenter 來管理通知
UNUserNotificationCenter *uncenter = [UNUserNotificationCenter currentNotificationCenter];
// 監(jiān)聽回調(diào)事件
[uncenter setDelegate:self];
//iOS10 使用以下方法注冊(cè)厨相,才能得到授權(quán)
[uncenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionBadge+UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
//TODO:授權(quán)狀態(tài)改變
NSLog(@"%@" , granted ? @"授權(quán)成功" : @"授權(quán)失敗");
}];
// 獲取當(dāng)前的通知授權(quán)狀態(tài), UNNotificationSettings
[uncenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%s\nline:%@\n-----\n%@\n\n", __func__, @(__LINE__), settings);
/*
UNAuthorizationStatusNotDetermined : 沒有做出選擇
UNAuthorizationStatusDenied : 用戶未授權(quán)
UNAuthorizationStatusAuthorized :用戶已授權(quán)
*/
if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
NSLog(@"未選擇");
} else if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
NSLog(@"未授權(quán)");
} else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
NSLog(@"已授權(quán)");
}
}];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
UIUserNotificationType types = UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
UIRemoteNotificationType types = UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
}
#pragma clang diagnostic pop
}
- 注冊(cè)APNs成功并上報(bào)DeviceToken
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注冊(cè) DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
- 其他回調(diào)函數(shù)
// 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); // 需要執(zhí)行這個(gè)方法,選擇是否提醒用戶鸥鹉,有Badge蛮穿、Sound、Alert三種類型可以選擇設(shè)置
}
// 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(); // 系統(tǒng)要求執(zhí)行這個(gè)方法
}
- (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];
}
#pragma mark - 實(shí)現(xiàn)注冊(cè)APNs失敗接口(可選)
///=============================================================================
/// @name 實(shí)現(xiàn)注冊(cè)APNs失敗接口(可選)
///=============================================================================
/**
* also used in iOS10
*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"%s\n[無法注冊(cè)遠(yuǎn)程提醒, 錯(cuò)誤信息]\nline:%@\n-----\n%@\n\n", __func__, @(__LINE__), error);
}
測(cè)試log輸出:成功在JPush Server上進(jìn)行了注冊(cè)
利用JPush的測(cè)試工具進(jìn)行測(cè)試毁渗,可以看到收到了推送的消息
剩下的事:
- 用戶登錄時(shí)或自動(dòng)登錄時(shí)將UserName綁定這個(gè)registrationID践磅。
- 用戶退出登錄時(shí)將UserName和這個(gè)registrationID解除綁定。
- 其余的交給服務(wù)器端處理【囊欤現(xiàn)在不清楚BigApp插件的php代碼是否已經(jīng)可以順利的進(jìn)行推送府适。
服務(wù)器端開發(fā)
Todo