

pod 'TPNS-iOS', '~> 版本' // 如果不指定版本则默认为本地 pod TPNS-iOS 最新版本
Unable to find a specification for 'TPNS-iOS',那么需要执行以下命令,并更新仓库确认版本:
```
pod repo update
pod search TPNS-iOS
pod install //安装 SDK#### 方式二:手动导入1. 进入腾讯移动推送 [控制台](https://console.tencentcloud.com/tpns),单击左侧菜单栏【[SDK 下载](https://console.tencentcloud.com/tpns/sdkdownload)】,进入下载页面,选择需要下载的 SDK 版本,单击操作栏中【下载】即可。2. 打开 demo 目录下的 SDK 文件夹,将 XGPush.h 及 libXG-SDK-Cloud.a 添加到工程,打开 XGPushStatistics 文件夹,获取 XGMTACloud.framework。3. 将 InAppMessage 文件夹导入到工程并在【Build Setting】>【Framework Search Paths】 添加查找路径(若您 SDK 版本低于1.2.8.0,则可以忽略此步骤)。4. 在 Build Phases 下,添加以下 Framework:


Capabilities中开启Time Sensitive Notifications
-ObjC 。

tpns.tencent.com。startXGWithAccessID:accessKey:delegate:方法之前调用头文件中的配置域名接口:tpns.sh.tencent.com。
示例/// @note TPNS SDK1.2.7.1+ [[XGPush defaultManager] configureClusterDomainName:@"tpns.sh.tencent.com"];
tpns.sgp.tencent.com。/// @note TPNS SDK1.2.7.1+[[XGPush defaultManager] configureClusterDomainName:@"tpns.sgp.tencent.com"];
tpns.hk.tencent.com。
示例/// @note TPNS SDK1.2.7.1+[[XGPush defaultManager] configureClusterDomainName:@"tpns.hk.tencent.com"];
tpns.tencent.com。
示例/// @note TPNS SDK1.2.7.1+[[XGPush defaultManager] configureClusterDomainName:@"tpns.tencent.com"];
XGPushDelegate 协议中的方法,开启推送服务。AppDelegate 示例如下:@interface AppDelegate () <XGPushDelegate>@end/**@param AccessID 通过 TPNS 管理台申请的 AccessID@param AccessKey 通过 TPNS 管理台申请的 AccessKey@param delegate 回调对象**/-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{[[XGPush defaultManager] startXGWithAccessID:<your AccessID> accessKey:<your AccessKey> delegate:self];return YES;}
AppDelegate 中,选择实现 XGPushDelegate 协议中的方法:/// 统一接收消息的回调/// @param notification 消息对象(有2种类型NSDictionary和UNNotification具体解析参考示例代码)/// @note 此回调为前台收到通知消息及所有状态下收到静默消息的回调(消息点击需使用统一点击回调)/// 区分消息类型说明:xg字段里的msgtype为1则代表通知消息msgtype为2则代表静默消息- (void)xgPushDidReceiveRemoteNotification:(nonnull id)notification withCompletionHandler:(nullable void (^)(NSUInteger))completionHandler{/// code}/// 统一点击回调/// @param response 如果iOS 10+/macOS 10.14+则为UNNotificationResponse,低于目标版本则为NSDictionary- (void)xgPushDidReceiveNotificationResponse:(nonnull id)response withCompletionHandler:(nonnull void (^)(void))completionHandler {/// code}
//打开 debug 开关[[XGPush defaultManager] setEnableDebug:YES];
XGPushDelegate 协议/**@brief 注册推送服务回调@param deviceToken APNs 生成的 Device Token@param xgToken TPNS 生成的 Token,推送消息时需要使用此值。TPNS 维护此值与 APNs 的 Device Token 的映射关系@param error 错误信息,若 error 为 nil 则注册推送服务成功@note TPNS SDK1.2.6.0+*/- (void)xgPushDidRegisteredDeviceToken:(nullable NSString *)deviceToken xgToken:(nullable NSString *)xgToken error:(nullable NSError *)error;/// 注册推送服务失败回调/// @param error 注册失败错误信息/// @note TPNS SDK1.2.7.1+- (void)xgPushDidFailToRegisterDeviceTokenWithError:(nullable NSError *)error {}
[TPNS] Current device token is 9298da5605c3b242261b57****376e409f826c2caf87aa0e6112f944[TPNS] Current TPNS token is 00c30e0aeddff1270d8****dc594606dc184
- (void)xgPushDidReceiveRemoteNotification:(nonnull id)notification withCompletionHandler:(nullable void (^)(NSUInteger))completionHandler;
if ([notification isKindOfClass:[UNNotification class]]) {completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);}
NSDictionary *tpnsInfo = notificationDic[@"xg"];NSNumber *msgType = tpnsInfo[@"msgtype"];if (msgType.integerValue == 1) {/// 前台收到通知消息} else if (msgType.integerValue == 2) {/// 收到静默消息} else if (msgType.integerValue == 9) {/// 收到本地通知(TPNS本地通知)}
/// 统一点击回调/// @param response 如果 iOS 10+/macOS 10.14+ 则为 UNNotificationResponse,低于目标版本则为 NSDictionary/// @note TPNS SDK1.2.7.1+- (void)xgPushDidReceiveNotificationResponse:(nonnull id)response withCompletionHandler:(nonnull void (^)(void))completionHandler;
xgPushDidReceiveRemoteNotification 会处理消息接收,并自动后续调用 application:didReceiveRemoteNotification:fetchCompletionHandler 方法。然而,该方法也可能被其他 SDK 也进行 hook 调用。application:didReceiveRemoteNotification:fetchCompletionHandler 方法处理其他推送平台的业务,请参照如下指引,避免业务重复:xgPushDidReceiveRemoteNotification 方法进行处理,非 移动推送消息请统一在 application:didReceiveRemoteNotification:fetchCompletionHandler 方法处理xgPushDidReceiveRemoteNotification 和 application:didReceiveRemoteNotification:fetchCompletionHandler 如果都执行,总共只需要调用一次 completionHandler。如果其他 SDK 也调用 completionHandler,确保整体的 completionHandler 只调用一次。这样可以防止由于多次 completionHandler 而引起的 crash。//获取 TPNS 生成的 Token[[XGPushTokenManager defaultTokenManager] xgTokenString];
[[XGPush defaultManager] uploadLogCompletionHandler:^(BOOL result, NSString * _Nullable errorMessage) {NSString *title = result ? NSLocalizedString(@"report_log_info", nil) : NSLocalizedString(@"failed", nil);if (result && errorMessage.length>0) {UIPasteboard *pasteboard = [UIPasteboardgeneralPasteboard];pasteboard.string = errorMessage;}[TPNSCommonMethodshowAlert:title message:errorMessage viewController:selfcompletion:nil];}];
文档反馈