cordova-plugin-tpns/src/android/TpnsReceiver.java

126 lines
4.7 KiB
Java
Raw Normal View History

// ~ Copyright (c) 2023 Beijing Shuto Technology Co,. Ltd. All rights reserved.
package com.shuto.plugin.tpns;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import com.tencent.android.tpush.NotificationAction;
import com.tencent.android.tpush.XGPushBaseReceiver;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushRegisterResult;
import com.tencent.android.tpush.XGPushShowedResult;
import com.tencent.android.tpush.XGPushTextMessage;
import org.json.JSONException;
import org.json.JSONObject;
public abstract class TpnsReceiver extends XGPushBaseReceiver {
private static final String TAG = TpnsPlugin.class.getSimpleName();
/**
* 消息透传处理
* @param context
* @param message 解析自定义的 JSON
*/
@Override
public void onTextMessage(Context context, XGPushTextMessage message) {
String text = "收到消息:" + message.toString();
// APP自主处理消息的过程...
JSONObject obj = null;
if(message !=null){
try{
obj = new JSONObject();
obj.put("content",message.getContent());
obj.put("customContent",message.getCustomContent());
obj.put("msgId",message.getMsgId());
obj.put("channel",message.getPushChannel());
obj.put("templateId",message.getTemplateId());
obj.put("title",message.getTitle());
obj.put("traceId",message.getTraceId());
}catch (JSONException e){
Log.e(TAG,e.getLocalizedMessage());
}
}
CordovaEventKit.kit.fireEvent("onTextMessage",obj);
Log.d(TAG, text);
}
@Override
public void onNotificationShowedResult(Context context, XGPushShowedResult notifiShowedRlt){
JSONObject obj = null;
if(notifiShowedRlt !=null){
try{
obj = new JSONObject();
obj.put("activity",notifiShowedRlt.getActivity());
obj.put("content",notifiShowedRlt.getContent());
obj.put("customContent",notifiShowedRlt.getCustomContent());
obj.put("msgId",notifiShowedRlt.getMsgId());
obj.put("notifactionId",notifiShowedRlt.getNotifactionId());
obj.put("actionType",notifiShowedRlt.getNotificationActionType());
obj.put("channel",notifiShowedRlt.getPushChannel());
obj.put("templateId",notifiShowedRlt.getTemplateId());
obj.put("title",notifiShowedRlt.getTitle());
obj.put("traceId",notifiShowedRlt.getTraceId());
}catch (JSONException e){
Log.e(TAG,e.getLocalizedMessage());
}
}
CordovaEventKit.kit.fireEvent("onNotificationShowedResult",obj);
}
// 通知点击回调actionType=0 为该消息被点击actionType=2 为该消息被清除
@Override
public 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;
}
JSONObject obj = null;
if(message !=null){
try{
obj = new JSONObject();
obj.put("activity",message.getActivityName());
obj.put("content",message.getContent());
obj.put("customContent",message.getCustomContent());
obj.put("msgId",message.getMsgId());
obj.put("actionType",message.getNotificationActionType());
obj.put("channel",message.getPushChannel());
obj.put("templateId",message.getTemplateId());
obj.put("title",message.getTitle());
obj.put("traceId",message.getTraceId());
}catch (JSONException e){
Log.e(TAG,e.getLocalizedMessage());
}
}
CordovaEventKit.kit.fireEvent("onNotificationClickedResult",obj);
// APP自主处理的过程。
Log.d(TAG, "广播接收到通知:" + text);
}
@Override
public void onQueryTagsResult(Context context, int errorCode, String data, String operateName) {
Log.i(TAG, "action - onQueryTagsResult, errorCode:" + errorCode + ", operateName:" + operateName + ", data: " + data);
Events.fireEvent("queryTags." + operateName, new Events.Extra() {
@Override
public void extra(Bundle extra) {
extra.putInt("code",errorCode);
extra.putString("data",data);
}
});
}
}