快速跑通

最近更新时间:2024-09-24 18:13:31

我的收藏

步骤1:创建应用

进入 控制台 ,单击创建应用,填写应用名称,选择数据中心,单击确定,完成应用创建。


步骤2:开通推送服务 Push

进入 推送服务 Push,单击立即购买免费试用 。(每个应用可免费试用一次,有效期7天)

注意:
推送服务 Push 试用或购买到期后,将自动停止提供推送服务(包括全员/标签/指定 UserID 推送、IM 消息离线推送等服务)。为避免影响您业务正常使用,请提前购买/续费

步骤3:集成 TIMPush 并注册推送

说明:
注册接口需要的参数 sdkAppId 和客户端密钥 appKey 获取路径是:IM 控制台 - 推送服务 Push - 接入设置页面,具体如下截图:



Android
iOS
Flutter
uni-app
React Native
1. 集成 TIMPush
implementation 'com.tencent.timpush:timpush:8.1.6906'
implementation 'com.tencent.liteav.tuikit:tuicore:latest.release'
2. 注册推送(成功后就可以收到在线推送通知了)
int sdkAppId = 0; //您的 sdkAppId
String appKey = ""; //客户端密钥
TIMPushManager.getInstance().registerPush(context, sdkAppId, appKey, new TIMPushCallback() {
@Override
public void onSuccess(Object data) {
}
@Override
public void onError(int errCode, String errMsg, Object data) {
}
});
说明:
1. 注册离线推送服务成功后,通过该接口 getRegistrationID 可获取推送唯一 ID 标识, 即 RegistrationID,然后可以根据 RegistrationID 来向指定设备推送消息;
2. RegistrationID 设备的推送唯一标识 ID,默认注册推送服务成功后会自动生成,也支持用户自定义设置,提供用户根据 RegistrationID 来向指定设备推送消息功能,卸载重装会改变, 需要在注册推送服务之前调用。
String registrationID = ""; //推送 ID,需要在注册推送服务之前调用
TIMPushManager.getInstance().setRegistrationID(registrationID, new TIMPushCallback() {
@Override
public void onSuccess(Object data) {
}
@Override
public void onError(int errCode, String errMsg, Object data) {
}
});
3. 实现点击通知栏回调
当收到推送时候,点击通知栏的点击事件,组件会以广播形式通知应用,应用在回调中配置 App 的跳转页面即可。注册回调时机建议放在应用 Application 的 oncreate() 函数中。
// 动态注册广播
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);
// 获取 ext 自定义跳转

} else {
Log.e(TAG, "onReceive ext is null");
}
}
}
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 'TXIMSDK_Plus_iOS_XCFramework'
pod 'TIMPush', '8.1.6906'
end
执行以下命令,安装 TIMPush 组件。
pod install # 如果无法安装 TUIKit 最新版本,执行以下命令更新本地的 CocoaPods 仓库列表。 pod repo update
2. 注册推送(成功后就可以收到在线推送通知了)
const int sdkAppId = 0; //您的 sdkAppId
static const NSString *appKey = @""; //客户端密钥

[TIMPushManager registerPush:sdkAppId appKey:appKey succ:^(NSData * _Nonnull deviceToken) {

} fail:^(int code, NSString * _Nonnull desc) {

}];
说明:
1. 注册离线推送服务成功后,通过该接口 getRegistrationID 可获取推送唯一 ID 标识, 即 RegistrationID,然后可以根据 RegistrationID 来向指定设备推送消息;
2. RegistrationID 设备的推送唯一标识 ID,默认注册推送服务成功后会自动生成,也支持用户自定义设置,提供用户根据 RegistrationID 来向指定设备推送消息功能,卸载重装会改变, 需要在注册推送服务之前调用。
NSString registrationID = @""; //推送 ID,需要在注册推送服务之前调用
[TIMPushManager setRegistrationID:registrationID callback:^{
}];
3. 实现点击通知栏回调
您需要在 AppDelegate.m 文件中实现 - onRemoteNotificationReceived 方法
#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;
}
1. 集成 TIMPush
本插件在 pub.dev 的包名为: tencent_cloud_chat_push , 您可以将其引入 pubspec.yaml 依赖目录中, 也可以执行下列命令自动安装。
flutter pub add tencent_cloud_chat_push
2. 注册推送(成功后就可以收到在线推送通知了)
您可定义一个函数来接收该回调,并据此跳转至对应会话页面或您的业务页面。
示例如下:
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 字段, 自己写解析方式, 跳转至对应页面. } }

TencentCloudChatPush().registerPush(onNotificationClicked: _onNotificationClicked, sdkAppId: 您的sdkAppId, appKey: "客户端密钥");
说明:
1. 注册离线推送服务成功后,通过该接口 getRegistrationID 可获取推送唯一 ID 标识, 即 RegistrationID,然后可以根据 RegistrationID 来向指定设备推送消息;
2. RegistrationID 设备的推送唯一标识 ID,默认注册推送服务成功后会自动生成,也支持用户自定义设置,提供用户根据 RegistrationID 来向指定设备推送消息功能,卸载重装会改变, 需要在注册推送服务之前调用。
String registrationID = ""; //推送 ID,需要在注册推送服务之前调用
TencentCloudChatPush().setRegistrationID(registrationID: registrationID);
3. 实现点击通知栏回调
Android
iOS
1. Application 类继承 TencentCloudChatPushApplication
package 替换成您自己的包名(一般 Android Studio 会自动生成) import com.tencent.chat.flutter.push.tencent_cloud_chat_push.application.TencentCloudChatPushApplication; public class MyApplication extends TencentCloudChatPushApplication { @Override public void onCreate() { super.onCreate(); } }
说明:
如果您已经创建了自己的 Application 为了其他用途,请直接 extends TencentCloudChatPushApplication 并保证 onCreate() 函数中,调用了 super.onCreate(); 即可。
1. AppDelegate 类继承 TIMPushDelegate
import UIKit
import Flutter

// Add these two import lines
import TIMPush
import tencent_cloud_chat_push

// Add `, TIMPushDelegate` to the following line
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, TIMPushDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

// Add this function
func offlinePushCertificateID() -> Int32 {
return TencentCloudChatPushFlutterModal.shared.offlinePushCertificateID();
}

// Add this function
func applicationGroupID() -> String {
return TencentCloudChatPushFlutterModal.shared.applicationGroupID()
}
// Add this function
func onRemoteNotificationReceived(_ notice: String?) -> Bool {
TencentCloudChatPushPlugin.shared.tryNotifyDartOnNotificationClickEvent(notice)
return true
}
}
1. uni-app 腾讯云推送服务(Push)插件导入HbuilderX 中的工程。如图所示:



2. 在 App.vue 中引入并注册腾讯云推送服务(Push)(成功后就可以收到在线推送通知了)
说明:
registerPush 注册推送服务成功后,通过 getRegistrationID 可获取推送 ID 标识, 即 RegistrationID,您可以根据 RegistrationID 向指定设备推送消息。
// 集成 TencentCloud-Push
import * as Push from '@/uni_modules/TencentCloud-Push';
const SDKAppID = 0; // 您的 SDKAppID
const appKey = ''; // 客户端密钥
Push.registerPush(SDKAppID, appKey, (data) => {
console.log('registerPush ok', data);
Push.getRegistrationID((registrationID) => {
console.log('getRegistrationID ok', registrationID);
});
}, (errCode, errMsg) => {
console.error('registerPush failed', errCode, errMsg);
}
);
3. 实现点击通知栏回调
当收到推送时,您可在 App.vue 的 onShow 中调用 getNotificationExtInfo 接口来获取通知栏推送的内容。
export default {
onLaunch: function() {
},
onShow: function() {
console.log('App Show')
Push.getNotificationExtInfo((extInfo) => {
console.log('getNotificationExtInfo ok', extInfo);
})
},
onHide: function() {
console.log('App Hide')
}
}
4. 使用云端证书,生成自定义基座
单击 HBuilderX 的运行 > 运行到手机或模拟器 > 制作自定义调试基座,使用云端证书制作 Android 或 iOS 自定义调试基座。如图所示:


1. 创建一个 React Native 项目(已有项目可忽略此步骤)
npx react-native@latest init MyReactNativeApp
2. 集成 @tencentcloud/react-native-push
npm install @tencentcloud/react-native-push --save
3. 注册推送
复制下面的代码到 App.tsx,并将 SDKAppID 和 appKey 替换为您的应用的信息。

import Push from '@tencentcloud/react-native-push';

const SDKAppID = 0; // 您的 SDKAppID
const appKey = ''; // 客户端密钥

if (Push) {
Push.registerPush(SDKAppID, appKey, (data) => {
console.log('registerPush ok', data);
Push.getRegistrationID((registrationID) => {
console.log('getRegistrationID ok', registrationID);
});
}, (errCode, errMsg) => {
console.error('registerPush failed', errCode, errMsg);
}
);
}
4. 配置 Native Modules 和相关依赖
For Android:
1)使用 Android Studio 打开 MyReactNativeApp 目录下的 android 项目。
2)编辑 settings.gradle,在文件末尾添加以下内容并保存。
include ':tencentcloudpush'
project(':tencentcloudpush').projectDir = new File('../node_modules/@tencentcloud/react-native-push/android')
编辑 app/build.gradle 文件,更新 dependencies 配置并保存。
dependencies {
// The version of react-native is set by the React Native Gradle Plugin
implementation("com.facebook.react:react-android")
// Required
implementation project(':tencentcloudpush')
implementation 'com.tencent.timpush:timpush:8.1.6906'
implementation 'com.tencent.liteav.tuikit:tuicore:latest.release'

if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android")
} else {
implementation jscFlavor
}
}
3)以上操作都完成后,选择 File > Sync Project with Gradle Files。
4)如果您的项目入口文件是 MainApplication.kt,请按如下方式修改:
import com.tencent.qcloud.rntimpush.TencentCloudPushApplication
import com.tencent.qcloud.rntimpush.TencentCloudPushPackage

// Replace Application with TencentCloudPushApplication
class MainApplication : TencentCloudPushApplication(), ReactApplication {
...
// add TencentCloudPushPackage to the list of packages returned in ReactNativeHost's getPackages() method
override fun getPackages(): List<ReactPackage> =
PackageList(this).packages.apply {
// Packages that cannot be autolinked yet can be added manually here, for example:
// add(MyReactNativePackage())
add(TencentCloudPushPackage())
}
}
5)如果您的项目入口文件是 MainApplication.java,请按如下方式修改:
import com.tencent.qcloud.rntimpush.TencentCloudPushApplication;
import com.tencent.qcloud.rntimpush.TencentCloudPushPackage;

// Replace Application with TencentCloudPushApplication
public class MainApplication extends TencentCloudPushApplication implements ReactApplication {
...
// add TencentCloudPushPackage to the list of packages returned in ReactNativeHost's getPackages() method
@Override
protected List<ReactPackage> getPackages() {
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new TencentCloudPushPackage());
return packages;
}
...
}
For iOS:
1)使用 XCode 打开 MyReactNativeApp/ios/MyReactNativeApp.xcworkspace,右键点击 Libraries > Add Files to "MyReactNativeApp",将 node_modules/@tencentcloud/react-native-push/ios 目录下的 TencentCloudPush.h 和 TencentCloudPush.mm 添加到工程。
2)编辑 MyReactNativeApp/ios/Podfile,添加依赖。
target 'MyReactNativeApp' 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 'TXIMSDK_Plus_iOS_XCFramework'
pod 'TIMPush', '8.1.6907'
end
3)安装 TIMPush 组件。
pod install
# 如果无法安装最新版本,执行以下命令更新本地的 CocoaPods 仓库列表
pod repo update
4)在App中添加推送权限,请在 Xcode 项目中启用推送通知功能。打开 Xcode 项目,在 Project > Target > Capabilities 页面中点击红框中的加号按钮,然后选择并添加 Push Notifications 。

添加后的结果如图中红框所示。

5. 在真机上运行(测试前请务必打开手机通知权限,允许应用通知。)
从项目根目录开始,在命令提示符中运行以下命令,在设备上安装并启动您的应用程序:
For Android:
npm run android
For iOS:
npm run

步骤4:发送全员/标签/指定 RegistrationID 推送

向全员或者被打标签的用户发送,详情请参见 全员/标签推送
批量向指定 RegistrationID 发送,详情请参见 单发推送