Note:
To use FCM offline push, you need to install Google Play Services on your mobile phone and use it outside Mainland China.
The process of implementing offline message push is as follows:
Note:
The website in this step is the official website of Firebase, which is accessible only outside Mainland China.
Note:
You already have a certificate and only need to change its information, click Edit.
After users successfully log in, use the setOfflinePushToken
method of TIMManager
to report the Certificate ID, generated and hosted by the IM console, and token, generated by the client after FCM integration, to the IM server.
Note:
After the token and certificate ID are correctly reported, the IM service can bind users with the corresponding device information, thus enabling the use of FCM to push notifications.
The following is sample code from the demo:
Define the certificate ID constant:
/****** FCM offline push parameter start ******/
// Use your certificate ID in the FCM push certificate information on the IM console
public static final long GOOGLE_FCM_PUSH_BUZID = 6768;
/****** FCM offline push parameter end ******/
Reporting the push certificate ID and token:
/**
* Report the push certificate ID and device information in ThirdPushTokenMgr.java
*/
public class ThirdPushTokenMgr {
private static final String TAG = "ThirdPushTokenMgr";
private String mThirdPushToken;
public static ThirdPushTokenMgr getInstance () {
return ThirdPushTokenHolder.instance;
}
private static class ThirdPushTokenHolder {
private static final ThirdPushTokenMgr instance = new ThirdPushTokenMgr();
}
public String getThirdPushToken() {
return mThirdPushToken;
}
public void setThirdPushToken(String mThirdPushToken) {
this.mThirdPushToken = mThirdPushToken; // Token value specified here
}
public void setPushTokenToTIM(){
String token = ThirdPushTokenMgr.getInstance().getThirdPushToken();
if(TextUtils.isEmpty(token)){
QLog.i(TAG, "setPushTokenToTIM third token is empty");
mIsTokenSet = false;
return;
}
TIMOfflinePushToken param = new TIMOfflinePushToken(Constants.GOOGLE_FCM_PUSH_BUZID, token);
TIMManager.getInstance().setOfflinePushToken(param, new TIMCallBack() {
@Override
public void onError(int code, String desc) {
Log.d(TAG, "setOfflinePushToken err code = " + code);
}
@Override
public void onSuccess() {
Log.d(TAG, "setOfflinePushToken success");
mIsTokenSet = true;
}
});
}
}
After the certificate ID and token are successfully reported, the IM server sends messages via FCM push notifications to the user before IM user logout on the device and even if the app is killed.
Note:
- FCM push is not 100% successful in reaching target users.
- FCM push may be delayed. Usually, this is related to the timing of app killing. In some cases, it is related to the FCM push service.
- If the IM user has logged out or been forced offline by the IM server (for example, due to login on another device), the device cannot receive pushed messages.
Set the custom content for the notification bar message before sending the message.
Android sample:
String extContent = "ext content";
TIMMessageOfflinePushSettings settings = new TIMMessageOfflinePushSettings();
settings.setExt(extContent.getBytes());
timMessage.setOfflinePushSettings(settings);
mConversation.sendMessage(false, timMessage, callback);
For information on configurations for the IM server, refer to the OfflinePushInfo Format Example.
The client will obtain the custom content from the corresponding Activity
once the notification bar message is clicked.
Bundle bundle = getIntent().getExtras();
String value = bundle.getString("ext");
Currently, FCM push does not support custom alert sounds.
Was this page helpful?