自定义点击跳转

最近更新时间:2024-09-12 14:36:51

我的收藏

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

如果您需要自定义解析收到的远程推送,您需要在 AppDelegate.m 文件中实现 TIMPushDelegate 协议并实现 - onRemoteNotificationReceived 方法。
实现示例代码如下:

@interface AppDelegate ()<TIMPushDelegate>

#pragma mark - TIMPush
...
- (BOOL)onRemoteNotificationReceived:(NSString *)notice {
//- 如果返回 YES, TIMPush 将不在执行内置的 TUIKit 离线推送解析逻辑,完全交由您自行处理;
//NSString *ext = notice;
//OfflinePushExtInfo *info = [OfflinePushExtInfo createWithExtString:ext];
//return YES;
//- 如果返回 NO,TIMPush 将继续执行内置的 TUIKit 离线推送解析逻辑,继续回调 - navigateToBuiltInChatViewController:groupID: 方法。
return NO;
}

@end

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);