iOS SDK統合ガイド

対応バージョン

この記事はMTPush iOS SDKバージョン:v3.0.0以降に対応しています。

リソースファイル

パッケージ名:engagelab-ios-{バージョン番号}

バージョン4.3.3からは、.aパッケージの提供を停止し、.xcframeworkパッケージのみを提供します。

  • libフォルダ:mtpush-ios-x.x.x.xcframework、mtpush-extension-ios-x.x.x.xcframeworkが含まれています(注意:シミュレータではAPNsはサポートされていません)。
  • READMEファイル:SDKに関する説明
  • demoフォルダ:サンプル

アプリケーション情報の取得

コンソールでアプリケーションを作成すると、自動的にAppKeyが生成され、アプリケーションを識別するために使用されます。

プロジェクトの設定

SDKのインポート

Cocoapodsインポート

pod 'MTPush' 注意:最新バージョンをインポートできない場合は、pod repo update masterコマンドを実行してローカルのpodライブラリを更新した後、再度pod 'MTPush'を実行してください。
              
                  pod 'MTPush'

    注意:最新バージョンをインポートできない場合は、pod repo update masterコマンドを実行してローカルのpodライブラリを更新した後、再度pod 'MTPush'を実行してください。

            
このコードブロックはフローティングウィンドウ内に表示されます
  • 指定したバージョンをインストールする場合は、次の方法を使用します(MTPush 3.5.0を例として):
pod 'MTPush', '3.5.0'
              
                  pod 'MTPush', '3.5.0'

            
このコードブロックはフローティングウィンドウ内に表示されます

手動インポート

  • SDKパッケージを解凍し、Xcodeで「Add files to 'Your project name'...」を選択し、mtpush-ios-x.x.x.xcframeworkをプロジェクトディレクトリに追加します。
  • Frameworkの追加
    • CFNetwork.framework
    • CoreFoundation.framework
    • CoreTelephony.framework
    • SystemConfiguration.framework
    • CoreGraphics.framework
    • Foundation.framework
    • UIKit.framework
    • Security.framework
    • libz.tbd
    • UserNotifications.framework
    • libresolv.tbd
    • libsqlite3.tbd

Build Settings

プロジェクトがiOS 7.0未満のオペレーティングシステムをサポートする必要がある場合は、Build Settingsに移動し、bitCodeオプションをオフにしてください。そうしないと、正常にコンパイルされません。

  • Search Pathsの下にあるUser Header Search PathsとLibrary Search Pathsを設定します。例えば、SDKフォルダ(デフォルトはlib)がプロジェクトファイルと同じ階層にある場合は、このパラメータを$(SRCROOT)/{静的ライブラリが存在するフォルダの名前}に設定します。

Capabilities

Xcode 8以降を使用して開発している場合は、アプリケーションのTargetのCapabilities->Push Notificationsオプションを有効にしてください。以下の図を参照してください: 1.png
Xcode 10以降の環境で開発している場合は、アプリケーションのTargetのCapabilities->Access WIFI Infomationオプションを有効にしてください。
image.png

ヘッダーファイルの追加

AppDelegate.mのヘッダーファイルの参照位置に次のコードを追加します。

// MTPush機能に必要なヘッダーファイルをインポート #import "MTPushService.h" // iOS10でAPNsを登録するために必要なヘッダーファイル #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif // idfa機能を使用する必要がある場合は導入するヘッダーファイル(オプション) #import <AdSupport/AdSupport.h>
              
              // MTPush機能に必要なヘッダーファイルをインポート
#import "MTPushService.h"
// iOS10でAPNsを登録するために必要なヘッダーファイル
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// idfa機能を使用する必要がある場合は導入するヘッダーファイル(オプション)
#import <AdSupport/AdSupport.h>

            
このコードブロックはフローティングウィンドウ内に表示されます

Delegateの追加

AppDelegateにDelegateを追加します。次のコードを参照してください:

@interface AppDelegate ()<MTPushRegisterDelegate> @end
              
              @interface AppDelegate ()<MTPushRegisterDelegate>

@end

            
このコードブロックはフローティングウィンドウ内に表示されます

初期化コードの追加

APNs初期化コードの追加

次のコードを-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsに追加してください。

//必須 MTPushRegisterEntity * entity = [[MTPushRegisterEntity alloc] init]; entity.types = MTPushAuthorizationOptionAlert|MTPushAuthorizationOptionSound|MTPushAuthorizationOptionProvidesAppNotificationSettings; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // カスタマイズでcategoriesを追加できます // iOS10以降はNSSet<UNNotificationCategory *> *categories // iOS8およびiOS9はNSSet<UIUserNotificationCategory *> *categories } [MTPushService registerForRemoteNotificationConfig:entity delegate:self];
              
                //必須
 
    MTPushRegisterEntity * entity = [[MTPushRegisterEntity alloc] init];
  entity.types = MTPushAuthorizationOptionAlert|MTPushAuthorizationOptionSound|MTPushAuthorizationOptionProvidesAppNotificationSettings;
  if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    // カスタマイズでcategoriesを追加できます
    // iOS10以降はNSSet<UNNotificationCategory *> *categories
    // iOS8およびiOS9はNSSet<UIUserNotificationCategory *> *categories
  }
  [MTPushService registerForRemoteNotificationConfig:entity delegate:self];

            
このコードブロックはフローティングウィンドウ内に表示されます

MTPush初期化コードの追加

次のコードを-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsに追加してください。

// オプション // IDFAを取得 // IDFA機能を使用するには、このコードを追加し、初期化メソッドのadvertisingIdentifierパラメータを入力します NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; // 必須 // Pushを初期化 [MTPushService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];
              
              // オプション
// IDFAを取得
// IDFA機能を使用するには、このコードを追加し、初期化メソッドのadvertisingIdentifierパラメータを入力します
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

// 必須
// Pushを初期化
[MTPushService setupWithOption:launchOptions appKey:appKey
                      channel:channel
             apsForProduction:isProduction
        advertisingIdentifier:advertisingId];

            
このコードブロックはフローティングウィンドウ内に表示されます
パラメータの説明
  • appKey
    • アプリケーションに設定されているappkeyが、コンソールでアプリケーションを作成した後に生成されたappkeyと同じであることを確認してください。
  • channel
    • アプリケーションパッケージのダウンロードチャネルを指定します。チャネルごとの統計を容易にするために、App Storeなどの特定の値を自分で定義できます。
  • apsForProduction
    • このパラメータは、現在のアプリケーションが使用するAPNs証明書環境を識別します。
    • 0(デフォルト値)は開発証明書を使用することを示し、1はアプリケーションを公開するために本番証明書を使用することを示します。
    • 注意:このフィールドの値は、Build SettingsのCode Signingで設定された証明書環境と同じでなければなりません。

APNsが正常に登録され、DeviceTokenが報告されます。

ヒント:このメソッドを呼び出さなくてもEngageLabにログインできます。ただし、APNs通知は利用できず、カスタムメッセージのみを使用できます。

AppDelegate.mでコールバックメソッドを実装し、コールバックメソッドにコードを追加します:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { /// 必須 - 登録されたDeviceToken [MTPushService registerDeviceToken:deviceToken]; }
              
              - (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

  /// 必須 - 登録されたDeviceToken
  [MTPushService registerDeviceToken:deviceToken];
}

            
このコードブロックはフローティングウィンドウ内に表示されます

APNs登録失敗インターフェースの実装(オプション)

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { //オプション NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); [MTPushService registerDeviceToken:deviceToken]; }
              
              - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  //オプション
  NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
   [MTPushService registerDeviceToken:deviceToken];
}

            
このコードブロックはフローティングウィンドウ内に表示されます

APNs通知を処理するためのコールバックメソッドの追加

AppDelegate.mでコールバックメソッドを実装し、コールバックメソッドにコードを追加します:

#pragma mark- MTPushRegisterDelegate // iOS 12をサポート - (void)mtpNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {// 通知インターフェースから直接アプリケーションに入ります。}else{// 通知設定インターフェースからアプリケーションに入ります。} } // iOS 10をサポート - (void)mtpNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler { // 必須 NSDictionary * userInfo = notification.request.content.userInfo; if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[MTPushService handleRemoteNotification:userInfo]; } completionHandler(UNNotificationPresentationOptionAlert); // このメソッドを実行するには、ユーザーにアラートを表示するかどうかを選択します。Badge、Sound、alertの3種類を設定できます } // iOS 10をサポート - (void)mtpNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { // 必須 NSDictionary * userInfo = response.notification.request.content.userInfo; if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[MTPushService handleRemoteNotification:userInfo]; } completionHandler(); // システムはこのメソッドの実行を要求します} - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // 必須、iOS 7をサポート [MTPushService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { // 必須、iOS 6以下のシステム用 [MTPushService handleRemoteNotification:userInfo]; }
              
              #pragma mark- MTPushRegisterDelegate

// iOS 12をサポート
- (void)mtpNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {// 通知インターフェースから直接アプリケーションに入ります。}else{// 通知設定インターフェースからアプリケーションに入ります。}
}

// iOS 10をサポート
- (void)mtpNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  // 必須
  NSDictionary * userInfo = notification.request.content.userInfo;
  if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[MTPushService handleRemoteNotification:userInfo];
  }
  completionHandler(UNNotificationPresentationOptionAlert); // このメソッドを実行するには、ユーザーにアラートを表示するかどうかを選択します。Badge、Sound、alertの3種類を設定できます
}

// iOS 10をサポート
- (void)mtpNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  // 必須
  NSDictionary * userInfo = response.notification.request.content.userInfo;
  if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[MTPushService handleRemoteNotification:userInfo];
  }
  completionHandler();	// システムはこのメソッドの実行を要求します}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

  // 必須、iOS 7をサポート
  [MTPushService handleRemoteNotification:userInfo];
  completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

  // 必須、iOS 6以下のシステム用
  [MTPushService handleRemoteNotification:userInfo];
}

            
このコードブロックはフローティングウィンドウ内に表示されます

MTPushカスタムメッセージを処理するためのコールバックメソッドの追加

MTPushのカスタムメッセージ機能を使用するには、【iOS APIガイド】を参照してカスタムメッセージ処理コールバックを実装してください。

正常に実行

プロジェクトをデバッグします。コンソールに次のログが出力されれば、統合は成功しています。

2016-08-19 17:12:12.745823 219b28[1443:286814] | MTP | I - [MTCORETcpEventController] ----- login result ----- uid:123456 registrationID:171976fa8a8620a14a4 idc:0
              
              2016-08-19 17:12:12.745823 219b28[1443:286814] | MTP | I - [MTCORETcpEventController] 
----- login result -----
uid:123456 
registrationID:171976fa8a8620a14a4 
idc:0

            
このコードブロックはフローティングウィンドウ内に表示されます

デバッグ中に問題が発生した場合は、【iOS SDKデバッグガイド】を参照してください。

高度な機能

MTush SDK関連イベントのリッスン

開発者は、APIで提供されている次のタイプの通知を含めることを推奨します:

extern NSString *const kMTCNetworkIsConnectingNotification; // 現在確立中
extern NSString * const kMTCNetworkDidSetupNotification; // 接続を確立
extern NSString * const kMTCNetworkDidCloseNotification; // 接続を閉じる
extern NSString * const kMTCNetworkDidRegisterNotification; // 登録に成功
extern NSString *const kMTCNetworkFailedRegisterNotification; // 登録に失敗
extern NSString * const kMTCNetworkDidLoginNotification; // ログインに成功

温暖なヒント: kMTCNetworkDidLoginNotification通知メソッドを追加して登録し、このメソッドでRegistration idを取得するか、registrationIDCompletionHandler:メソッドを呼び出して、completionHandlerを通じて取得することもできます。

extern NSString * const kMTCNetworkDidReceiveMessageNotification; // カスタムメッセージを受信(非APNS)
KMTCNetworkDidReceiveMessageNotificationの転送データは、NSNotificationのuserInfoを通じて取得できます。title、content、extras情報などが含まれています。ドキュメントを参照してください:iOS SDK API

通知統計サービス

温暖なヒント: Notification Service ExtensionはiOS 10の新機能で、mutable-contentフィールドによって制御されます。 Webコンソールを使用してプッシュする場合は、オプション設定でmutable-contentオプションをチェックしてください。RESTFul APIを使用してプッシュする場合は、mutable-contentフィールドをtrueに設定してください。

開発者は、Notification Service Extension SDKを使用して、各APNsメッセージの配信状況を報告できます。 注意:EngageLabコンソールに表示されるインプレッションデータは、ここで報告された配信データから取得されます。

alt text

使用方法:

  • Service Extension Serviceを作成します。詳細はiOS 10 Service Extensionを参照してください。
  • mtpush-extension-ios-xxx.xcframeworkファイルをService Extensionプロジェクトにインポートします。
  • Frameworkを追加:libz.tbdとlibresolv.tbd。
  • [mtpushSetAppkey:]を呼び出してappkeyを設定します。このappkeyはEngageLabアプリケーションのappkeyと同じである必要があることに注意してください。
  • [mtpushReceiveNotificationRequest:]メソッドを呼び出してapnsを報告し、統計が適切に提供されるようにします。メソッドのブロックコールバックでapnsを表示します。

より具体的な使用例については、バージョンzipパッケージに含まれているDemoコードを参照してください。 参照ドキュメント:iOS SDK API

Voip Push

  • iOS 8で導入された、PushKitフレームワークに基づくVoip pushは、ユーザーのAPPが強制終了された場合でも起動してコードを実行できるようにします。
  • Voipは開発証明書と本番証明書を区別しません。Apple Push Notification service SSL(Sandbox & Production)とVoIP Services Certificateの両方がVoip pushをサポートしています。

PushKit.Frameworkライブラリの追加

  • XcodeのBuild Phases - Link Binary With LibrariesでPushKit.frameworkライブラリを導入します。
  • ヘッダーファイル#import <PushKit/PushKit.h>をインポートします。
  • AppDelegateでPKPushRegistryDelegateプロトコルを実装します。

Voip権限の追加

Project-> Capabilities->Background Modesで、Voice over IPオプションをオンにします。

コードの実装

  • didFinishLaunchingWithOptionsメソッドでVoip登録を実装します。
- (void)voipRegistration{ dispatch_queue_t mainQueue = dispatch_get_main_queue(); PKPushRegistry *voipRegistry = [[PKPushRegistry alloc] initWithQueue:mainQueue]; voipRegistry.delegate = self; // プッシュタイプをVoIPに設定 voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP]; }
              
              - (void)voipRegistration{
  dispatch_queue_t mainQueue = dispatch_get_main_queue();
  PKPushRegistry *voipRegistry = [[PKPushRegistry alloc] initWithQueue:mainQueue];
  voipRegistry.delegate = self;
  // プッシュタイプをVoIPに設定
  voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
}

            
このコードブロックはフローティングウィンドウ内に表示されます
  • Tokenを送信
/// システムがVoipTokenを返し、Engagelabサーバーに報告 - (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)pushCredentials forType:(PKPushType)type{ if (type == PKPushTypeVoIP) { [MTPushService registerVoipToken:pushCredentials.token]; } }
              
              /// システムがVoipTokenを返し、Engagelabサーバーに報告
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)pushCredentials forType:(PKPushType)type{
   if (type == PKPushTypeVoIP) {
    [MTPushService registerVoipToken:pushCredentials.token];
   }
}

            
このコードブロックはフローティングウィンドウ内に表示されます
  • Voip pushを受信すると、受信インターフェースを呼び出してVoip配信を統計します。
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type{ if (type == PKPushTypeVoIP) { // Engagelabサーバーにレシートを送信 [MTPushService handleVoipNotification:payload.dictionaryPayload]; } } - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void(^)(void))completion{ if (type == PKPushTypeVoIP) { // Engagelabサーバーにレシートを送信 [MTPushService handleVoipNotification:payload.dictionaryPayload]; } }
              
              - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type{
  if (type == PKPushTypeVoIP) {
    // Engagelabサーバーにレシートを送信
    [MTPushService handleVoipNotification:payload.dictionaryPayload];
  }
}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void(^)(void))completion{
   if (type == PKPushTypeVoIP) {
    // Engagelabサーバーにレシートを送信
    [MTPushService handleVoipNotification:payload.dictionaryPayload];
   }
}

            
このコードブロックはフローティングウィンドウ内に表示されます

技術サポート

統合中に問題が発生した場合

  • ドキュメントを注意深く読んで、見逃している部分がないか確認してください。
  • EngageLabにアクセスして類似の質問を検索できます。 問題をより迅速に解決するために、ヘルプを求めるときは次の情報を提供してください:
    • 咨询する製品がMTPushであり、他のEngageLab製品を使用しているかどうか。
    • 呼び出しているAPI、渡しているパラメータ、完全なエラーメッセージ、および例外が発生した時点。
    • メッセージを受信しない場合は、アプリケーションのAppkey、メッセージのMessage ID、およびデバイスのregistration IDを提供してください。
    • SDKによって障害が発生した場合は、対応するSDKバージョンと完全なログ記録を提供してください。TXTファイルを使用してログ情報をアップロードしてください。
    • 異常が発生したデバイスがiOSの場合は、具体的なモデルとシステムを提供してください。
icon
お問い合わせ