使用 SDK

启动推送服务

启动推送服务。

接口定义

- (void)startPushWithDelegate:(nullable id<TMFPushDelegate>)delegate;

参数说明

参数 类型 描述 必选
delegate TMFPushDelegate 实现 TMFPushDelegate 协议的回调对象,一般是 AppDelegate N

调用示例

[[TMFPush defaultManager] startPushWithDelegate:self];

终止推送服务

终止推送服务以后,将无法通过推送服务向设备推送消息,如果再次需要接收服务的消息推送,则必须需要再次调用 startPushWithDelegate: 方法重启推送服务。

接口定义

- (void)stopPushNotification;

调用示例

[[TMFPush defaultManager] stopPushNotification];

自定义通知栏消息行为

创建消息支持的行为

在通知消息中创建一个可以点击的事件行为。

接口定义

+ (nullable id)actionWithIdentifier:(nonnull NSString *)identifier title:(nonnull NSString *)title options:(TMFNotificationActionOptions)options;

参数说明

参数 类型 描述 必选
identifier NSString 行为唯一标识 N
title NSString 行为名称 Y
options TMFNotificationActionOptions 行为支持的选项 Y

其中,TMFNotificationActionOptions 的定义如下:

/**
 @brief 点击行为对象的属性配置

 - TMFNotificationActionOptionNone: 无
 - TMFNotificationActionOptionAuthenticationRequired: 执行前需要解锁验证
 - TMFNotificationActionOptionDestructive: 显示高亮(红色)
 - TMFNotificationActionOptionForeground: 拉起应用到前台
 */
typedef NS_OPTIONS(NSUInteger, TMFNotificationActionOptions) {
    TMFNotificationActionOptionNone = (0),
    TMFNotificationActionOptionAuthenticationRequired = (1 << 0),
    TMFNotificationActionOptionDestructive = (1 << 1),
    TMFNotificationActionOptionForeground = (1 << 2)
};

返回值

类型 描述
TMFNotificationAction 在通知消息中创建一个可以点击的事件行为

调用示例

TMFNotificationAction *action1 = [TMFNotificationAction actionWithIdentifier:@"tmfaction001" title:@"tmfAction1" options:TMFNotificationActionOptionNone];

push_icon注意:通知栏带有点击事件的特性,只有在 iOS8.0 + 以上支持,iOS 7.x or earlier的版本,此方法返回空。

创建分类对象

创建分类对象,用以管理通知栏的 Action 对象。

接口定义

+ (nullable id)categoryWithIdentifier:(nonnull NSString *)identifier actions:(nullable NSArray<TMFNotificationAction *> *)actions intentIdentifiers:(nullable NSArray<NSString *> *)intentIdentifiers options:(TMFNotificationCategoryOptions)options;

参数说明

参数 类型 描述 必选
identifier NSString 分类对象的标识 Y
actions SArray 当前分类拥有的行为对象组 N
intentIdentifiers NSArray 用以表明可以通过 Siri 识别的标识 N
options TMFNotificationCategoryOptions 分类的特性 Y

其中,TMFNotificationCategoryOptions的定义如下:

/**
 @brief 分类对象的属性配置

 - TMFNotificationCategoryOptionNone: 无
 - TMFNotificationCategoryOptionCustomDismissAction: 发送消失事件给UNUserNotificationCenter(iOS 10 or later)对象
 - TMFNotificationCategoryOptionAllowInCarPlay: 允许CarPlay展示此类型的消息
 */
typedef NS_OPTIONS(NSUInteger, TMFNotificationCategoryOptions) {
    TMFNotificationCategoryOptionNone = (0),
    TMFNotificationCategoryOptionCustomDismissAction = (1 << 0),
    TMFNotificationCategoryOptionAllowInCarPlay = (1 << 1)
};

TMFNotificationAction 的定义见 创建消息支持的行为

返回值

类型 描述
TMFNotificationCategory 创建用以管理通知栏 Action 的分类对象

push_icon注意:通知栏带有点击事件的特性,只有在iOS8+以上支持,iOS 8 or earlier的版本,此方法返回空。

调用示例

TMFNotificationCategory *category = [TMFNotificationCategory categoryWithIdentifier:@"tmfCategory" actions:@[action1, action2] intentIdentifiers:@[] options:TMFNotificationCategoryOptionNone];

创建配置类

管理推送消息通知栏的样式和特性。

接口定义

+ (nullable instancetype)configureNotificationWithCategories:(nullable NSSet<TMFNotificationCategory *> *)categories types:(TMFUserNotificationTypes)types;

参数说明

参数 类型 描述 必选
categories NSSet 通知栏中支持的分类集合 N
types TMFUserNotificationTypes 注册通知的样式 Y

其中,TMFUserNotificationTypes 的定义如下:

/**
 @brief 注册通知支持的类型

 - TMFUserNotificationTypeNone: 无
 - TMFUserNotificationTypeBadge: 支持应用角标
 - TMFUserNotificationTypeSound: 支持铃声
 - TMFUserNotificationTypeAlert: 支持弹框
 - TMFUserNotificationTypeCarPlay: 支持CarPlay,iOS 10.0+
 - TMFUserNotificationTypeCriticalAlert: 支持紧急提醒播放声音, iOS 12.0+
 - TMFUserNotificationTypeProvidesAppNotificationSettings: 让系统在应用内通知设置中显示按钮, iOS 12.0+
 - TMFUserNotificationTypeProvisional: 能够将非中断通知临时发布到 Notification Center, iOS 12.0+
 - TMFUserNotificationTypeNewsstandContentAvailability: 支持 Newsstand, iOS 3.0–8.0
 */
typedef NS_OPTIONS(NSUInteger, TMFUserNotificationTypes) {
    TMFUserNotificationTypeNone = (0),
    TMFUserNotificationTypeBadge = (1 << 0),
    TMFUserNotificationTypeSound = (1 << 1),
    TMFUserNotificationTypeAlert = (1 << 2),
    TMFUserNotificationTypeCarPlay = (1 << 3),
    TMFUserNotificationTypeCriticalAlert = (1 << 4),
    TMFUserNotificationTypeProvidesAppNotificationSettings = (1 << 5),
    TMFUserNotificationTypeProvisional = (1 << 6),
    TMFUserNotificationTypeNewsstandContentAvailability = (1 << 3)
};

TMFNotificationCategory 的定义见 创建分类对象

调用示例

TMFNotificationConfigure *configure = [TMFNotificationConfigure configureNotificationWithCategories:[NSSet setWithObject:category] types:TMFUserNotificationTypeAlert|TMFUserNotificationTypeBadge|TMFUserNotificationTypeSound];

管理设备 Token

查询设备 Token

查询当前应用从 APNs 获取的 Token 字符串。

接口定义

@property (copy, nonatomic, nullable, readonly) NSString *deviceTokenString;

调用示例

NSString *token = [[TMFPushTokenManager defaultTokenManager] deviceTokenString];

查询注册结果

SDK 的启动方法自动注册设备从 APNs 获取的 Token 到TMF推送服务器,注册结果会在 TMFPushDelegate 的回调方法返回。

接口定义

- (void)tmfPushDidRegisterDeviceToken:(NSString *)deviceToken error:(NSError *)error;

参数说明

参数 类型 描述 必选
deviceToken NSString 回调从 APNs 获取到的 Token Y
error NSError 注册通知的样式 N

push_icon注意:此回调方法在注册成功之后调用,当前的 Token 已经注册过之后,SDK 将缓存注册信息,此方法将不会再调用。

调用示例

- (void)tmfPushDidRegisterDeviceToken:(NSString *)deviceToken error:(NSError *)error {
    NSLog(@"[TMFPush] did register deviceToken: %@, error: %@", deviceToken, error);
}

收到推送的回调

SDK 收到推送结果会在 TMFPushDelegate 的回调方法返回。

接口定义

- (void)tmfPushDidReceiveRemoteNotification:(id)notification withCompletionHandler:(nullable void (^)(UIBackgroundFetchResult))completionHandler;

参数说明

参数 类型 描述 必选
notification NSDictionary 当前推送消息 Y
  • notification:当前推送消息。
  • completionHandler:处理结果。

completionHandler 回调

参数 类型 描述 必选
result UIBackgroundFetchResult 收到通知后,告知后台操作结束 Y

其中,UIBackgroundFetchResult 的系统定义如下:

typedef NS_ENUM(NSUInteger, UIBackgroundFetchResult) {
    UIBackgroundFetchResultNewData,
    UIBackgroundFetchResultNoData,
    UIBackgroundFetchResultFailed
} NS_ENUM_AVAILABLE_IOS(7_0);

操作类型定义如下:

  • UIBackgroundFetchResultNewData 成功拉取到数据。
  • UIBackgroundFetchResultNoData 没有新数据。
  • UIBackgroundFetchResultFailed 拉取数据失败或者超时。

调用示例

- (void)tmfPushDidReceiveRemoteNotification:(nonnull id)notification withCompletionHandler:(nullable void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"[TMFPush] did receive remote notification: %@", notification);
    if ([notification isKindOfClass:[NSDictionary class]]) {
        // NSDictionary
        NSDictionary *data = notification;
        for (NSString *key in data) {
            id value = data[key];
            NSLog(@"[TMFPush] key: %@, value: %@", key, value);
        }

        completionHandler(UIBackgroundFetchResultNewData);
    }
}

收到推送的回调-iOS 10

SDK 收到推送结果会在 TMFPushDelegate (以下)的回调方法返回。

接口定义

- (void)tmfPushUserNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(nullable UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0));

参数说明

参数 类型 描述 必选
center UNUserNotificationCenter [UNUserNotificationCenter currentNotificationCenter] Y
notification UNNotification 当前推送消息 N

completionHandler 回调

参数 类型 描述 必选
options UNNotificationPresentationOptions 收到通知后,告知后台处理结束 Y

其中,UNNotificationPresentationOptions 的系统定义如下:

typedef NS_OPTIONS(NSUInteger, UNNotificationPresentationOptions) {
    UNNotificationPresentationOptionBadge   = (1 << 0),
    UNNotificationPresentationOptionSound   = (1 << 1),
    UNNotificationPresentationOptionAlert   = (1 << 2),
} __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __OSX_AVAILABLE(10.14);

提醒类型如下:

  • UNNotificationPresentationOptionBadge 角标提醒。
  • UNNotificationPresentationOptionSound 声音提醒。
  • UNNotificationPresentationOptionAlert 弹窗提醒。

可以通过或操作同时设置多种类型。

调用示例

- (void)tmfPushUserNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(nullable UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0)) {
    NSLog(@"[TMFPush] did receive remote notification(iOS10+): %@", notification);
    UNNotification *unNotification = notification;
    UNNotificationContent *content = unNotification.request.content;
    NSLog(@"[TMFPush] title: %@, body: %@", content.title, content.body);
    NSLog(@"[TMFPush] userInfo: %@", content.userInfo);

    completionHandler(UNNotificationPresentationOptionBadge
                     |UNNotificationPresentationOptionSound);
}

收到推送点击的回调-iOS 10

SDK 收到推送提示点击结果会在 TMFPushDelegate (以下)的回调方法返回。

接口定义

- (void)tmfPushUserNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(nullable UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0));

参数说明

参数 类型 描述 必选
center UNUserNotificationCenter [UNUserNotificationCenter currentNotificationCenter] Y
response UNNotificationResponse 用户对通知消息的响应对象 N

completionHandler 回调

参数 类型 描述 必选
void - - Y

推送弹窗点击处理结束时,需要调用 completionHandler() 结束当前行为。

调用示例

- (void)tmfPushUserNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(nullable UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)) {

    NSDictionary *userInfo = response.notification.request.content.userInfo;
    NSLog(@"[TMFPush] did handle action for remote notification, userInfo: %@", userInfo);

    completionHandler();
}

查询设备通知权限

查询设备通知权限是否被用户允许。

接口定义

- (void)deviceNotificationIsAllowed:(nonnull void (^)(BOOL isAllowed))handler;

completionHandler 回调

参数 类型 描述 必选
isAllowed BOOL 设备是否具备推送通知权限 Y

调用示例

[[TMFPush defaultManager] deviceNotificationIsAllowed:^(BOOL isAllowed) {
        <#code#>
}];

控制台日志输出

TMFPush 使用 TMFBaseCore 中定义的日志输出接口和日志级别定义,下面是配置组件日志的示例:

// AppDelegate.m
#if DEBUG
[TMFPush setLogLevels:TMFBaseCoreLogLevelAll];
#elif
[TMFPush setLogLevels:TMFBaseCoreLogLevelNone];
#endif

下面是只输出警告+错误的日志配置示例:

[TMFPush setLogLevels:(TMFBaseCoreLogLevelWarn | TMFBaseCoreLogLevelError)];

查询 SDK 版本

查询当前 SDK 的版本。

接口定义

- (nonnull NSString *)sdkVersion;

调用示例

[[TMFPush defaultManager] sdkVersion];
Copyright © 2013-2023 Tencent Cloud. all right reserved,powered by GitbookUpdate Time 2023-08-31 14:46:07

results matching ""

    No results matching ""