logout
API is called to log out proactively or you are forced to log out due to multi-device login, you cannot receive offline push messages even though IM offline push is enabled.AppID
and AppKey
. Currently, mobile phone vendors supported are Google FCM, Mi, Huawei, OPPO, vivo, and Meizu.AppId
, AppKey
, and AppSecret
to the push certificates in the console. Take Google as an example.Vendor Push Platform | Configuring in the IM console |
| |
Vendor | Action after Click | Specified In-app Page |
Mi | Open the specified in-app page | intent:// your hostname /your path #Intent;scheme=your protocol, that is, the scheme you defined ;launchFlags=0x4000000;component=complete class name of the page to which your app is to be redirected ;endTUIKit demo configuration: intent://com.tencent.qcloud/detail#Intent;scheme=pushscheme;launchFlags=0x4000000;component=com.tencent.qcloud.tim.tuikit/com.tencent.qcloud.tim.demo.main.MainActivity;end |
Huawei | Open the specified in-app page | intent:// your hostname /your path #Intent;scheme=your protocol, that is, the scheme you defined ;launchFlags=0x4000000;component=complete class name of the page to which your app is to be redirected ;endTUIKit demo configuration: intent://com.tencent.qcloud/detail#Intent;scheme=pushscheme;launchFlags=0x4000000;component=com.tencent.qcloud.tim.tuikit/com.tencent.qcloud.tim.demo.main.MainActivity;end |
Meizu | Open the specified in-app page | Complete class name of the page to which your app is to be redirectedTUIKit demo configuration: com.tencent.qcloud.tim.demo.main.MainActivity |
OPPO | Open the specified in-app page | Complete class name of the page to which your app is to be redirectedTUIKit demo configuration: activity: com.tencent.qcloud.tim.demo.main.MainActivity |
vivo | Open the specified in-app page | intent:// your hostname /your path #Intent;scheme=your protocol, that is, the scheme you defined ;launchFlags=0x4000000;component=complete class name of the page to which your app is to be redirected ;endTUIKit demo configuration: intent://com.tencent.qcloud/detail#Intent;scheme=pushscheme;launchFlags=0x4000000;component=com.tencent.qcloud.tim.tuikit/com.tencent.qcloud.tim.demo.main.MainActivity;end |
Google FCM | No need to configure | Redirect to the Launcher page of the app by default |
<!-- The redirected-to page configured in the TUIKit demo is `MainActivity`, so you need to fill in `com.tencent.qcloud.tim.demo.main.MainActivity` here. After integration with your app, you need to replace the class name with the complete class name of the page to which your app is to be redirected.--><activityandroid:name="complete class name of the page to which your app is to be redirected"android:launchMode="singleTask"android:screenOrientation="portrait"android:windowSoftInputMode="adjustResize|stateHidden"><!-- Open the in-app page for offline push --><intent-filter><action android:name="android.intent.a, and configuration for Google FCM: pushscheme://com.tencent.qcloud/detail --><dataandroid:host="your hostname"android:path="your path"android:scheme="your protocol, that is, the scheme you defined" /></intent-filter></activity>
public class PrivateConstants {// Certificate ID generated after uploading a third-party push certificate in the IM consolepublic static final long GOOGLE_FCM_PUSH_BUZID = 15518;}
<!-- Note: The `applicationId` of the TUIKit demo is `com.tencent.qcloud.tim.tuikit`. You need to replace xxxx here with the `applicationId` of your app. --><!-- ********Google cloud messaging starts******** --><serviceandroid:name="xxxx.GoogleFCMMsgService"android:exported="false"><intent-filter><action android:name="com.google.firebase.MESSAGING_EVENT" /></intent-filter></service><!-- ********Google cloud messaging ends******** -->
build.gradle
file.dependencies {...classpath 'com.google.gms:google-services:4.2.0'}
build.gradle
file.apply plugin: 'com.google.gms.google-services'
dependencies {......// Main packageimplementation 'com.tencent.tpns:tpns:1.3.1.1-release'// Google FCMimplementation "com.tencent.tpns:fcm:1.3.1.1-release"// Google cloud messagingimplementation ('com.google.firebase:firebase-messaging:19.0.1')// Miimplementation "com.tencent.tpns:xiaomi:1.3.1.1-release"// Meizuimplementation "com.tencent.tpns:meizu:1.3.1.1-release"// OPPOimplementation "com.tencent.tpns:oppo:1.3.1.1-release"// vivoimplementation "com.tencent.tpns:vivo:1.3.2.0-release"// Huaweiimplementation 'com.tencent.tpns:huawei:1.3.1.1-release'implementation 'com.huawei.hms:push:5.0.2.300'}
public void init() {...if (BrandUtil.isBrandXiaoMi()) {// Mi offline pushMiPushClient.registerPush(this, PrivateConstants.XM_PUSH_APPID, PrivateConstants.XM_PUSH_APPKEY);} else if (BrandUtil.isBrandHuawei()) {// For Huawei offline push, set whether to receive the call example of the push notification bar messageHmsMessaging.getInstance(this).turnOnPush().addOnCompleteListener(new com.huawei.hmf.tasks.OnCompleteListener<Void>() {@Overridepublic void onComplete(com.huawei.hmf.tasks.Task<Void> task) {if (task.isSuccessful()) {DemoLog.i(TAG, "huawei turnOnPush Complete");} else {DemoLog.e(TAG, "huawei turnOnPush failed: ret=" + task.getException().getMessage());}}});new Thread(){@Overridepublic void run() {try{// read from agconnect-services.jsonString appId = AGConnectServicesConfig.fromContext(MainActivity.this).getString("client/app_id");String token = HmsInstanceId.getInstance(MainActivity.this).getToken(appId, "HCM");DemoLog.i(TAG, "huawei get token:" + token);if(!TextUtils.isEmpty(token)) {// Call the `setOfflinePushConfig` API of the IM SDK to report this tokenString token = (String) o;}} catch (ApiException e) {DemoLog.e(TAG, "huawei get token failed, " + e);}}}.start();} else if (MzSystemUtils.isBrandMeizu(this)) {// Meizu offline pushPushManager.register(this, PrivateConstants.MZ_PUSH_APPID, PrivateConstants.MZ_PUSH_APPKEY);} else if (BrandUtil.isBrandVivo()) {// vivo offline pushPushClient.getInstance(getApplicationContext()).initialize();DemoLog.i(TAG, "vivo support push: " + PushClient.getInstance(getApplicationContext()).isSupport());PushClient.getInstance(getApplicationContext()).turnOnPush(new IPushActionListener() {@Overridepublic void onStateChanged(int state) {if (state == 0) {String regId = PushClient.getInstance(getApplicationContext()).getRegId();DemoLog.i(TAG, "vivopush open vivo push success regId = " + regId);// // Call the `setOfflinePushConfig` API of the IM SDK to report this tokenString token = (String) o;} else {// According to the vivo documentation, state = 101 means this particular vivo device or system version does not support vivo Push. See https://dev.vivo.com.cn/documentCenter/doc/156 for details.DemoLog.i(TAG, "vivopush open vivo push fail state = " + state);}}});} else if (HeytapPushManager.isSupportPush()) {// OPPO offline pushOPPOPushImpl oppo = new OPPOPushImpl();oppo.createNotificationChannel(this);// According to the OPPO documentation, the app must call the init(...) API before proceeding to subsequent operations.HeytapPushManager.init(this, false);HeytapPushManager.register(this, PrivateConstants.OPPO_PUSH_APPKEY, PrivateConstants.OPPO_PUSH_APPSECRET, oppo);} else if (BrandUtil.isGoogleServiceSupport()) {FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new com.google.android.gms.tasks.OnCompleteListener<InstanceIdResult>() {@Overridepublic void onComplete(Task<InstanceIdResult> task) {if (!task.isSuccessful()) {DemoLog.w(TAG, "getInstanceId failed exception = " + task.getException());return;}// Get new Instance ID tokenString token = task.getResult().getToken();DemoLog.i(TAG, "google fcm getToken = " + token);// Call the `setOfflinePushConfig` API of the IM SDK to report this tokenString token = (String) o;}});}}
public class GoogleFCMMsgService extends FirebaseMessagingService {...@Overridepublic void onNewToken(String token) {DemoLog.i(TAG, "onNewToken token=" + token);// Call the `setOfflinePushConfig` API of the IM SDK to report this tokenString pushToken = token;}...}
businessID
as the certificate ID of the vendor and isTPNSToken
as false
and report the token obtained after successful registration of the vendor push service. Note: If you use the offline push service of TPNS, set isTPNSToken
as true
and report the token obtained after successful registration of the TPNS push service. Then, TPNS will provide the push service.V2TIMOfflinePushConfig v2TIMOfflinePushConfig = null;// Set `businessID` as the certificate ID of the vendor and `isTPNSToken` as `false`, and report the token obtained after registration of the vendor push service.v2TIMOfflinePushConfig = new V2TIMOfflinePushConfig(0, token, true);V2TIMManager.getOfflinePushManager().setOfflinePushConfig(v2TIMOfflinePushConfig, new V2TIMCallback() {@Overridepublic void onError(int code, String desc) {DemoLog.d(TAG, "setOfflinePushToken err code = " + code);}@Overridepublic void onSuccess() {DemoLog.d(TAG, "setOfflinePushToken success");}});
// When the app is switched to the backgroundV2TIMManager.getOfflinePushManager().doBackground(totalCount, new V2TIMCallback() {@Overridepublic void onError(int code, String desc) {DemoLog.e(TAG, "doBackground err = " + code + ", desc = " + desc);}@Overridepublic void onSuccess() {DemoLog.i(TAG, "doBackground success");}});// When the app is switched back to the foregroundV2TIMManager.getOfflinePushManager().doForeground(new V2TIMCallback() {@Overridepublic void onError(int code, String desc) {DemoLog.e(TAG, "doForeground err = " + code + ", desc = " + desc);}@Overridepublic void onSuccess() {DemoLog.i(TAG, "doForeground success");}});
OfflineMessageContainerBean containerBean = new OfflineMessageContainerBean();OfflineMessageBean entity = new OfflineMessageBean();entity.content = message.getExtra().toString();entity.sender = message.getFromUser();entity.nickname = chatInfo.getChatName();entity.faceUrl = TUIChatConfigs.getConfigs().getGeneralConfig().getUserFaceUrl();containerBean.entity = entity;V2TIMOfflinePushInfo v2TIMOfflinePushInfo = new V2TIMOfflinePushInfo();v2TIMOfflinePushInfo.setExt(new Gson().toJson(containerBean).getBytes());// For OPPO, you must set the `ChannelID` to receive push messages. The `ChannelID` must be identical with that in the console.v2TIMOfflinePushInfo.setAndroidOPPOChannelID("tuikit");final V2TIMMessage v2TIMMessage = message.getTimMessage();String msgID = V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, isGroup ? null : userID, isGroup ? groupID : null,V2TIMMessage.V2TIM_PRIORITY_DEFAULT, false, v2TIMOfflinePushInfo, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {}@Overridepublic void onError(int code, String desc) {TUIChatUtils.callbackOnError(callBack, TAG, code, desc);}@Overridepublic void onSuccess(V2TIMMessage v2TIMMessage) {TUIChatLog.v(TAG, "sendMessage onSuccess:" + v2TIMMessage.getMsgID());message.setMsgTime(v2TIMMessage.getTimestamp());TUIChatUtils.callbackOnSuccess(callBack, message);}});
getIntent().getExtras()
to get the offline push parameters configured in Step 6. For sample code, see the handleOfflinePush() method of the TUIKit demo.private void handleOfflinePush() {// Determine whether to log in to IM again based on the login status// 1. If the login status is V2TIMManager.V2TIM_STATUS_LOGOUT, redirect to the login page and log in to IM again.if (V2TIMManager.getInstance().getLoginStatus() == V2TIMManager.V2TIM_STATUS_LOGOUT) {Intent intent = new Intent(MainActivity.this, SplashActivity.class);if (getIntent() != null) {intent.putExtras(getIntent());}startActivity(intent);finish();return;}// 2. If the login status is not V2TIMManager.V2TIM_STATUS_LOGOUT, the app runs in the background, and the offline push parameters can be parsed directly.final OfflineMessageBean bean = OfflineMessageDispatcher.parseOfflineMessage(getIntent());if (bean != null) {setIntent(null);NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);if (manager != null) {manager.cancelAll();}if (bean.action == OfflineMessageBean.REDIRECT_ACTION_CHAT) {if (TextUtils.isEmpty(bean.sender)) {return;}TUIUtils.startChat(bean.sender, bean.nickname, bean.chatType);}}}
getIntent().getExtras()
to get the offline push parameters configured in Step 6, and then parse messages and customize the redirection.desc
information will be displayed by default in the push message.
Was this page helpful?