iOS篇 未完待續(xù)
- 導(dǎo)入框架
- Cocoapods 導(dǎo)入
pod 'JPush', '3.1.0'
- 手動導(dǎo)入(比較麻煩,不是很推薦)
- 在極光官網(wǎng)下載最新 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 導(dǎo)入,建議都更新為線上最新版本,來避免 JCore 版本不一致導(dǎo)致的沖突。
2. 手動導(dǎo)入,在工程中只需保留一個最新版本的 jcore-ios-x.x.x.a 靜態(tài)庫文件。
-
開啟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
這邊是大家經(jīng)常會犯錯的地方,apsForProduction:NO表示采用的是開發(fā)證書,YES表示采用生產(chǎn)證書發(fā)布應(yīng)用
//初始化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) 并添加回調(diào)方法
@interface AppDelegate ()<JPUSHRegisterDelegate>
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required - 注冊 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
@end
- 添加處理 APNs 通知回調(diào)方法
#pragma mark- JPUSHRegisterDelegate
// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//從通知界面直接進入應(yīng)用
}else{
//從通知設(shè)置界面進入應(yīng)用
}
}
// 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í)行這個方法,選擇是否提醒用戶,有 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í)行這個方法
}
- (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給服務(wù)器端(也可以設(shè)置別名,設(shè)置別名也在這里寫)
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
ZPLog(@"------>registrationID: %@",[JPUSHService registrationID]);
//上傳極光registrationID給服務(wù)器
}];
公司推送后臺
- 標簽類型
- 項目
- 性別
- 年齡
-
用戶類型
1.png
- 查看通知后的動作
- 默認打開app
- 打開app里的某個模塊
-
打開網(wǎng)頁
2.png
思路:本來是想App 用戶登錄成功后,后臺返回改用戶的標簽數(shù)組給我,然后我調(diào)用此 API 來設(shè)置標簽.但是發(fā)現(xiàn)這些標簽中有個歲數(shù),假設(shè)用戶登錄后,一直不退出,那他的標簽里的歲數(shù)一直就沒變. 商量后,由我App這邊自己組裝好和后臺約定的用戶的標簽數(shù)組,然后每次打開App,都設(shè)置一下標簽
/**
覆蓋tags
調(diào)用該接口會覆蓋用戶所有的tags
@param tags 需要設(shè)置的tags集合
@param completion 響應(yīng)回調(diào)
@param seq 請求序列號
*/
+ (void)setTags:(NSSet<NSString *> *)tags
completion:(JPUSHTagsOperationCompletion)completion
seq:(NSInteger)seq;
常見收不到通知的原因
- 環(huán)境不對,先檢查客戶端的方法
初始化代碼中 apsForProduction:isProduction 的值需要與客戶端的證書環(huán)境保持一致 - 后臺Api推送的話再去看后臺的,后臺也有一個配置生產(chǎn)還是測試環(huán)境的屬性.
- 前后端的appkey不一致


