com.tencent.android.tpush
,其中有以下几个重要的对外提供接口的类名,如下表所示:类名 | 说明 |
XGPushManager | Push 服务推送 |
XGPushConfig | Push 服务配置项接口 |
XGPushBaseReceiver | 接收消息和结果反馈的 Receiver,需要开发者在 AndroidManifest.xml 自主完成静态注册 |
public static void registerPush(Context context)
XGPushManager.registerPush(getApplicationContext());
public static void registerPush(Context context,final XGIOperateCallback callback)
XGPushManager.registerPush(this, new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {Log.d("TPush", "注册成功,设备token为:" + data);}@Overridepublic void onFail(Object data, int errCode, String msg) {Log.d("TPush", "注册失败,错误码:" + errCode + ",错误信息:" + msg);}})
/*** 操作回调接口*/public interface XGIOperateCallback {/*** 操作成功时的回调。* @param data 操作成功的业务数据,如注册成功时的token信息等。* @param flag 标记码*/public void onSuccess(Object data, int flag);/*** 操作失败时的回调* @param data 操作失败的业务数据* @param errCode 错误码* @param msg 错误信息*/public void onFail(Object data, int errCode, String msg);}
/**** @param context 当前上下文* @param errorCode 0 为成功,其它为错误码* @param message 注册结果返回*/@Overridepublic void onRegisterResult(Context context, int errorCode, XGPushRegisterResult message) {if (context == null || message == null) {return;}String text = "";if (errorCode == XGPushBaseReceiver.SUCCESS) { // 注册成功// 在这里拿tokenString token = message.getToken();text = "注册成功,token:" + token;} else {text = message + "注册失败,错误码:" + errorCode;}Log.d(LogTag, text);}
方法名 | 返回值 | 默认值 | 描述 |
getToken() | String | 无 | 设备的 Token,即设备唯一识别 ID |
getAccessId() | long | 0 | 获取注册的 AccessId |
getAccount | String | 无 | 获取注册绑定的账号 |
getTicket() | String | 无 | 登录态票据 |
getTicketType() | short | 0 | 票据类型 |
public static void unregisterPush(Context context)
XGPushManager.unregisterPush(getApplicationContext(), new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int i) {Log.d("TPush", "反注册成功");}@Overridepublic void onFail(Object data, int errCode, String msg) {Log.d("TPush", "反注册失败,错误码:" + errCode + ",错误信息:" + msg);}});
/*** 反注册结果* @param context 当前上下文* @param errorCode 为成功,其它为错误码*/@Overridepublic void onUnregisterResult(Context context, int errorCode) {if (context == null) {return;}String text = "";if (errorCode == XGPushBaseReceiver.SUCCESS) {text = "反注册成功";} else {text = "反注册失败" + errorCode;}Log.d(LogTag, text);}
onNotificationShowedResult(Context, XGPushShowedResult)
方法实现。其中,XGPushShowedResult 对象提供读取通知内容的接口。onNotificationShowedResult
仅支持移动推送自建通道下发通知抵达的监听,不支持厂商通道消息抵达的监听。public abstract void onNotificationShowedResult(Context context,XGPushShowedResult notifiShowedRlt);
onNotificationClickedResult(Context, XGPushClickedResult)
方法实现。public abstract void onNotificationClickedResult(Context context, XGPushClickedResult notifiClickedRlt);
// 通知点击回调,actionType=0 为该消息被点击,actionType=2 为该消息被清除@Overridepublic void onNotificationClickedResult(Context context, XGPushClickedResult message) {if (context == null || message == null) {return;}String text = "";if (message.getActionType() == NotificationAction.clicked.getType()) {// 通知在通知栏被点击// APP自己处理点击的相关动作text = "通知被打开 :" + message;} else if (message.getActionType() == NotificationAction.delete.getType()) {// 通知被清除// APP自己处理通知被清除后的相关动作text = "通知被清除 :" + message;}// APP自主处理的过程。Log.d(LogTag, "广播接收到通知:" + text);}
XGPushClickedResult
类成员方法列表:方法名 | 返回值 | 默认值 | 描述 |
getMsgId() | long | 0 | 消息 ID |
getTitle() | String | 无 | 通知标题 |
getContent() | String | 无 | 通知正文内容 |
getActionType() | String | 无 | 0 表示该通知被点击,2 表示该通知被清除 |
getPushChannel() | String | 100 | 被点击通知的所下发通道标识。 100:移动推送自建通道。 101:FCM 通道。 102:华为通道。 103:小米通道。 104:vivo 通道。 105:OPPO 通道。 106:魅族通道。 |
public static void cancelAllNotifaction(Context context)
XGPushManager.cancelAllNotifaction(context);
public static void createNotificationChannel(Context context, String channelId, String channelName, boolean enableVibration, boolean enableLights, boolean enableSound, Uri soundUri)
// 请将铃声文件放置在 Android 工程资源目录 raw 下,此处以文件 ring.mp3 为例String uri = "android.resource://" + context.getPackageName() + "/" + R.raw.ring;Uri soundUri = Uri.parse(uri);XGPushManager.createNotificationChannel(context,"default_message", "默认通知", true, true, true, soundUri);
<receiver android:name="com.tencent.android.xg.cloud.demo.MessageReceiver"><intent-filter><!-- 接收消息透传 --><action android:name="com.tencent.android.xg.vip.action.PUSH_MESSAGE" /><!-- 监听注册、反注册、设置/删除标签、通知被点击等处理结果 --><action android:name="com.tencent.android.xg.vip.action.FEEDBACK" /></intent-filter></receiver>
public void onTextMessage(Context context,XGPushTextMessage message)
方法名 | 返回值 | 默认值 | 描述 |
getContent() | String | 无 | 消息正文内容,通常只需要下发本字段即可 |
getCustomContent() | String | 无 | 消息自定义 key-value |
getTitle() | String | 无 | 消息标题(从前台下发应用内消息字中的描述不属于标题) |
WebView.setDataDirectorySuffix()
方法为每个进程指定唯一的数据目录后缀,否则可能引起程序崩溃。配置示例代码如下:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {// 自 Android 9 起,未非 app 主进程的 WebView 实例设置不同的 WebView 数据目录String processName = getProcessName()if (processName != null&& !processName.equals(context.getPackageName())) {WebView.setDataDirectorySuffix(processName)}}
XGPushConfig.enableShowInMsg(context, true);
//新建本地通知XGLocalMessage local_msg = new XGLocalMessage();//设置本地消息类型,1:通知,2:消息local_msg.setType(1);// 设置消息标题local_msg.setTitle("qq");//设置消息内容local_msg.setContent("ww");//设置消息日期,格式为:20140502local_msg.setDate("20140930");//设置消息触发的小时(24小时制),例如:22代表晚上10点local_msg.setHour("19");//获取消息触发的分钟,例如:05代表05分local_msg.setMin("31");//设置消息样式,默认为0或不设置local_msg.setBuilderId(0);//设置动作类型:1打开activity或App本身,2打开浏览器,3打开Intent ,4通过包名打开应用local_msg.setAction_type(1);//设置拉起应用页面local_msg.setActivity("com.qq.xgdemo.SettingActivity");// 设置URLlocal_msg.setUrl("http://www.baidu.com");// 设置Intentlocal_msg.setIntent("intent:10086#Intent;scheme=tel;action=android.intent.action.DIAL;S.key=value;end");// 是否覆盖原先build_id的保存设置。1覆盖,0不覆盖local_msg.setStyle_id(1);// 设置音频资源local_msg.setRing_raw("mm");// 设置key,valueHashMap<String, Object> map = new HashMap<String, Object>();map.put("key", "v1");map.put("key2", "v2");local_msg.setCustomContent(map);//添加通知到本地XGPushManager.addLocalNotification(context,local_msg);
public static void clearLocalNotifications(Context context)
XGPushManager.clearLocalNotifications(context);
public static void upsertAccounts(Context context, List<AccountInfo> accountInfoList, XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {Log.i("TPush", "onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {Log.w("TPush", "onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};List<XGPushManager.AccountInfo> accountInfoList = new ArrayList<>();accountInfoList.add(new XGPushManager.AccountInfo(XGPushManager.AccountType.UNKNOWN.getValue(), "account-test"));XGPushManager.upsertAccounts(context, accountInfoList, xgiOperateCallback);
+[国家或地区码][手机号]
,例如+8613711112222(其中前面有一个+号 ,86为国家或地区码,13711112222为手机号)。若绑定的手机号不带国家或地区码,则移动推送下发短信时自动增加+86的前缀;若带上国家或地区码,则按照指定的号码绑定。如需删除绑定的手机号,则需调用 delAccountsByKeys
接口并设置 accountTypeSet 为 1002
。public static void upsertPhoneNumber(Context context, String phoneNumber, XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {Log.i("TPush", "onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {Log.w("TPush", "onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};XGPushManager.upsertPhoneNumber(context, phoneNumber, xgiOperateCallback);
//解绑指定账号(有注册回调)void delAccount(Context context, final String account, XGIOperateCallback callback)//解绑指定账号(无注册回调)void delAccount(Context context, final String account )
XGPushManager.delAccount(getApplicationContext(),"test");
public static void delAccounts(Context context, final Set<Integer> accountTypeSet, XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {Log.i("TPush", "onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {Log.w("TPush", "onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};Set<Integer> accountTypeSet = new HashSet<>();accountTypeSet.add(XGPushManager.AccountType.CUSTOM.getValue());accountTypeSet.add(XGPushManager.AccountType.IMEI.getValue());XGPushManager.delAccounts(context, accountTypeSet, xgiOperateCallback);
//解绑所有的账号信息(有注册回调)void clearAccounts(Context context, XGIOperateCallback callback)//解绑所有的账号信息(无注册回调)void clearAccounts(Context context)
XGPushManager.clearAccounts(getApplicationContext());
public static void clearAndAppendTags(Context context, String operateName, Set<String> tags)
String[] tags = "tag1 tag2".split(" ");Set<String> tagsSet = new HashSet<>(Arrays.asList(tags));XGPushManager.clearAndAppendTags(getApplicationContext(), "clearAndAppendTags :" + System.currentTimeMillis(), tagsSet);
:
号,例如 test:2, level:2
,则会删除这个设备已绑定的所有 test:*
和 level:*
标签,再新增 test:2
和 level:2
。:
号,例如 test:2 level
,则会删除这个设备的全部历史标签,再新增 test:2
和 level
标签。:
号为后台关键字,请根据具体的业务场景使用。public static void appendTags(Context context, String operateName, Set<String> tags)
String[] tags = "tag1 tag2".split(" ");Set<String> tagsSet = new HashSet<>(Arrays.asList(tags));XGPushManager.appendTags(getApplicationContext(), "appendTags:" + System.currentTimeMillis(), tagsSet);
public static void delTags(Context context, String operateName, Set<String> tags, XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {Log.i("TPush", "onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {Log.w("TPush", "onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};Set<String> tagSet = new HashSet<>();tagSet.add("tag1");tagSet.add("tag2");XGPushManager.delTags(context, "delTags", tagSet, xgiOperateCallback);
public static void clearTags(Context context, String operateName, XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {Log.i("TPush", "onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {Log.w("TPush", "onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};XGPushManager.clearTags(context, "clearTags", xgiOperateCallback);
public static void queryTags(final Context context, final String operateName, final int offset, final int limit, final XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {Log.i("TPush", "onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {Log.w("TPush", "onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};XGPushManager.queryTags(context, 0, 100, xgiOperateCallback);
public static void upsertAttributes(Context context, String operateName, Map<String, String> attributes, XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {log("action - onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {log("action - onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};Map<String,String> attr = new HashMap<>();attr.put("name", "coding-test");attr.put("gender", "male");attr.put("age", "100");XGPushManager.upsertAttributes(context, "addAttributes-test", attr, xgiOperateCallback);
public static void delAttributes(Context context, String operateName, Set<String> attributes, XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {log("action - onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {log("action - onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};Set<String> stringSet = new HashSet<>();stringSet.add("name");stringSet.add("gender");XGPushManager.delAttributes(context, "delAttributes-test", stringSet, xgiOperateCallback);
public static void clearAttributes(Context context, String operateName, XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {log("action - onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {log("action - onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};XGPushManager.clearAttributes(context, "cleanAttributes-test", xgiOperateCallback);
public static void clearAndAppendAttributes(Context context, String operateName, Map<String, String> attributes, XGIOperateCallback callback)
XGIOperateCallback xgiOperateCallback = new XGIOperateCallback() {@Overridepublic void onSuccess(Object data, int flag) {log("action - onSuccess, data:" + data + ", flag:" + flag);}@Overridepublic void onFail(Object data, int errCode, String msg) {log("action - onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);}};Map<String,String> attr = new HashMap<>();attr.put("name", "coding-test");attr.put("gender", "male");attr.put("age", "100");XGPushManager.clearAndAppendAttributes(context, "setAttributes-test", attr, xgiOperateCallback);
XGPushConfig.enablePullUpOtherApp(Context context, boolean pullUp);
XGPushConfig.enablePullUpOtherApp(context, false); // 默认为 true: 开启保活
public static void enableDebug(Context context, boolean debugMode)
XGPushConfig.enableDebug(context, true); // 默认为 false: 不打开
public static String getToken(Context context)
XGPushConfig.getToken(context);
public static String getOtherPushToken(Context context)
XGPushConfig.getOtherPushToken(context);
public static String getCustomContentFromIntent(Context context, Intent intent)
String customContent = XGPushManager.getCustomContentFromIntent(this, intent);
public static boolean setAccessId(Context context, long accessId)
long accessId = 0L; // 当前应用的 accessIdXGPushConfig.setAccessId(context, accessId);
public static boolean setAccessKey(Context context, String accessKey)
String accessKey = ""; // 您应用的 accessKeyXGPushConfig.setAccessKey(context, accessKey);
public static void uploadLogFile(Context context, HttpRequestCallback httpRequestCallback)
XGPushManager.uploadLogFile(context, new HttpRequestCallback() {@Overridepublic void onSuccess(String result) {Log.d("TPush", "上传成功,文件地址:" + result);}@Overridepublic void onFailure(int errCode, String errMsg) {Log.d("TPush", "上传失败,错误码:" + errCode + ",错误信息:" + errMsg);}});
XGPushConfig.enableDebug(this, true);
。
本页内容是否解决了您的问题?