实现离线消息推送的过程如下:
OPPO 手机使用深度定制 Android 系统,对于第三方 App 自启动权限管理很严格,默认情况下第三方 App 都不会在系统的自启动白名单内,App 在后台时容易被系统 kill,因此推荐在 OPPO 设备上集成 OPPO 推送,OPPO 推送是 OPPO 设备的系统级服务,推送到达率较高。目前,即时通信 IM 仅支持 OPPO 推送的通知栏消息。
注意:
- 此指引文档是直接参考 OPPO 推送官方文档所写,若 OPPO 推送有变动,请以 OPPO 推送官网文档 为准。
- 如果不需要对 OPPO 设备做专门的离线推送适配,可以忽略此章节。
AppId
、AppKey
、AppSecret
和MasterSecret
信息。按照 OPPO 官网要求,在 OPPO Android 8.0 及以上系统版本必须配置 ChannelID,否则推送消息无法展示。您需要先在 App 中创建对应的 ChannelID(例如 tuikit
):
public void createNotificationChannel(Context context) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "oppotest";
String description = "this is opptest";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("tuikit", name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
登录腾讯云 即时通信 IM 控制台,单击目标应用卡片,进入应用的基础配置页面。
单击【Android平台推送设置】区域的【添加证书】。
说明:
如果您原来已有证书只需变更信息,可以单击【Android平台推送设置】区域的【编辑】进行修改更新。
单击【确认】保存信息,证书信息保存后10分钟内生效。
待推送证书信息生成后,记录证书的ID
。
PushManager.getInstance().register(…)
初始化 Opush 推送服务。PushCallback
的 onRegister
回调方法中得到regId
。regId
信息。若您需要通过 OPPO 推送进行即时通信 IM 消息的推送通知,必须在用户登录成功后通过TIMManager
中的setOfflinePushToken
方法将您托管到即时通信 IM 控制台生成的证书 ID 及 OPPO 推送服务返回的 regId 上报到即时通信 IM 服务端。
注意:
正确上报 regId 与证书 ID 后,即时通信 IM 服务才能将用户与对应的设备信息绑定,从而使用 OPPO 推送服务进行推送通知。
定义证书 ID 常量示例代码:
/****** OPPO 离线推送参数 start ******/
// 在腾讯云控制台上传第三方推送证书后分配的证书 ID
public static final long OPPO_PUSH_BUZID = 7005;
/****** OPPO 离线推送参数 end ******/
上报推送的证书 ID 及 regId 示例代码:
/**
* 在 ThirdPushTokenMgr.java 中对推送的证书 ID 及设备信息进行上报操作
*/
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 void setThirdPushToken(String mThirdPushToken) {
this.mThirdPushToken = mThirdPushToken; // regId 在此处传值,结合上文自定义 BroadcastReciever 类文档说明
}
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 = null;
if(IMFunc.isBrandXiaoMi()){ // 判断厂商品牌,根据不同厂商选择不同的推送服务
param = new TIMOfflinePushToken(Constants.XM_PUSH_BUZID, token);
}else if(IMFunc.isBrandHuawei()){
param = new TIMOfflinePushToken(Constants.HW_PUSH_BUZID, token);
}else if(IMFunc.isBrandMeizu()){
param = new TIMOfflinePushToken(Constants.MZ_PUSH_BUZID, token);
}else if(IMFunc.isBrandOppo()){
param = new TIMOfflinePushToken(Constants.OPPO_PUSH_BUZID, token);
}else if(IMFunc.isBrandVivo()){
param = new TIMOfflinePushToken(Constants.VIVO_PUSH_BUZID, token);
}else{
return;
}
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;
}
});
}
}
成功上报证书 ID 及 regId 后,即时通信 IM 服务端会在该设备上的即时通信 IM 用户 logout 之前、App 被 kill 之后将消息通过 OPPO 推送通知到用户端。
说明:
- OPPO 推送的常见问题请参见 OPPO PUSH FAQ。
- 若即时通信 IM 用户已经 logout 或被即时通信 IM 服务端主动下线(例如在其他端登录被踢等情况),则该设备上不会再收到消息推送。
您可以选择点击通知栏消息后打开应用、打开网页或打开应用内指定界面。
默认为点击通知栏消息打开应用。
您需要在 添加证书 时选择【打开网页】并输入以http://
或https://
开头的网址,例如https://www.tencentcloud.com/document/product/269?from_cn_redirect=1
。
打开应用内指定界面有以下几种方式:
Activity(推荐)
该方式比较简单,填入打开的 Activity 的完整类名即可,例如 com.tencent.qcloud.tim.demo.SplashActivity
Intent action
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
android.intent.action.VIEW
。在发消息前设置每条消息的通知栏自定义内容。
注意:
OPPO 要求自定义的数据必须是 json 格式。
Android 端示例如下:
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("extKey", "ext content");
} catch (JSONException e) {
e.printStackTrace();
}
String extContent = jsonObject.toString();
TIMMessageOfflinePushSettings settings = new TIMMessageOfflinePushSettings();
settings.setExt(extContent.getBytes());
timMessage.setOfflinePushSettings(settings);
mConversation.sendMessage(false, timMessage, callback);
服务端示例请参见 OfflinePushInfo 的格式示例。
在控制台选择设置点击通知 打开应用 、 打开应用内指定界面 的 Intent action 选项或者 Activity 选项后,当点击通知栏的消息时,客户端在相应的 Activity
中获取自定义内容。
Bundle bundle = intent.getExtras();
Set<String> set = bundle.keySet();
if (set != null) {
for (String key : set) {
// 其中 key 和 value 分别为发送端设置的 extKey 和 ext content
String value = bundle.getString(key);
Log.i("oppo push custom data", "key = " + key + ":value = " + value);
}
}
目前 OPPO 推送不支持自定义的提示音。
本页内容是否解决了您的问题?