有奖捉虫:行业应用 & 管理与支持文档专题 HOT
如果您想尽可能简单地接入 TIMPush 组件,您需要使用 TUILogin 的 login/logout 接口进行 IM 账号的登入/登出操作,TIMPush 组件能自动感知登入/登出事件。

步骤1:集成 TIMPush 组件

1. TIMPush 组件支持 cocoapods 集成,您需要在 Podfile 中添加组件依赖。
target 'YourAppName' do
# Uncommment the next line if you're using Swift or would like to use dynamic frameworks
use_frameworks!
use_modular_headers!
# Pods for Example
pod 'TIMPush', '7.9.5668'
end
2. 执行以下命令,安装 TIMPush 组件。
pod install # 如果无法安装 TUIKit 最新版本,执行以下命令更新本地的 CocoaPods 仓库列表。 pod repo update

步骤2:配置推送参数

1. 当您上传证书到 IM 控制台后,IM 控制台会为您分配一个证书 ID,见下图:



2. 您需要在 AppDelegate 中,实现 - offlinePushCertificateID 协议方法返回证书 ID 即可。
#pragma mark - TIMPush

- (int)offlinePushCertificateID {
return kAPNSBusiId;
}

- (NSString *)applicationGroupID {
return kTIMPushAppGorupKey;
}

- (void)navigateToBuiltInChatViewController:(NSString *)userID groupID:(NSString *)groupID {
// custom navigate
}
至此,您已经完成了基本推送功能的接入。
注意:
1. 当您登录后,在控制台上看到 APNs configuration success 日志打印时,即表示已成功接入。
2. 如果您的 App 已经获取到了推送权限,此时退入后台或者杀死进程,即可收到远程推送通知。
3. 如果您未接入 TUICore 组件,不需要使用 TUILogin 的登录/登出,但依然想实现离线推送,您只需:
调用 [TIMPush disableAutoRegisterPush]。
在您的 App/IM 登录完成后,主动调用 registerPush 方法注册推送。
退出登录时,主动调用 unRegisterPush 方法 反注册推送。

步骤3:发消息时设置离线推送参数(含 UI 集成时 TUIChat 已默认添加,可跳过此步骤)

调用 sendMessage 发送消息时,您可以通过 V2TIMOfflinePushInfo 设置离线推送参数。调用 V2TIMOfflinePushInfo 的ext 设置自定义 ext 数据,当用户收到离线推送启动 App 的时候,可以在单击通知跳转的回调中获取到 ext 字段,然后根据 ext 字段内容跳转到指定的 UI 界面。可以参见 TUIMessageBaseDataProvider 的 sendMessage: 方法:
V2TIMOfflinePushInfo *pushInfo = [[V2TIMOfflinePushInfo alloc] init];
pushInfo.title = @"推送标题";
pushInfo.desc = @"推送内容";
BOOL isGroup = groupID.length > 0;
NSString *senderId = isGroup ? (groupID) : ([TUILogin getUserID]);
NSString *nickName = isGroup ? (conversationData.title) : ([TUILogin getNickName] ?: [TUILogin getUserID]);

NSDictionary *ext = @{
@"entity" : @{
@"action" : @1,
@"content" : @"推送内容",
@"sender" : senderId,
@"nickname" : nickName,
@"faceUrl" : [TUILogin getFaceUrl] ?: @"",
@"chatType" : isGroup ? @(V2TIM_GROUP) : @(V2TIM_C2C),
@"version": @(kOfflinePushVersion),
}
};
NSData *data = [NSJSONSerialization dataWithJSONObject:ext options:NSJSONWritingPrettyPrinted error:nil];
pushInfo.ext = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//以下是兼容安卓的字段,需要填写
pushInfo.AndroidOPPOChannelID = @"tuikit";
pushInfo.AndroidSound = TUIConfig.defaultConfig.enableCustomRing ? @"private_ring" : nil;
pushInfo.AndroidHuaWeiCategory = @"IM";
pushInfo.AndroidVIVOCategory = @"IM";

步骤4: 点击离线推送后自定义跳转

1. 如果您需要自定义解析收到的远程推送,您需要在 AppDelegate.m 文件中实现 - onRemoteNotificationReceived 方法。
#pragma mark - TIMPush
- (BOOL)onRemoteNotificationReceived:(NSString *)notice {
//- 如果返回 YES, TIMPush 将不在执行内置的 TUIKit 离线推送解析逻辑,完全交由您自行处理;
//- 如果返回 NO,TIMPush 将继续执行内置的 TUIKit 离线推送解析逻辑,继续回调 - navigateToBuiltInChatViewController:groupID: 方法。
return NO;
}
2. 如果您想使用内置的 TUIChat 推送解析逻辑,并跳转到 TUIChat 的聊天页面,您可以实现 - navigateToBuiltInChatViewController 方法。
#pragma mark - TIMPush

- (void)navigateToBuiltInChatViewController:(NSString *)userID groupID:(NSString *)groupID {

UITabBarController *tab = [self getMainController];

if (![tab isKindOfClass: UITabBarController.class]) {
// 正在登录中
return;
}
if (tab.selectedIndex != 0) {
[tab setSelectedIndex:0];
}
self.window.rootViewController = tab;
UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
if (![nav isKindOfClass:UINavigationController.class]) {
return;
}
UIViewController *vc = nav.viewControllers.firstObject;
if (![vc isKindOfClass:NSClassFromString(@"ConversationController")]
&& ![vc isKindOfClass:NSClassFromString(@"ConversationController_Minimalist")]) {
return;
}
if ([vc respondsToSelector:NSSelectorFromString(@"pushToChatViewController:userID:")]) {
[vc performSelector:NSSelectorFromString(@"pushToChatViewController:userID:") withObject:groupID withObject:userID];
}
}

步骤5: 统计推送抵达率

1. 如果您需要统计推送的抵达和点击数据,您需要在 AppDelegate.m 文件中实现 - applicationGroupID 方法,返回 App Group ID。
2. 在 Notification Service Extension 的 -didReceiveNotificationRequest:withContentHandler: 方法中调用推送抵达率统计函数:
@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
//appGroup 标识当前主 APP 和 Extension 之间共享的 APP Group,需要在主 APP 的 Capability 中配置 App Groups 能力。
//格式为group + [主bundleID]+ key
//如group.com.tencent.im.pushkey
NSString * appGroupID = kTIMPushAppGorupKey;
__weak typeof(self) weakSelf = self;
[TIMPush onReceiveNotificationRequest:request inAppGroupID:appGroupID callback:^(UNNotificationContent *content) {
weakSelf.bestAttemptContent = [content mutableCopy];
// Modify the notification content here...
// self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
weakSelf.contentHandler(weakSelf.bestAttemptContent);
}];
}
注意:
1. 上报推送触达数据,需要开启 mutable-content 开关来支持 iOS 10 的 Extension 功能。



2. 数据详情可在推送数据页面查看,推送数据页面仅限 购买推送插件 后使用。

关于合规

TIMPush 在您未主动调用 registerPush 之前,不会有其他任何操作,符合相关规定。
如果您使用了 TUILogin 的登录登出,TIMPush 会在内部自动调用 registerPush 或 unRegisterPush。
恭喜您已经完成了推送插件的接入,需要提醒您:消息推送插件试用或购买到期后,将自动停止提供推送服务(包括普通消息离线推送、全员推送等服务)。为避免影响您业务正常使用,请提前购买/续费


关于全员/标签推送

全员/标签推送支持发送特定内容,还可根据标签、属性,针对特定用户群体发送个性化内容,例如会员活动、区域通知等,助力拉新、转化、促活等各个阶段运营工作的有效进行,同时支持推送送达报表,自助推送故障排查工具,具体效果请参见 效果展示
更多详细内容建议查阅全员/标签推送