iOS API 指南

最新更新:2022-12-05

適用版本

本文匹配的 MTPush iOS SDK 版本為:v3.0.0 及以後版本。

設置數據中心

功能說明

此功能用於匹配你在EngageLab門戶(Portal)所選定的數據中心。請確保你所設置的數據中心和在門戶端選定的一致。 不設置的話,SDK會使用默認的新加坡數據中心節點。

支持的版本

開始支持的版本:v4.3.0

接口定義

  • (void)setSiteName:(NSString *)siteName;

參數說明

  • siteName

    • 數據中心的名稱

調用說明

請在調用初始化接口(setupWithOption:channel:apsForProduction:advertisingIdentifier)之前調用此接口。

標籤與別名 API(iOS)

功能說明

溫馨提示,設置標籤別名請注意處理 call back 結果。

只有 call back 返回值為 0 才設置成功,才可以向目標推送。否則服務器 API 會返回 1011 錯誤。所有回調函數都在主線程運行。


提供幾個相關 API 用來操作別名(alias)與標籤(tags)。

這幾個 API 可以在 App 裡任何地方調用。

別名 alias

為安裝了應用程序的用戶,取個別名來標識。以後給該用戶 Push 消息時,就可以用此別名來指定。 每個用戶只能指定一個別名。

同一個應用程序內,對不同的用戶,建議取不同的別名。這樣,盡可能根據別名來唯一確定用戶。

舉例:在一個用戶要登錄的遊戲中,可能設置別名為 userid。遊戲運營時,發現該用戶 3 天沒有玩遊戲了,則根據 userid 調用服務器端 API 發通知到客戶端提醒用戶。

標籤 tag

為安裝了應用程序的用戶,打上標籤。其目的主要是方便開發者根據標籤,來批量下發 Push 消息。

可為每個用戶打多個標籤。

舉例: game, old_page, women

新增標籤

調用此 API 來增加標籤,在 block 中返回結果

Note:這個接口是增加邏輯,而不是覆蓋邏輯

支持的版本

開始支持的版本:v3.5.0。

接口定義

+ (void)addTags:(NSSet<NSString *> *)tags completion:(MTPushTagsOperationCompletion)completion seq:(NSInteger)seq;
          + (void)addTags:(NSSet<NSString *> *)tags
 completion:(MTPushTagsOperationCompletion)completion
        seq:(NSInteger)seq;

        
此代碼塊在浮窗中顯示

參數說明

  • completion

    • 回調返回的 tags 為查詢結果。返回對應的狀態碼:0 為成功,其他返回碼請參考錯誤碼定義。 seq 為調用時傳入的會話序列號
  • seq

    • 請求時傳入的序列號,會在回調時原樣返回

Alias Block

typedef void (^MTPushAliasOperationCompletion)(NSInteger iResCode, NSString *iAlias, NSInteger seq);
          typedef void (^MTPushAliasOperationCompletion)(NSInteger iResCode, NSString *iAlias, NSInteger seq);

        
此代碼塊在浮窗中顯示

獲取 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]
          - (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;
          - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

        
此代碼塊在浮窗中顯示
  • 基於 iOS 7 及以上的系統版本,如果是使用 iOS 7 的 Remote Notification 特性那麼處理函數需要使用:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;
          - (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)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(); // 系統要求執行這個方法 }
          - (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{ //從通知設置界面進入應用 } }
          // 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];
              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 是自己定義的   }
              - (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)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); }];
          [MTPushService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
    NSLog(@"resCode : %d,registrationID: %@",resCode,registrationID);
}];

        
此代碼塊在浮窗中顯示

溫馨提示:
建議使用此接口獲取 registrationID,模擬器中調用此接口 resCode 返回 1011,registrationID 返回 nil。

獲取 registrationID

調用此 API 來取得應用程序對應的 RegistrationID。只有當應用程序成功註冊到 MTPush 的服務器時才返回對應的值,否則返回空字符串。

接口定義

+(NSString *)registrationID
          +(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 後下發給用戶。

設置角標

API setBadge 設置 MTPush 服務器中存儲的 badge 值

接口定義

+ (BOOL)setBadge:(int)value
          + (BOOL)setBadge:(int)value

        
此代碼塊在浮窗中顯示

參數說明

  • value 取值範圍:[0,99999]

本地仍須調用 UIApplication:setApplicationIconBadgeNumber 函數設置圖標上顯示的 badge 值

  • 返回值
    • 在 value 的取值區間內返回 TRUE,否則返回 FALSE

清空角標

清空 MTPush 服務器中存儲的 badge 值,即 [setBadge:0]

接口定義

+ (void)resetBadge
          + (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]
          - (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
          // 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 // }
          - (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 
  // }

        
此代碼塊在浮窗中顯示

註冊/更新本地通知

支持的版本

開始支持的版本:v3.0.0。

功能說明

API 用於註冊或更新推送(支持 iOS 10,並兼容 iOS 10 以下版本)。

接口定義

+ (void)addNotification:(MTPushNotificationRequest *)request;
          + (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]; }
          - (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];
}

        
此代碼塊在浮窗中顯示

移除本地通知

支持的版本

開始支持的版本:v3.0.0。

功能說明

API 用於移除待推送或已在通知中心顯示的推送(支持 iOS 10,並兼容 iOS 10 以下版本)。

接口定義

+ (void)removeNotification:(MTPushNotificationIdentifier *)identifier;
          + (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]; }
          - (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];
}

        
此代碼塊在浮窗中顯示

查找本地通知

支持的版本

開始支持的版本:v3.0.0。

功能說明

API 用於查找推送(支持 iOS 10,並兼容 iOS 10 以下版本)。

接口定義

+ (void)findNotification:(MTPushNotificationIdentifier *)identifier;
          + (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]; }
          - (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;
          + (void)setDebugMode;

        
此代碼塊在浮窗中顯示

調用說明

當需要了解更多的調試信息時候,調用 API 開啟 Debug 模式。

代碼示例

[MTPushService setDebugMode];
          [MTPushService setDebugMode];

        
此代碼塊在浮窗中顯示

關閉日誌信息

支持的版本

開始支持的版本:v3.0.0。

功能說明

API 用來關閉日誌信息(除了必要的錯誤信息)。

接口定義

+ (void)setLogOFF;
          + (void)setLogOFF;

        
此代碼塊在浮窗中顯示

調用說明

不需要任何調試信息的時候,調用此 API (發佈時建議調用此 API,用來屏蔽日誌信息,節省性能消耗)。

代碼示例

[MTPushService setLogOFF];
          [MTPushService setLogOFF];

        
此代碼塊在浮窗中顯示

設置手機號碼

支持的版本

開始支持的版本:v3.0.0。

功能說明

用於短信補充功能。設置手機號碼後,可實現“推送不到短信到”的通知方式,提高推送達到率。

接口定義

+ (void)setMobileNumber:(NSString *)mobileNumber completion:(void (^)(NSError *error))completion
          + (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 } }];
          [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;
          + (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); }];
           [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;
          + (void)setTcpSSL:(BOOL)isSSL;

        
此代碼塊在浮窗中顯示

參數說明

  • isSSL:YES 加密, NO 不加密

調用說明

請在初始化接口前調用。

代碼示例

[MTPushService setTcpSSL:YES];
            [MTPushService setTcpSSL:YES];

        
此代碼塊在浮窗中顯示

Notification Service Extension 相關接口

支持的版本

Notification Service Extension SDK v3.0.0 及以後的版本。

功能說明

使用 Notification Service Extension SDK 上報推送送達情況。

設置 appkey 接口

設置 appkey 接口,必須提前調用

接口定義

+ (void)mtpushSetAppkey:(NSString *)appkey
          + (void)mtpushSetAppkey:(NSString *)appkey

        
此代碼塊在浮窗中顯示

參數說明

  • appkey  需要和 main app 中的 MTPush SDK 的 appkey 保持一致

消息展示統計

消息送達統計接口,調用該接口上報 APNs 消息體中的 MTPush 相關數據。

接口定義

+ (void)mtpushReceiveNotificationRequest:(UNNotificationRequest *)request with:(void (^)(void))completion
          + (void)mtpushReceiveNotificationRequest:(UNNotificationRequest *)request with:(void (^)(void))completion

        
此代碼塊在浮窗中顯示

參數說明

  • request  UNNotificationRequest
  • completion 消息送達上報回調,請在該回調中執行顯示 APNs 等操作。

關閉日誌

默認為開啟,建議發佈時關閉以減少不必要的 IO

接口定義

+ (void)setLogOff
          + (void)setLogOff

        
此代碼塊在浮窗中顯示
在文档中心打开