tencent cloud

文档反馈

自定义点击跳转

最后更新时间:2024-06-13 10:39:26

    Flutter

    步骤1: 厂商配置

    参见 厂商配置 > Flutter,完成厂商配置。请注意,点击后续动作,请使用默认配置。
    
    
    

    步骤2: 客户端代码配置

    参见 客户端代码配置,完成配置。

    步骤3: 处理消息点击回调, 并解析参数

    请定义一个函数, 用于接受推送消息点击回调事件.
    该函数请定义成 {required String ext, String? userID, String? groupID} 的入参形式。
    其中, ext字段, 为该消息所携带的完整 ext 信息, 由发送方指定, 如果未指定, 则有默认值. 您可根据解析该字段, 跳转至对应页面。
    userID 和 groupID 字段,为本插件,自动尝试解析 ext Json String, 获取里面携带的单聊对方 userID 和 群聊 groupID 信息。如果您未自定义 ext 字段,ext 字段由 SDK 或 UIKit 默认指定,则可使用此处的默认解析。如果尝试解析失败, 则为 null 空。
    您可定义一个函数来接收该回调,并据此跳转至对应会话页面或您的业务页面。
    示例如下:
    void _onNotificationClicked({required String ext, String? userID, String? groupID}) { print("_onNotificationClicked: $ext, userID: $userID, groupID: $groupID"); if (userID != null || groupID != null) { // 根据 userID 或 groupID 跳转至对应 Message 页面. } else { // 根据 ext 字段, 自己写解析方式, 跳转至对应页面. } }

    步骤4: 挂载监听回调

    请在 IM 登录完成后, 且在其他插件 (如 CallKit) 使用前, 立即注册推送插件。
    调用 TencentCloudChatPush().registerPush 方法, 需传入上一步定义的点击回调函数。
    此外,您还可选传入 apnsCertificateID iOS 推送证书 ID 及 androidPushOEMConfig Android 推送厂商配置。此二项配置已在前序步骤指定,若无修改必要,可不再传入。
    TencentCloudChatPush().registerPush(onNotificationClicked: _onNotificationClicked);
    说明:
    如果您的应用需要使用推送插件进行业务消息通知,并且在启动后不会立即启动并登录 IM 模块,或者在登录 IM 模块之前需要通过获取消息点击回调来处理业务导航,建议您尽早调用 TencentCloudChatPush().registerOnNotificationClickedEvent 方法,手动挂载消息单击回调,以便及时获取消息参数。
    在这种场景下,您可以在调用 TencentCloudChatPush().registerPush 之前执行此函数,并尽可能提前将其放置在代码中。
    TencentCloudChatPush().registerOnNotificationClickedEvent(onNotificationClicked: _onNotificationClicked);

    Android

    收到离线推送后,通知栏会显示推送信息如图所示,单击通知栏可以自定义打开应用的界面。
    
    
    
    1. 控制台配置点击后续动作按如下配置,选择打开应用内指定界面,插件用户会默认填写跳转参数。
    
    
    
    2. 注册和回调处理点击事件,注册时机建议放在应用 Application 的 oncreate() 函数中。组件会以回调或者广播形式通知应用,应用在回调中配置自定义的跳转页面即可。
    回调方式如下:
    TUICore.registerEvent(TUIConstants.TIMPush.EVENT_NOTIFY, TUIConstants.TIMPush.EVENT_NOTIFY_NOTIFICATION, new ITUINotification() {
    @Override
    public void onNotifyEvent(String key, String subKey, Map<String, Object> param) {
    Log.d(TAG, "onNotifyEvent key = " + key + "subKey = " + subKey);
    if (TUIConstants.TIMPush.EVENT_NOTIFY.equals(key)) {
    if (TUIConstants.TIMPush.EVENT_NOTIFY_NOTIFICATION.equals(subKey)) {
    if (param != null) {
    String extString = (String)param.get(TUIConstants.TIMPush.NOTIFICATION_EXT_KEY);
    TUIUtils.handleOfflinePush(extString, null);
    }
    }
    }
    }
    });
    广播方式如下:
    // 动态注册广播
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(TUIConstants.TIMPush.NOTIFICATION_BROADCAST_ACTION);
    LocalBroadcastManager.getInstance(context).registerReceiver(localReceiver, intentFilter);
    
    //广播接收者
    public class OfflinePushLocalReceiver extends BroadcastReceiver {
    public static final String TAG = OfflinePushLocalReceiver.class.getSimpleName();
    
    @Override
    public void onReceive(Context context, Intent intent) {
    DemoLog.d(TAG, "BROADCAST_PUSH_RECEIVER intent = " + intent);
    if (intent != null) {
    String ext = intent.getStringExtra(TUIConstants.TIMPush.NOTIFICATION_EXT_KEY);
    TUIUtils.handleOfflinePush(ext, null);
    } else {
    DemoLog.e(TAG, "onReceive ext is null");
    }
    }
    }

    iOS

    请在调用 sendMessage 发送消息的时候设置 V2TIMOfflinePushInfoext 字段,当用户收到离线推送启动 App 的时候,可以在 AppDelegate -> didReceiveRemoteNotification 系统回调获取到 ext 字段,然后根据 ext 字段内容跳转到指定的 UI 界面。
    denny 给 vinson 发送消息的场景为例。
    发送方:denny 需在发送消息时设置推送扩展字段 ext
    // denny在发送消息时设置 offlinePushInfo,并指定 ext 字段
    V2TIMMessage *msg = [[V2TIMManager sharedInstance] createTextMessage:@"文本消息"];
    V2TIMOfflinePushInfo *info = [[V2TIMOfflinePushInfo alloc] init];
    info.ext = @"jump to denny";
    [[V2TIMManager sharedInstance] sendMessage:msg receiver:@"vinson" groupID:nil priority:V2TIM_PRIORITY_DEFAULT
    onlineUserOnly:NO offlinePushInfo:info progress:^(uint32_t progress) {
    } succ:^{
    } fail:^(int code, NSString *msg) {
    }];
    接收方:vinson 的 App 虽然不在线,但可以接收到 APNS 离线推送,当 vinson 点击推送消息时会启动 App:
    // vinson 启动 APP 后会收到以下回调
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    // 解析推送扩展字段 desc
    if ([userInfo[@"ext"] isEqualToString:@"jump to denny"]) {
    //跳转到和 denny 的聊天界面
    }
    }
    
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持