適用版本
本文匹配的 MTPush iOS SDK 版本為:v3.0.0 及以後版本。
獲取 APNs(通知) 推播內容
適用版本
開始支持的版本:v3.0.0。
功能說明
iOS 設備收到一條推播(APNs),用戶點選推播通知打開應用程式時,應用程式根據狀態不同進行處理需在 AppDelegate 中的以下兩個方法中添加代碼以獲取 apn 內容。
- 如果 App 狀態為未運行,此函數將被調用,如果 launchOptions 包含 UIApplicationLaunchOptionsRemoteNotificationKey 表示用戶點選 apn 通知導緻 app 被啓動運行;如果不含有對應鍵值則表示 App 不是因點選 apn 而被啓動,可能為直接點選 icon 被啓動或其他。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
// apn 內容獲取:
NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]
- 基於 iOS 6 及以下的係統版本,如果 App 狀態為正在前台或者點選通知欄的通知訊息,那麼此函數將被調用,並且可通過 AppDelegate 的 applicationState 是否為 UIApplicationStateActive 判斷程式是否在前台運行。此種情況在此函數中處理:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
- 基於 iOS 7 及以上的係統版本,如果是使用 iOS 7 的 Remote Notification 特性那麼處理函數需要使用:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
- 基於 iOS 10 及以上的係統版本,原 [application: didReceiveRemoteNotification:] 將會被係統廢棄, 由新增 UserNotifications Framework中的[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] 或者 [UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:] 方法替代。 在當前版本及以上可實現 SDK 封裝的 MTPushRegisterDelegate 協議方法,適配 iOS10 新增的 delegate 協議方法。 即以下兩個方法:
- (void)mtpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler;
// NSDictionary * userInfo = notification.request.content.userInfo;
// APNs 內容為 userInfo
- (void)mtpNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler;
// NSDictionary * userInfo = response.notification.request.content.userInfo;
// APNs 內容為 userInfo
示例代碼
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// 取得 APNs 標準信息內容
NSDictionary *aps = [userInfo valueForKey:@"aps"];
NSString *content = [aps valueForKey:@"alert"]; //推播顯示的內容
NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge 數量
NSString *sound = [aps valueForKey:@"sound"]; //播放的聲音
// 取得 Extras 字段內容
NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"]; //服務端中 Extras 字段,key 是自己定義的
NSLog(@"content =[%@], badge=[%d], sound=[%@], customize field =[%@]",content,badge,sound,customizeField1);
// iOS 10 以下 Required
[MTPushService handleRemoteNotification:userInfo];
}
//iOS 7 Remote Notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"this is iOS7 Remote Notification");
// iOS 10 以下 Required
[MTPushService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark- MTPushRegisterDelegate // 2.1.9 版新增MTPushRegisterDelegate,需實現以下兩個方法
// iOS 10 Support
- (void)mtpNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[MTPushService handleRemoteNotification:userInfo];
}
else {
// 在地通知
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要執行這個方法,選擇是否提醒用戶,有 Badge、Sound、Alert 三種類型可以選擇設置
}
// iOS 10 Support
- (void)mtpNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[MTPushService handleRemoteNotification:userInfo];
}
else {
// 在地通知
}
completionHandler(); // 係統要求執行這個方法
}
- 基於iOS 12以上的版本,UserNotifications Framework新增回調方法[userNotificationCenter:openSettingsForNotification:],在3.1.1及以上版本MTPushRegisterDelegate同樣新增了對應的回調方法。當從應用程式外部通知介面或通知設置介面進入應用程式時,該方法將回調。
// iOS 12 Support
- (void)mtpNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
if (notification) {
//從通知介面直接進入應用程式
}else{
//從通知設置介面進入應用程式
}
}
參考文檔:Handling Local and Remote Notifications
獲取自訂訊息推播內容
適用版本
開始支持的版本:v3.0.0。
功能說明
- 隻有在前端運行的時候才能收到自訂訊息的推播。
- 從 MTPush 服務器獲取用戶推播的自訂訊息內容和標題以及附加字段等。
實現方法
獲取 iOS 的推播內容需要在 delegate 類中註冊通知並實現回調方法。
在方法- (BOOL)application:(UIApplication _)application didFinishLaunchingWithOptions:(NSDictionary _) launchOptions 加入下面的代碼:
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kMTCNetworkDidReceiveMessageNotification object:nil];
實現回調方法 networkDidReceiveMessage
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary * userInfo = [notification userInfo];
NSString *content = [userInfo valueForKey:@"content"];
NSString *messageID = [userInfo valueForKey:@"_j_msgid"];
NSDictionary *extras = [userInfo valueForKey:@"extras"];
NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服務端傳遞的 Extras 附加字段,key 是自己定義的
}
參數說明
- content:獲取推播的內容
- messageID:獲取推播的 messageID(key 為 @"_j_msgid")
- extras:獲取用戶自訂參數
- customizeField1:根據自訂 key 獲取自訂的 value
更多實現參考 SDK 下載壓縮包中的 demo。
獲取 RegistrationID
適用版本
開始支持的版本:v3.0.0。
RegistrationID 定義
集成了 MTPush SDK 的應用程式在第一次成功註冊到 MTPush 服務器時,MTPush 服務器會給客戶端返回一個唯一的該設備的標識 - RegistrationID。MTPush SDK 會以廣播的形式傳送 RegistrationID 到應用程式。
應用程式可以把此 RegistrationID 保存以自己的應用程式服務器上,然後就可以根據 RegistrationID 來嚮設備推播訊息或者通知。
獲取 registrationID(with block)
接口定義
+ (void)registrationIDCompletionHandler:(void(^)(int resCode,NSString *registrationID))completionHandler;
參數說明
- (void(^)(int resCode,NSString *registrationID))completionHandler
- completionHandler 用於處理設置返回結果
- resCode 返回的結果狀態碼
- registrationID 返回 registrationID
[MTPushService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
NSLog(@"resCode : %d,registrationID: %@",resCode,registrationID);
}];
溫馨提示: 建議使用此接口獲取 registrationID,類比器中調用此接口 resCode 返回 1011,registrationID 返回 nil。
獲取 registrationID
調用此 API 來取得應用程式對應的 RegistrationID。 隻有當應用程式成功註冊到 MTPush 的服務器時才返回對應的值,否則返回空字符串。
接口定義
+(NSString *)registrationID
溫馨提示: iOS 9 係統,應用程式卸載重裝,APNs 返回的 devicetoken 會發生變化,開發者需要獲取設備最新的 Registration id。請在 kMTCNetworkDidLoginNotification 的實現方法裏面調用 "RegistrationID" 這個接口來獲取 RegistrationID。
附加說明
通過 RegistrationID 推播訊息和通知
可以通過 RegistrationID 來推播訊息和通知,當 audience 參數為 RegistrationID 時候即可根據 RegistrationID 推播。
設置 Badge
適用版本
開始支持的版本:v3.0.0。
功能說明
badge 是 iOS 用來標記應用程式狀態的一個數字,出現在程式圖示右上角。 MTPush 封裝 badge 功能,允許應用程式上載 badge 值至 MTPush 服務器,由 MTPush 後台幫助管理每個用戶所對應的推播 badge 值,簡化了設置推播 badge 的操作。
實際應用程式中,開發者可以直接對 badge 值做增減操作,無需自己維護用戶與 badge 值之間的對應關係。 推播訊息時,隻需在控製台 設置角標 +1,EngageLab 會在服務器中存儲的每個用戶的 badge 值上自動 +1 後傳送給用戶。
設置角標
設置 MTPush 服務器中存儲的 badge 值
接口定義
+ (BOOL)setBadge:(int)value
參數說明
- value 取值範圍:[0,99999]
在地仍須調用 UIApplication:setApplicationIconBadgeNumber 函數設置圖示上顯示的 badge 值
- 返回值
- 在 value 的取值區間內返回 TRUE,否則返回 FALSE
清空角標
清空 MTPush 服務器中存儲的 badge 值,即 [setBadge:0]
接口定義
+ (void)resetBadge
在地通知
適用版本
開始支持的版本:v3.0.0。
功能說明
iOS 設備收到一條在地通知,用戶點選通知打開應用程式時,應用程式根據狀態不同進行處理需在 AppDelegate 中的以下兩個方法中添加代碼以獲取在地通知內容:
- 如果 App 狀態為未運行,此函數將被調用,如果 launchOptions 包含 UIApplicationLaunchOptionsLocalNotificationKey 表示用戶點選在地通知導緻 app 被啓動運行;如果不含有對應鍵值則表示 App 不是因點選在地通知而被啓動,可能為直接點選 icon 被啓動或其他。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
// 在地通知內容獲取:NSDictionary *localNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey]
- 如果 App 狀態為正在前台或者後台運行,那麼此函數將被調用,並且可通過 AppDelegate的applicationState 是否為 UIApplicationStateActive 判斷程式是否在前台運行。此種情況在此函數中處理:
// NS_DEPRECATED_IOS(4_0, 10_0, "Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]")
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
// 在地通知為 notification
- 在 iOS 10 以上上述方法將被係統廢棄,由新增 UserNotifications Framework 中的
-[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:]
或者-[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:]
方法替代。為此,SDK 封裝了 MTPushRegisterDelegate 協議,隻需實現相應的協議方法即可適配 iOS 10 新增的delegate方法,與上述遠程推播新回調方法一緻,也即是如下方法:
- (void)mtpNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^) (NSInteger))completionHandler;
// if (![notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// 在地通知為 notification
// }
- (void)mtpNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler: (void (^)())completionHandler;
// if (![response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// 在地通知為 response.notification
// }
註冊/更新在地通知
功能說明
API 用於註冊或更新推播(支持 iOS 10,並兼容 iOS 10 以下版本)。
接口定義
+ (void)addNotification:(MTPushNotificationRequest *)request;
參數說明
- request [MTPushNotificationRequest] 實體類型,可傳入推播的屬性。
調用說明
request 中傳入已有推播的 request.requestIdentifier 即更新已有的推播,否則為註冊新推播。
代碼示例
- (void)testAddNotification {
MTPushNotificationContent *content = [[MTPushNotificationContent alloc] init];
content.title = @"Test Notifications";
content.subtitle = @"2016";
content.body = @"This is a test code";
content.badge = @1;
content.categoryIdentifier = @"Custom Category Name";
// 5s 後提醒 iOS 10 以上支持
MTPushNotificationTrigger *trigger1 = [[MTPushNotificationTrigger alloc] init];
trigger1.timeInterval = 5;
//每小時重複 1 次 iOS 10 以上支持
MTPushNotificationTrigger *trigger2 = [[MTPushNotificationTrigger alloc] init];
trigger2.timeInterval = 3600;
trigger2.repeat = YES;
//每周一早上 8:00 提醒,iOS 10 以上支持
NSDateComponents *components = [[NSDateComponents alloc] init];
components.weekday = 2;
components.hour = 8;
MTPushNotificationTrigger *trigger3 = [[MTPushNotificationTrigger alloc] init];
trigger3.dateComponents = components;
trigger3.repeat = YES;
//#import <CoreLocation/CoreLocation.h>
//一到某地點提醒,iOS 8 以上支持
CLLocationCoordinate2D cen = CLLocationCoordinate2DMake(37.335400, -122.009201);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:cen
radius:2000.0
identifier:@"engagelab"];
MTPushNotificationTrigger *trigger4 = [[MTPushNotificationTrigger alloc] init];
trigger4.region = region;
//5s 後提醒,iOS 10 以下支持
MTPushNotificationTrigger *trigger5 = [[MTPushNotificationTrigger alloc] init];
trigger5.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
MTPushNotificationRequest *request = [[MTPushNotificationRequest alloc] init];
request.requestIdentifier = @"sampleRequest";
request.content = content;
request.trigger = trigger1;//trigger2;//trigger3;//trigger4;//trigger5;
request.completionHandler = ^(id result) {
NSLog(@"結果返回:%@", result);
};
[MTPushService addNotification:request];
}
移除本地通知
功能說明
API 用於移除待推播或已在通知中心顯示的推播(支持 iOS 10,並兼容 iOS 10 以下版本)。
接口定義
+ (void)removeNotification:(MTPushNotificationIdentifier *)identifier;
參數說明
- identifier [MTPushNotificationIdentifier]實體類型。
調用說明
- iOS 10 以上 identifier 設置為 nil,則移除所有在通知中心顯示推播和待推播請求,也可以通過設置 identifier.delivered 和 identifier.identifiers 來移除相應在通知中心顯示推播或待推播請求,identifier.identifiers 如果設置為 nil 或空數組則移除相應標誌下所有在通知中心顯示推播或待推播請求。
- iOS 10 以下 identifier 設置為 nil,則移除所有推播,identifier.delivered 屬性無效,另外可以通過 identifier.notificationObj 傳入特定推播對象來移除此推播。
代碼示例
- (void)testRemoveNotification {
MTPushNotificationIdentifier *identifier = [[MTPushNotificationIdentifier alloc] init];
identifier.identifiers = @[@"sampleRequest"];
identifier.delivered = YES; //iOS 10 以上有效,等於 YES 則在通知中心顯示的裏面移除,等於 NO 則為在待推播的裏面移除;iOS 10 以下無效
[MTPushService removeNotification:identifier];
}
- (void)testRemoveAllNotification {
[MTPushService removeNotification:nil]; // iOS 10 以下移除所有推播;iOS 10 以上移除所有在通知中心顯示推播和待推播請求
// //iOS 10 以上支援
// MTPushNotificationIdentifier *identifier = [[MTPushNotificationIdentifier alloc] init];
// identifier.identifiers = nil;
// identifier.delivered = YES; //等於 YES 則移除所有在通知中心顯示的,等於 NO 則為移除所有待推播的
// [MTPushService removeNotification:identifier];
}
查找本地通知
功能說明
API 用於查找推播(支持 iOS 10,並兼容 iOS 10 以下版本)。
接口定義
+ (void)findNotification:(MTPushNotificationIdentifier *)identifier;
參數說明
- identifier [MTPushNotificationIdentifier]實體類型
調用說明
- iOS 10 以上可以通過設置 identifier.delivered和identifier.identifiers 來查找相應在通知中心顯示推播或待推播請求,identifier.identifiers 如果設置為 nil 或空數組則返回相應標誌下所有在通知中心顯示推播或待推播請求;iOS 10 以下 identifier.delivered 屬性無效,identifier.identifiers 如果設置 nil 或空數組則返回所有未觸發的推播。
- 須要設置 identifier.findCompletionHandler 回調才能得到查找結果,通過 (NSArray *results) 返回相應對象數組。
代碼示例
- (void)testFindNotification {
MTPushNotificationIdentifier *identifier = [[MTPushNotificationIdentifier alloc] init];
identifier.identifiers = @[@"sampleRequest"];
identifier.delivered = YES; //iOS 10 以上有效,等於 YES 則在通知中心顯示的裏面查找,等於 NO 則在待推播的裏面查找;iOS10 以下無效
identifier.findCompletionHandler = ^(NSArray *results) {
NSLog(@"返回結果為:%@", results); // iOS 10 以下返回 UILocalNotification 對象數組,iOS10 以上根據 delivered 傳入值返回 UNNotification 或 UNNotificationRequest 對象數組
};
[MTPushService findNotification:identifier];
}
- (void)testFindAllNotification {
MTPushNotificationIdentifier *identifier = [[MTPushNotificationIdentifier alloc] init];
identifier.identifiers = nil;
identifier.delivered = YES; //iOS 10 以上有效,等於 YES 則查找所有在通知中心顯示的,等於 NO 則為查找所有待推播的;iOS 10 以下無效
identifier.findCompletionHandler = ^(NSArray *results) {
NSLog(@"返回結果為:%@", results); // iOS 10 以下返回 UILocalNotification 對象數組,iOS 10 以上根據 delivered 傳入值返回 UNNotification 或 UNNotificationRequest 對象數組
};
[MTPushService findNotification:identifier];
}
日誌等級設置
適用版本
開始支持的版本:v3.0.0。
開啓 Debug 模式
功能說明
API 用於開啓 Debug 模式,顯示更多的日誌信息。
接口定義
+ (void)setDebugMode;
調用說明
當需要了解更多的調試信息時候,調用 API 開啓 Debug 模式。
代碼示例
[MTPushService setDebugMode];
關閉日誌信息
功能說明
API 用來關閉日誌信息(除了必要的錯誤信息)。
接口定義
+ (void)setLogOFF;
調用說明
不需要任何調試信息的時候,調用此 API (PO時建議調用此 API,用來屏蔽日誌信息,節省性能消耗)。
代碼示例
[MTPushService setLogOFF];
設置手機號碼
適用版本
開始支持的版本:v3.0.0。
功能說明
用於簡訊補充功能。設置手機號碼後,可實現“推播不到簡訊到”的通知方式,提高推播達到率。
接口定義
+ (void)setMobileNumber:(NSString *)mobileNumber completion:(void (^)(NSError *error))completion
參數說明
- mobileNumber 手機號碼。隻能以 “+” 或者數字開頭,後面的內容隻能包含 “-” 和數字,並且長度不能超過 20。如果傳 nil 或空串則為解除號碼綁定操作。
- completion 回響回調。成功則 error 為空,失敗則 error 帶有錯誤碼及錯誤信息,具體錯誤碼詳見錯誤碼定義。
調用說明
此接口調用頻率有限製,10s 之內最多 3 次。建議在登入成功以後,再調用此接口。結果信息通過 completion 異步返回,也可將completion 設置為 nil 不處理結果信息。
代碼示例
[MTPushService setMobileNumber:@"xxx" completion:^(NSError *error) {
if (error) {
NSLog(@"error:%@", error);
}
else {
// success
}
}];
上報語言信息
適用版本
開始支持的版本:v3.0.0。
功能說明
API 用於上報用戶語言信息
接口定義
+ (void)setUserLanguage:(NSString *)language completionHandler:(void(^)(int resCode, NSError *error))handler;
參數說明
- language:語言信息
- handler:上報回調
調用說明
建議在登入成功之後調用此接口。
代碼示例
[MTPushService setUserLanguage:@"zh_Hans" completionHandler:^(int resCode, NSError *error) {
NSLog(@"language report: %d, %@", resCode, error);
}];
設置TCP加密模式
適用版本
開始支持的版本:v3.3.0。
功能說明
API 用於設置是否TCP加密連接
接口定義
- (void)setTcpSSL:(BOOL)isSSL;
參數說明
- isSSL:YES 加密, NO 不加密
調用說明
請在初始化接口前調用。
代碼示例
[MTPushService setTcpSSL:YES];
Notification Service Extension 相關接口
支持的版本
Notification Service Extension SDK v3.0.0 及以後的版本。
功能說明
使用 Notification Service Extension SDK 上報推播送達情況。
設置 appkey 接口
設置 appkey 接口,必須提前調用
接口定義
+ (void)mtpushSetAppkey:(NSString *)appkey
參數說明
- appkey 需要和 main app 中的 MTPush SDK 的 appkey 保持一緻
訊息展示統計
訊息送達統計接口,調用該接口上報 APNs 訊息體中的 MTPush 相關數據。
接口定義
+ (void)mtpushReceiveNotificationRequest:(UNNotificationRequest *)request with:(void (^)(void))completion
參數說明
- request UNNotificationRequest
- completion 訊息送達上報回調,請在該回調中執行顯示 APNs 等操作。
關閉日誌
預設為開啓,建議 PO 時關閉以減少不必要的 IO
接口定義
+ (void)setLogOff