集成 Push

更新时间:2024-10-21 10:01:34下载pdf

基于涂鸦 智能生活 App SDK 开发的 App,支持消息推送(Push)功能,即给用户发送运营 Push 和产品的告警 Push。

项目配置

  1. 打开您的 XCode 项目。

  2. TARGETS > Capabilities 的路径,打开 Push Notifications 的开关。

    集成 Push

  3. 登录 涂鸦开发者平台,在您的 App SDK 配置页面,配置 Push 证书 和上传 Push 证书。

    集成 Push

初始化 Push

didFinishLaunchingWithOptions 方法中,初始化 Push:

示例代码

Objective-C:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [application registerForRemoteNotifications];
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
        //iOS10 需要加下面这段代码
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        UNAuthorizationOptions types10 = UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
        [center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                //点击允许
            } else {
                //点击不允许
            }
        }];
    }

}

Swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    application.registerForRemoteNotifications()
    application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
    if #available(iOS 10.0, *) {
    //iOS10 需要加下面这段代码
        let center = UNUserNotificationCenter.current()
        center.delegate = self as? UNUserNotificationCenterDelegate
        let options: UNAuthorizationOptions = [.alert, .badge, .sound]
        center.requestAuthorization(options: options) { (granted, error) in
            if granted {
                //点击允许
            } else {
               //点击不允许
            }
        }
    }
}

注册 Push ID

didRegisterForRemoteNotificationsWithDeviceToken 中,注册 pushId 到智能生活 App SDK。

示例代码

Objective-C:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [ThingSmartSDK sharedInstance].deviceToken = deviceToken;
}

Swift:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    ThingSmartSDK.sharedInstance().deviceToken = deviceToken
}

接收通知

接收到远程通知,在代理方法 didReceiveRemoteNotification 中执行。

示例代码

Objective-C:

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

}

Swift:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        
}

发送 Push

集成 Push 能力后,您就可以在涂鸦开发者平台进行每次的 Push 推送设置。

新增运营 Push

涂鸦开发者平台营销推送 页面,新建App消息推送。用户会在 App 上接收到您的推送提醒,新品上市推荐或者生活温馨提醒等信息,方便有效营销您的用户。

集成 Push

新增设备消息

涂鸦开发者平台消息推送 页面,完成设备信息推送。更多详细操作,请参考 推送设备信息

集成 Push