Show Menu
主题×

推送消息

Adobe Mobile 和 Adobe Mobile SDK 允许您将推送消息发送给用户。该 SDK 还允许您轻松报告在点进推送消息后打开您的应用程序的用户。
本主题中的信息是适用于可能实施的建议。我们强烈建议您查看 Apple 的 iOS 文档,以确定适用于您的应用程序的最佳实施方式。您的实施应由正在使用的框架以及应用程序的目标 iOS 版本来确定。
要使用推送消息,您​ 必须 ​具有 SDK 版本 4.6 或更高版本。
请不要在您的应用程序中手动设置 Experience Cloud ID。这会导致创建一个新的独特用户,该用户将由于其选择启用状态而不接受推送消息。例如,假定已选择接收推送消息的用户登录到您的应用程序。登录后,如果您在应用程序中手动设置 ID,则会创建一个未选择接收推送消息的新独特用户。该新用户将不会接收您的推送消息。

先决条件

不支持将应用程序移动到新的报表包。如果迁移到新报表包,则推送配置可能会中断,并且可能无法发送消息。

启用推送消息

  1. 确认 ADBMobileConfig.json 文件中包含推送消息所需的设置。
    "marketingCloud" 对象必须为推送消息配置了其 "org" 属性。
    "marketingCloud": { 
        "org": "3CE342C92046435B0A490D4C@AdobeOrg" 
    }
    
    
  2. AppDelegate 中导入库。
    #import "ADBMobile.h"
    
    
  3. 要确定应用程序需要获得权限的设置,请参阅 配置远程通知支持
    以下是需要获得警报、徽章、声音和远程通知使用权限的可能实施示例:
    // 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 
    }
    
    
  4. 必须使用 ADBMobile 类中的 setPushIdentifier: 方法将推送令牌传递到 SDK。
    - (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
        ... 
        [ADBMobile setPushIdentifier:deviceToken]; 
        ... 
    }
    
    
  5. 要确定环境的正确实施,请转到 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(); 
    }
    
    
  6. 为了保持您的预计推送受众的准确度,当用户通过在 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