timpush-configs.json from the console. Path: Console > Push > App Push > Push Settings > Manufacturer configuration > Android > FCM > Download certificate.
assets directory. Recommended path: app/src/main/assets/timpush-configs.json, where app is your app module name and can be replaced with the actual name. If the project has no assets directory, create it under src/main/.google() and mavenCentral() resolve Android official components, and the Tencent Cloud Maven repository resolves TIMPush and other Tencent Cloud dependencies.File | DSL type |
settings.gradle、build.gradle | Groovy DSL |
settings.gradle.kts、build.gradle.kts | Kotlin DSL |
distributionUrl in gradle/wrapper/gradle-wrapper.properties at the project root.settings.gradle (Groovy DSL) or settings.gradle.kts (Kotlin DSL), add repositories in both pluginManagement > repositories and dependencyResolutionManagement > repositories.pluginManagement {repositories {gradlePluginPortal()google()mavenCentral()maven { url "https://mirrors.tencent.com/nexus/repository/maven-public/" }}}dependencyResolutionManagement {repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories {google()mavenCentral()maven { url "https://mirrors.tencent.com/nexus/repository/maven-public/" }}}
pluginManagement {repositories {gradlePluginPortal()google()mavenCentral()maven { url = uri("https://mirrors.tencent.com/nexus/repository/maven-public/") }}}dependencyResolutionManagement {repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories {google()mavenCentral()maven { url = uri("https://mirrors.tencent.com/nexus/repository/maven-public/") }}}
buildscript > repositories of the project-level build.gradle. Add Huawei, Honor, and Google FCM plugin classpaths in the corresponding manufacturer tab under Manufacturer channel integration.buildscript {repositories {google()mavenCentral()maven { url "https://mirrors.tencent.com/nexus/repository/maven-public/" }}}
dependencyResolutionManagement > repositories of settings.gradle.dependencyResolutionManagement {repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)repositories {google()mavenCentral()maven { url "https://mirrors.tencent.com/nexus/repository/maven-public/" }}}
buildscript > repositories and allprojects > repositories of the project-level build.gradle. Add Huawei, Honor, and Google FCM plugin classpaths in the corresponding manufacturer tab under Manufacturer channel integration.buildscript {repositories {google()mavenCentral()maven { url "https://mirrors.tencent.com/nexus/repository/maven-public/" }}}allprojects {repositories {google()mavenCentral()maven { url "https://mirrors.tencent.com/nexus/repository/maven-public/" }}}
build.gradle or build.gradle.kts. The base package provides TIMPush registration, listeners, and common capabilities.VERSION means the TIMPush SDK version (for example 8.9.7537). For the latest version, see Release Notes。dependencies {implementation 'com.tencent.timpush:timpush:VERSION'implementation 'com.tencent.liteav.tuikit:tuicore:VERSION'}
dependencies {implementation("com.tencent.timpush:timpush:VERSION")implementation("com.tencent.liteav.tuikit:tuicore:VERSION")}
timpush-configs.json that includes the FCM certificate has been re-downloaded and placed in the app module assets directory;google-services.json from the Firebase console and add it to the app module root (same level as the app module build.gradle / build.gradle.kts):app/google-services.json
buildscript > dependencies > classpath directive. Consequently, the buildscript > repositories block in the project-level build.gradle (or build.gradle.kts) must include the google() repository (and any other required repositories); otherwise, resolution of the plugin classpath may fail.plugins {} block in the project-level build file, the existing configuration can be retained as is, and no migration is required.buildscript {repositories {google()mavenCentral()gradlePluginPortal()maven { url "https://mirrors.tencent.com/nexus/repository/maven-public/" }}dependencies {// Keep this consistent with the Android Gradle Plugin version currently used by the project.classpath 'com.android.tools.build:gradle:<AGP_VERSION>'// If the project uses the Kotlin Android plugin, keep this consistent with the current Kotlin Gradle Plugin version.classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:<KOTLIN_VERSION>'// Google FCM. 4.4.2 requires AGP 7.3.0 or later. For lower AGP versions, use 4.3.15.classpath 'com.google.gms:google-services:4.4.2'}}
buildscript {repositories {google()mavenCentral()gradlePluginPortal()maven { url = uri("https://mirrors.tencent.com/nexus/repository/maven-public/") }}dependencies {// Keep this consistent with the Android Gradle Plugin version currently used by the project.classpath("com.android.tools.build:gradle:<AGP_VERSION>")// If the project uses the Kotlin Android plugin, keep this consistent with the current Kotlin Gradle Plugin version.classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:<KOTLIN_VERSION>")// Google FCM. 4.4.2 requires AGP 7.3.0 or later. For lower AGP versions, use 4.3.15.classpath("com.google.gms:google-services:4.4.2")}}
com.google.gms:google-services:4.4.2 requires AGP 7.3.0 or later. If the project AGP is lower than 7.3.0, use a Google Services Gradle Plugin version compatible with the current AGP, for example 4.3.15.buildscript > dependencies of the project-level build.gradle:classpath 'com.google.gms:google-services:4.3.15'
build.gradle or build.gradle.kts.plugins {// Google FCM。id 'com.google.gms.google-services'}// Or: apply plugin: 'com.google.gms.google-services'
plugins {// Google FCM。id("com.google.gms.google-services")}// Or: apply(plugin = "com.google.gms.google-services")
dependencies of the app module build.gradle or build.gradle.kts, and keep VERSION consistent with the base package:implementation 'com.tencent.timpush:fcm:VERSION'
implementation("com.tencent.timpush:fcm:VERSION")
setAndroidFCMChannelID(String channelID) to set the FCM channel notification channel ID:V2TIMOfflinePushInfo pushInfo = new V2TIMOfflinePushInfo();pushInfo.setAndroidFCMChannelID("your_fcm_channel_id");
registrationID for the device. The server, Console Access Test, and troubleshooting tools can use this identifier to send offline pushes to the device. If the app also integrates Chat and completes login, you can also use userID to send offline pushes to devices that have established a push relationship for that user.appKey affects the registration method and available push identifiers:appKey = Push Key: Register TIMPush standalone push capability. The Push Key is the client key.appKey = null: Reuse the Chat login state to register for push. Call this only after Chat login succeeds.userID and registrationID becomes invalid and must be registered again.appKey of registerPush.registerPush(appKey = Push Key).getRegistrationID and print the registrationID within the success callback of registerPush. This facilitates sending offline push notifications based on the registrationID afterwards.import android.util.Log;import com.tencent.timpush.TIMPushCallback;import com.tencent.timpush.TIMPushManager;// The method should be called after the user has agreed to the privacy policy.private void registerTIMPush() {int sdkAppId = 0; // TODO: Replace with your SDKAppID.String appKey = ""; // TODO: Replace with your Push Key.TIMPushManager.getInstance().registerPush(this, sdkAppId, appKey, new TIMPushCallback<Object>() {@Overridepublic void onSuccess(Object data) {Log.d(TAG, ">>>>> registerPush success, data = " + data);TIMPushManager.getInstance().getRegistrationID(new TIMPushCallback<Object>() {@Overridepublic void onSuccess(Object data) {String registrationID = (String) data;Log.d(TAG, ">>>>> getRegistrationID success, registrationID = " + registrationID);}@Overridepublic void onError(int errCode, String errMsg, Object data) {Log.e(TAG, ">>>>> getRegistrationID failed, errCode = " + errCode+ ", errMsg = " + errMsg);}});}@Overridepublic void onError(int errCode, String errMsg, Object data) {Log.e(TAG, ">>>>> registerPush failed, errCode = " + errCode+ ", errMsg = " + errMsg);}});}
import android.util.Logimport com.tencent.timpush.TIMPushCallbackimport com.tencent.timpush.TIMPushManager// The method should be called after the user has agreed to the privacy policy.private fun registerTIMPush() {val sdkAppId = 0 // TODO: Replace with your SDKAppID.val appKey = "" // TODO: Replace with your Push Key.TIMPushManager.getInstance().registerPush(this, sdkAppId, appKey, object : TIMPushCallback<Any?>() {override fun onSuccess(data: Any?) {Log.d("TIMPush", ">>>>> registerPush success, data = $data")TIMPushManager.getInstance().getRegistrationID(object : TIMPushCallback<Any?>() {override fun onSuccess(data: Any?) {Log.d("TIMPush", ">>>>> getRegistrationID success, registrationID = $data")}override fun onError(errCode: Int, errMsg: String?, data: Any?) {Log.e("TIMPush", ">>>>> getRegistrationID failed, errCode = $errCode, errMsg = $errMsg")}})}override fun onError(errCode: Int, errMsg: String?, data: Any?) {Log.e("TIMPush", ">>>>> registerPush failed, errCode = $errCode, errMsg = $errMsg")}})}
registerPush(appKey = null) in the Chat login success callback. It is recommended to call getRegistrationID and print the registrationID within the success callback of registerPush. This facilitates sending offline push notifications based on the registrationID afterwards.import android.content.Context;import android.util.Log;import com.tencent.imsdk.v2.V2TIMCallback;import com.tencent.imsdk.v2.V2TIMManager;import com.tencent.imsdk.v2.V2TIMSDKConfig;import com.tencent.qcloud.tim.push.TIMPushCallback;import com.tencent.qcloud.tim.push.TIMPushManager;public void loginIMAndRegisterPush(Context context) {int sdkAppId = 0; // TODO: Replace with your SDKAppID.String userID = "<YOUR_USER_ID>";String userSig = "<YOUR_USER_SIG>";boolean initSuccess = V2TIMManager.getInstance().initSDK(context, sdkAppId, new V2TIMSDKConfig());if (!initSuccess) {Log.e("TIMPush", ">>>>> IM SDK init failed");return;}V2TIMManager.getInstance().login(userID, userSig, new V2TIMCallback() {@Overridepublic void onSuccess() {TIMPushManager.getInstance().registerPush(context, sdkAppId, null, new TIMPushCallback<Object>() {@Overridepublic void onSuccess(Object data) {Log.d("TIMPush", ">>>>> registerPush success, data = " + data);TIMPushManager.getInstance().getRegistrationID(new TIMPushCallback<Object>() {@Overridepublic void onSuccess(Object data) {String registrationID = (String) data;Log.d(TAG, ">>>>> getRegistrationID success, registrationID = " + registrationID);}@Overridepublic void onError(int errCode, String errMsg, Object data) {Log.e(TAG, ">>>>> getRegistrationID failed, errCode = " + errCode+ ", errMsg = " + errMsg);}});}@Overridepublic void onError(int errCode, String errMsg, Object data) {Log.e("TIMPush", ">>>>> registerPush failed, errCode = " + errCode+ ", errMsg = " + errMsg);}});}@Overridepublic void onError(int code, String desc) {Log.e("TIMPush", ">>>>> IM login failed, code = " + code + ", desc = " + desc);}});}
import android.content.Contextimport android.util.Logimport com.tencent.imsdk.v2.V2TIMCallbackimport com.tencent.imsdk.v2.V2TIMManagerimport com.tencent.imsdk.v2.V2TIMSDKConfigimport com.tencent.qcloud.tim.push.TIMPushCallbackimport com.tencent.qcloud.tim.push.TIMPushManagerfun loginIMAndRegisterPush(context: Context) {val sdkAppId = 0 // TODO: Replace with your SDKAppID.val userID = "<YOUR_USER_ID>"val userSig = "<YOUR_USER_SIG>"val initSuccess = V2TIMManager.getInstance().initSDK(context, sdkAppId, V2TIMSDKConfig())if (!initSuccess) {Log.e("TIMPush", ">>>>> IM SDK init failed")return}V2TIMManager.getInstance().login(userID, userSig, object : V2TIMCallback {override fun onSuccess() {TIMPushManager.getInstance().registerPush(context, sdkAppId, null, object : TIMPushCallback<Any>() {override fun onSuccess(data: Any?) {Log.d("TIMPush", ">>>>> registerPush success, data = $data")TIMPushManager.getInstance().getRegistrationID(object : TIMPushCallback<Any?>() {override fun onSuccess(data: Any?) {Log.d("TIMPush", ">>>>> getRegistrationID success, registrationID = $data")}override fun onError(errCode: Int, errMsg: String?, data: Any?) {Log.e("TIMPush", ">>>>> getRegistrationID failed, errCode = $errCode, errMsg = $errMsg")}})}override fun onError(errCode: Int, errMsg: String?, data: Any?) {Log.e("TIMPush", ">>>>> registerPush failed, errCode = $errCode, errMsg = $errMsg")}})}override fun onError(code: Int, desc: String?) {Log.e("TIMPush", ">>>>> IM login failed, code = $code, desc = $desc")}})}
registerPush before Chat login. Otherwise the SDK may register a Push-type account for the standalone push scenario and generate corresponding Push DAU. Extra fees may apply after Push DAU exceeds the package quota.onSuccess callback of registerPush is triggered.registrationID or userID for the current scenario, and confirm that the token has been uploaded.onError is triggered, check the meaning of code in Error codes.registerPush.registrationID must be exactly the same as the userID used for Chat login. Otherwise account kick-out may cause push loss.registrationID or userID to send an offline push test.V2TIMOfflinePushInfo. Example:V2TIMOfflinePushInfo pushInfo = new V2TIMOfflinePushInfo();pushInfo.setTitle("Push title");pushInfo.setDesc("Push content");pushInfo.setExt("Business custom ext".getBytes());V2TIMManager.getMessageManager().sendMessage(v2TIMMessage,userID,null,V2TIMMessage.V2TIM_PRIORITY_DEFAULT,false,pushInfo,new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {}@Overridepublic void onError(int code, String desc) {Log.e("TIMPush", ">>>>> sendMessage failed, code = " + code + ", desc = " + desc);}@Overridepublic void onSuccess(V2TIMMessage message) {Log.d("TIMPush", ">>>>> sendMessage success, msgID = " + message.getMsgID());}});
sendMessage belongs to Chat SDK messaging. Users who integrate TIMPush only do not need to add full Chat initialization, login, and message sending just to verify offline push.
ext field (for example, target page or conversation ID). ext is a string with a business-defined structure. JSON is recommended for easier client parsing. The examples below use the following structure:# conversationType 1 means one-to-one chat (conversationID is the sender userID). 2 means group chat (conversationID is groupID).{"conversationID":"user_A","conversationType":1}
Ext field of the request body:{"MsgBody": [],"OfflinePushInfo": {"PushFlag": 0,"Title": "Offline push title","Desc": "Offline push content","Ext": "{\\"conversationID\\":\\"user_A\\",\\"conversationType\\":1}"}}
Ext field; enter a JSON string.ext property of V2TIMOfflinePushInfo, then send them with the message. setExt takes byte[], so convert your custom JSON string to a byte array first.ext with OfflinePushExtInfo, so you do not need to set it manually. The example below applies when you call Chat SDK yourself to send messages.import android.util.Log;import com.tencent.imsdk.v2.V2TIMManager;import com.tencent.imsdk.v2.V2TIMMessage;import com.tencent.imsdk.v2.V2TIMOfflinePushInfo;import com.tencent.imsdk.v2.V2TIMSendCallback;V2TIMOfflinePushInfo pushInfo = new V2TIMOfflinePushInfo();pushInfo.setTitle("Push title");pushInfo.setDesc("Push content");// TODO: ext is business-defined. Replace with your target page, conversation ID, and other parameters as needed.String ext = "{\\"conversationID\\":\\"user_A\\",\\"conversationType\\":1}";pushInfo.setExt(ext.getBytes());V2TIMMessage message = V2TIMManager.getMessageManager().createTextMessage("Hello TIMPush");V2TIMManager.getMessageManager().sendMessage(message,"<TARGET_USER_ID>", // For one-to-one chat, pass the peer userID; for group chat, pass nullnull, // For group chat, pass groupID; for one-to-one chat, pass nullV2TIMMessage.V2TIM_PRIORITY_DEFAULT,false,pushInfo,new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {}@Overridepublic void onSuccess(V2TIMMessage msg) {Log.d("TIMPush", ">>>>> sendMessage success, msgID = " + msg.getMsgID());}@Overridepublic void onError(int code, String desc) {Log.e("TIMPush", ">>>>> sendMessage failed, code = " + code + ", desc = " + desc);}});
import android.util.Logimport com.tencent.imsdk.v2.V2TIMManagerimport com.tencent.imsdk.v2.V2TIMMessageimport com.tencent.imsdk.v2.V2TIMOfflinePushInfoimport com.tencent.imsdk.v2.V2TIMSendCallbackval pushInfo = V2TIMOfflinePushInfo()pushInfo.title = "Push title"pushInfo.desc = "Push content"// TODO: ext is business-defined. Replace with your target page, conversation ID, and other parameters as needed.pushInfo.ext = "{\\"conversationID\\":\\"user_A\\",\\"conversationType\\":1}".toByteArray()val message = V2TIMManager.getMessageManager().createTextMessage("Hello TIMPush")V2TIMManager.getMessageManager().sendMessage(message,"<TARGET_USER_ID>", // For one-to-one chat, pass the peer userID; for group chat, pass nullnull, // For group chat, pass groupID; for one-to-one chat, pass nullV2TIMMessage.V2TIM_PRIORITY_DEFAULT,false,pushInfo,object : V2TIMSendCallback<V2TIMMessage> {override fun onProgress(progress: Int) {}override fun onSuccess(msg: V2TIMMessage) {Log.d("TIMPush", ">>>>> sendMessage success, msgID = ${msg.msgID}")}override fun onError(code: Int, desc: String?) {Log.e("TIMPush", ">>>>> sendMessage failed, code = $code, desc = $desc")}})
Application.onCreate(), call addPushListener to register TIMPushListener, then parse ext in onNotificationClicked and redirect to the business page. If you integrate TUIKit and use ext built by OfflinePushExtInfo, you can parse with new Gson().fromJson(ext, OfflinePushExtInfo.class) and then redirect.import android.app.Application;import android.text.TextUtils;import android.util.Log;import com.tencent.timpush.TIMPushListener;import com.tencent.timpush.TIMPushManager;import org.json.JSONObject;public class App extends Application {private static final String TAG = "TIMPush";@Overridepublic void onCreate() {super.onCreate();TIMPushManager.getInstance().addPushListener(new TIMPushListener() {@Overridepublic void onNotificationClicked(String ext) {Log.d(TAG, ">>>>> TIMPush notification clicked, ext = " + ext);if (TextUtils.isEmpty(ext)) {return;}// 1. Parse ext. The JSON structure is business-defined and must match the sender.String conversationID;int conversationType;try {JSONObject json = new JSONObject(ext);conversationID = json.optString("conversationID");conversationType = json.optInt("conversationType");} catch (Exception e) {Log.e(TAG, ">>>>> parse ext failed: " + e.getMessage());return;}// 2. TODO: Redirect to the target page based on business fields.// If using Chat / TUIKit, redirect after user login succeeds;// For cold start, cache the parameters first and redirect in the login callback.}});}}
import android.app.Applicationimport android.text.TextUtilsimport android.util.Logimport com.tencent.timpush.TIMPushListenerimport com.tencent.timpush.TIMPushManagerimport org.json.JSONObjectclass App : Application() {override fun onCreate() {super.onCreate()TIMPushManager.getInstance().addPushListener(object : TIMPushListener() {override fun onNotificationClicked(ext: String?) {Log.d("TIMPush", ">>>>> TIMPush notification clicked, ext = $ext")if (TextUtils.isEmpty(ext)) return// 1. Parse ext. The JSON structure is business-defined and must match the sender.val conversationID: Stringval conversationType: Inttry {val json = JSONObject(ext)conversationID = json.optString("conversationID")conversationType = json.optInt("conversationType")} catch (e: Exception) {Log.e("TIMPush", ">>>>> parse ext failed: ${e.message}")return}// 2. TODO: Redirect to the target page based on business fields.// If using Chat / TUIKit, redirect after user login succeeds;// For cold start, cache the parameters first and redirect in the login callback.}})}}
Apakah halaman ini membantu?
Anda juga dapat Menghubungi Penjualan atau Mengirimkan Tiket untuk meminta bantuan.
masukan