admin
2021-04-07 cbb88109494ffc7916f6639c20ce05c0cec941a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.weikou.beibeivideo.util;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
 
import com.lcjian.library.util.common.PackageUtils2;
import com.lcjian.library.util.common.StringUtils;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushBaseReceiver;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.android.tpush.XGPushShowedResult;
import com.tencent.android.tpush.service.XGPushService;
 
/**
 * Created by weikou2015 on 2016/9/8.
 */
public class XGPush {
 
    public static void registerPush(Context context) {
        // 开启logcat输出,方便debug,发布时请关闭
        XGPushConfig.enableDebug(context, true);
        // 如果需要知道注册是否成功,请使用registerPush(getApplicationContext(), XGIOperateCallback)带callback版本
        // 如果需要绑定账号,请使用registerPush(getApplicationContext(),account)版本
        // 具体可参考详细的开发指南
        // 传递的参数为ApplicationContext
        SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
        String loginId = sp.getString("LoginUid", "");
        XGPushManager.setTag(context, PackageUtils2.getVersionCode(context) + "_A");
        if (StringUtils.isEmpty(loginId)) {
            XGPushManager.registerPush(context, new XGIOperateCallback() {
                @Override
                public void onSuccess(Object o, int i) {
                }
 
                @Override
                public void onFail(Object o, int i, String s) {
                }
            });
        } else {
            XGPushManager.registerPush(context, loginId, new XGIOperateCallback() {
                @Override
                public void onSuccess(Object o, int i) {
                }
 
                @Override
                public void onFail(Object o, int i, String s) {
                }
            });
 
        }
//        // 2.36(不包括)之前的版本需要调用以下2行代码
//        Intent service = new Intent(context, XGPushService.class);
//        context.startService(service);
 
        // 其它常用的API:
        // 绑定账号(别名)注册:registerPush(context,account)或registerPush(context,account, XGIOperateCallback),其中account为APP账号,可以为任意字符串(qq、openid或任意第三方),业务方一定要注意终端与后台保持一致。
        // 取消绑定账号(别名):registerPush(context,"*"),即account="*"为取消绑定,解绑后,该针对该账号的推送将失效
        // 反注册(不再接收消息):unregisterPush(context)
        // 设置标签:setTag(context, tagName)
        // 删除标签:deleteTag(context, tagName)
    }
 
    public static void unRegisterPush(Context context) {
        XGPushManager.unregisterPush(context);
    }
 
}