Adobe Mobile および Adobe Mobile SDK を使用すると、ユーザーにプッシュメッセージを送信できます。また、SDK では、プッシュメッセージをクリックした結果としてアプリを開いたユーザーを簡単に報告できます。
このトピック内の情報では、考えられる実装を提案しています。Apple の iOS ドキュメントを参照して、アプリに最適な実装を特定することを強くお勧めします。実装は、使用しているフレームワークおよびアプリのターゲットとなる iOS のバージョンによって特定する必要があります。
プッシュメッセージを使用するには、SDK バージョン 4.6 以降が必要です。
アプリ内部の Experience Cloud ID を手動で設定しないでください。これにより、新しいユニークユーザーが作成されます。このユーザーはオプトイン状態のため、プッシュメッセージを受信しません。例えば、プッシュメッセージの通知をオプトインしたユーザーが、アプリにログインしたと想定します。ログイン後、アプリ内で手動で ID を設定すると、プッシュメッセージの受信を選択しない新しいユニークユーザーが作成されます。この新しいユーザーは、プッシュメッセージを受信しません。
ライブラリをプロジェクトに追加し、ライフサイクル指標を実装します。
詳しくは、「ライフサイクル指標」を参照してください。
SDK を ID サービスに対して有効化する必要があります。詳しくは、「SDK ID サービスオプションの設定」を参照してください。
新しいレポートスイートへのアプリの移行はサポートされていません。新しいレポートスイートに移行すると、プッシュ設定が破損し、メッセージが送信されない可能性があります。
プッシュメッセージに必要な設定が ADBMobileConfig.json
ファイルに含まれていることを確認します。
"marketingCloud"
オブジェクトの "org"
プロパティをプッシュメッセージ用に設定する必要があります。
"marketingCloud": {
"org": "3CE342C92046435B0A490D4C@AdobeOrg"
}
ライブラリを AppDelegate
に読み込みます。
#import "ADBMobile.h"
アプリケーションが権限を要求する必要がある設定を決定するには、「Configuring Remote Notification Support(リモート通知サポートの設定)」を参照してください。
アラート、バッジ、サウンドおよびリモートの通知を使用する権限をリクエストする実装として考えられる例を以下に示します。
// iOS 10 and newer
if (NSClassFromString(@"UNUserNotificationCenter")) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) { NSLog(@"authorization given"); }
else { NSLog(@"authorization rejected"); }
if (error) { NSLog(@"error during authorization: %@", error.localizedDescription); }
}];
// have to ask for permission for remote notifications separately
[application registerForRemoteNotifications];
// make this class the delegate for user notification handling
center.delegate = self;
}
// iOS 8.0 to iOS 9.3.5
else if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
UIUserNotificationTypetypes = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge| UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:settings];
// have to ask for permission for remote notifications separately
[application registerForRemoteNotifications];
}
// older than iOS 8.0
else {
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeSound];
#pragma GCC diagnostic pop
}
ADBMobile クラスの setPushIdentifier:
メソッドを使用して、プッシュトークンを SDK に渡す必要があります。
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
...
[ADBMobile setPushIdentifier:deviceToken];
...
}
自社の環境に適した実装を判断するには、UserNotifications に移動します。
この手順を使用すると、ユーザーがプッシュメッセージのクリックスルーを使用してアプリを開くときに userInfo
辞書を SDK に渡すことによってプッシュレポートを有効にできます。
以下のコードサンプルは、考えられる実装の一例です。
// device running < iOS 7
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// only send the hit if the app is inactive
if (application.applicationState == UIApplicationStateInactive) {
[ADBMobile trackPushMessageClickThrough:userInfo];
}
}
// device running between iOS 7 and iOS 9.3.5
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult)) completionHandler {
// only send the hit if the app is inactive
if (application.applicationState == UIApplicationStateInactive) {
// only run this code if the UNUserNotificationCenterclass is not available or its delegate is not set (pre iOS 10)
if (!NSClassFromString(@"UNUserNotificationCenter") || ![UNUserNotificationCenter currentNotificationCenter].delegate) {
[ADBMobiletrackPushMessageClickThrough:userInfo];
}
}
completionHandler(UIBackgroundFetchResultNoData);
}
// device running >= iOS 10
- (void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
if ([response.notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class]) {
[ADBMobile trackPushMessageClickThrough:response.notification.request.content.userInfo];
}
completionHandler();
}
推定されるプッシュオーディエンスを正確に保つために、アプリのプッシュメッセージをユーザーが手動で無効にしたときは、AppDelegate
のapplicationDidBecomeActive:
メソッドで [ADBMobile setPushIdentifier: nil]
を呼び出すことによって、SDK に通知します。
// device running < iOS 7
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// only send the hit if the app is inactive
if (application.applicationState == UIApplicationStateInactive) {
[ADBMobile trackPushMessageClickThrough:userInfo];
}
}
// device running between iOS 7 and iOS 9.3.5
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult)) completionHandler {
// only send the hit if the app is inactive
if (application.applicationState == UIApplicationStateInactive) {
// only run this code if the UNUserNotificationCenterclass is not available or its delegate is not set (pre iOS 10)
if (!NSClassFromString(@"UNUserNotificationCenter") || ![UNUserNotificationCenter currentNotificationCenter].delegate) {
[ADBMobiletrackPushMessageClickThrough:userInfo];
}
}
completionHandler(UIBackgroundFetchResultNoData);
}
// device running >= iOS 10
- (void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
if ([response.notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class]) {
[ADBMobile trackPushMessageClickThrough:response.notification.request.content.userInfo];
}
completionHandler();
}
AppDelegate.m
実装の例を以下に示します。
#import "AppDelegate.h"
#import "ADBMobile.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (NSClassFromString(@"UNUserNotificationCenter")) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) { NSLog(@"authorization given"); }
else { NSLog(@"authorization rejected"); }
if (error) { NSLog(@"error during authorization: %@", error.localizedDescription); }
}];
center.delegate = self;
[application registerForRemoteNotifications];
}
else if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge| UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}
else {
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeBadge| UIRemoteNotificationTypeSound];
#pragma GCC diagnostic pop
}
return YES;
}
- (void) applicationDidBecomeActive:(UIApplication *)application {
if (NSClassFromString(@"UNUserNotifcationCenter")) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {
[ADBMobile setPushIdentifier:nil];
}
}];
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
else if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
if (![application isRegisteredForRemoteNotifications]) {
[ADBMobile setPushIdentifier:nil];
}
}
else if ([application enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone) {
[ADBMobile setPushIdentifier:nil];
}
#pragma GCC diagnostic pop
}
- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[ADBMobile setPushIdentifier:deviceToken];
}
- (void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[ADBMobile setPushIdentifier:nil];
}
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateInactive) {
[ADBMobile trackPushMessageClickThrough:userInfo];
}
}
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if (application.applicationState == UIApplicationStateInactive) {
if (!NSClassFromString(@"UNUserNotificationCenter") || ![UNUserNotificationCenter currentNotificationCenter].delegate) {
[ADBMobile trackPushMessageClickThrough:userInfo];
}
}
completionHandler(UIBackgroundFetchResultNoData);
}
- (void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
if ([response.notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class]) {
[ADBMobile trackPushMessageClickThrough:response.notification.request.content.userInfo];
}
completionHandler();
}
@end