信鸽推送。。。。。。
首先在 https://xg.qq.com/xg/ctr_index/download网站上注册一个账号
然后在这里新建一个应用
应用包名要跟项目包名一致
那个 ACCESS ID和ACCESS KEY留着有用.
在自己项目的builder里面加上;ndk:
ndk {
//选择要添加的对应cpu类型的.so库。
abiFilters ‘armeabi’, ‘armeabi-v7a’, ‘arm64-v8a’, ‘x86’, ‘x86_64’, ‘mips’, ‘mips64’
// 还可以添加 ‘x86’, ‘x86_64’, ‘mips’, ‘mips64’
//在这里吧刚刚自己要用的 id和 key 复制到这里
manifestPlaceholders = [
XG_ACCESS_ID : “2100328227”,
XG_ACCESS_KEY: “AM48BR5KP79R”,
]
}
导入依赖:
//根据需求选择对应的版本号
implementation ‘com.tencent.xinge:xinge:3.1.2-beta’
implementation ‘com.tencent.wup:wup:1.0.0.E-alpha’
implementation ‘com.tencent.mid:mid:3.9.0-alpha’
然后创建个MessageReceiver类继承XGPushBaseReceiver:
//这点是在清单文件中配置刚刚那个 MessageReceiver类
<receiver android:name=".receiver.MessageReceiver" android:exported="true" >
<intent-filter>
<!-- 接收消息透传 -->
<action android:name="com.tencent.android.tpush.action.PUSH_MESSAGE" />
<!-- 监听注册、反注册、设置/删除标签、通知被点击等处理结果 -->
<action android:name="com.tencent.android.tpush.action.FEEDBACK" />
</intent-filter>
</receiver>
public class MessageReceiver extends XGPushBaseReceiver {
private Intent intent = new Intent("com.qq.xgdemo.activity.UPDATE_LISTVIEW");
public static final String LogTag = "TPushReceiver";
private void show(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
// 通知展示
@Override
public void onNotifactionShowedResult(Context context,
XGPushShowedResult notifiShowedRlt) {
if (context == null || notifiShowedRlt == null) {
return;
}
XGNotification notific = new XGNotification();
notific.setMsg_id(notifiShowedRlt.getMsgId());
notific.setTitle(notifiShowedRlt.getTitle());
notific.setContent(notifiShowedRlt.getContent());
// notificationActionType==1为Activity,2为url,3为intent
notific.setNotificationActionType(notifiShowedRlt
.getNotificationActionType());
//Activity,url,intent都可以通过getActivity()获得
notific.setActivity(notifiShowedRlt.getActivity());
notific.setUpdate_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(Calendar.getInstance().getTime()));
// NotificationService.getInstance(context).save(notific);
context.sendBroadcast(intent);
show(context, "您有1条新消息, " + "通知被展示 , " + notifiShowedRlt.toString());
Log.d("LC", "+++++++++++++++++++++++++++++展示通知的回调");
}
//反注册的回调
@Override
public void onUnregisterResult(Context context, int errorCode) {
if (context == null) {
return;
}
String text = "";
if (errorCode == XGPushBaseReceiver.SUCCESS) {
text = "反注册成功";
} else {
text = "反注册失败" + errorCode;
}
Log.d(LogTag, text);
show(context, text);
}
//设置tag的回调
@Override
public void onSetTagResult(Context context, int errorCode, String tagName) {
if (context == null) {
return;
}
String text = "";
if (errorCode == XGPushBaseReceiver.SUCCESS) {
text = "\"" + tagName + "\"设置成功";
} else {
text = "\"" + tagName + "\"设置失败,错误码:" + errorCode;
}
Log.d(LogTag, text);
show(context, text);
}
//删除tag的回调
@Override
public void onDeleteTagResult(Context context, int errorCode, String tagName) {
if (context == null) {
return;
}
String text = "";
if (errorCode == XGPushBaseReceiver.SUCCESS) {
text = "\"" + tagName + "\"删除成功";
} else {
text = "\"" + tagName + "\"删除失败,错误码:" + errorCode;
}
Log.d(LogTag, text);
show(context, text);
}
// 通知点击回调 actionType=1为该消息被清除,actionType=0为该消息被点击。此处不能做点击消息跳转,详细方法请参照官网的Android常见问题文档
@Override
public void onNotifactionClickedResult(Context context,
XGPushClickedResult message) {
Log.e("LC", "+++++++++++++++ 通知被点击 跳转到指定页面。");
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
if (context == null || message == null) {
return;
}
String text = "";
if (message.getActionType() == XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) {
// 通知在通知栏被点击啦。。。。。
// APP自己处理点击的相关动作
// 这个动作可以在activity的onResume也能监听,请看第3点相关内容
text = "通知被打开 :" + message;
} else if (message.getActionType() == XGPushClickedResult.NOTIFACTION_DELETED_TYPE) {
// 通知被清除啦。。。。
// APP自己处理通知被清除后的相关动作
text = "通知被清除 :" + message;
}
Toast.makeText(context, "广播接收到通知被点击:" + message.toString(),
Toast.LENGTH_SHORT).show();
// 获取自定义key-value
String customContent = message.getCustomContent();
if (customContent != null && customContent.length() != 0) {
try {
JSONObject obj = new JSONObject(customContent);
// key1为前台配置的key
if (!obj.isNull("key")) {
String value = obj.getString("key");
Log.d(LogTag, "get custom value:" + value);
}
// ...
} catch (JSONException e) {
e.printStackTrace();
}
}
// APP自主处理的过程。。。
Log.d(LogTag, text);
show(context, text);
}
//注册的回调
@Override
public void onRegisterResult(Context context, int errorCode,
XGPushRegisterResult message) {
// TODO Auto-generated method stub
if (context == null || message == null) {
return;
}
String text = "";
if (errorCode == XGPushBaseReceiver.SUCCESS) {
text = message + "注册成功";
// 在这里拿token
String token = message.getToken();
} else {
text = message + "注册失败错误码:" + errorCode;
}
Log.d(LogTag, text);
show(context, text);
}
// 消息透传的回调
@Override
public void onTextMessage(Context context, XGPushTextMessage message) {
// TODO Auto-generated method stub
String text = "收到消息:" + message.toString();
// 获取自定义key-value
String customContent = message.getCustomContent();
if (customContent != null && customContent.length() != 0) {
try {
JSONObject obj = new JSONObject(customContent);
// key1为前台配置的key
if (!obj.isNull("key")) {
String value = obj.getString("key");
Log.d(LogTag, "get custom value:" + value);
}
// ...
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.d("LC", "++++++++++++++++透传消息");
// APP自主处理消息的过程...
Log.d(LogTag, text);
show(context, text);
} }
创建个 XGNotification类
public class XGNotification {
private Integer id;
private long msg_id;
private String title;
private String content;
private String activity;
private int notificationActionType;
private String update_time;
public void setId(Integer id) {
this.id = id;
}
public void setMsg_id(long msg_id) {
this.msg_id = msg_id;
}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
public void setActivity(String activity) {
this.activity = activity;
}
public void setNotificationActionType(int notificationActionType) {
this.notificationActionType = notificationActionType;
}
public void setUpdate_time(String update_time) {
this.update_time = update_time;
}
public Integer getId() {
return id;
}
public long getMsg_id() {
return msg_id;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public String getActivity() {
return activity;
}
public int getNotificationActionType() {
return notificationActionType;
}
public String getUpdate_time() {
return update_time;
}
public XGNotification() {
}
public XGNotification(Integer id, Long msg_id, String title,
String content, String activity, int notificationActionType, String update_time) {
super();
this.id = id;
this.msg_id = msg_id;
this.title = title;
this.content = content;
this.activity = activity;
this.notificationActionType = notificationActionType;
this.update_time = update_time;
}}
最后再mainactivity中:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//开启信鸽的日志输出,线上版本不建议调用
XGPushConfig.enableDebug(this, true);
//注册数据更新监听器
MessageReceiver receiver = new MessageReceiver();
IntentFilter intentFilter = new IntentFilter();
//这里需要改自己的包名 后面的UPDATE_LISTVIEW 不改
intentFilter.addAction("com.bw.wowotou.UPDATE_LISTVIEW");
registerReceiver(receiver, intentFilter);
//注册获取token
XGPushManager.registerPush(this, new XGIOperateCallback() {
@Override
public void onSuccess(Object o, int i) {
Log.i("xxx",o.toString());
//通过网络请求给后台
}
@Override
public void onFail(Object o, int i, String s) {
}
});
}
}
然后创建推送
这里需要获取一个token值,前面我们在代码中已经获取过一个token值,复制下来填到刚刚那个框框里。