473 lines
20 KiB
Plaintext
473 lines
20 KiB
Plaintext
/**
|
|
* PushMessageReceiver.uts
|
|
* 极光推送消息接收器,继承自JPushMessageReceiver
|
|
*/
|
|
|
|
import Context from 'android.content.Context';
|
|
import Intent from 'android.content.Intent';
|
|
import Log from 'android.util.Log';
|
|
import Bundle from 'android.os.Bundle';
|
|
import ArrayList from 'java.util.ArrayList';
|
|
|
|
import CmdMessage from 'cn.jpush.android.api.CmdMessage';
|
|
import CustomMessage from 'cn.jpush.android.api.CustomMessage';
|
|
import JPushMessage from 'cn.jpush.android.api.JPushMessage';
|
|
import NotificationMessage from 'cn.jpush.android.api.NotificationMessage';
|
|
import VoipDataMessage from 'cn.jpush.android.api.VoipDataMessage';
|
|
import JPushMessageReceiver from 'cn.jpush.android.service.JPushMessageReceiver';
|
|
import JSONObject from 'org.json.JSONObject';
|
|
|
|
// 导入事件回调管理器
|
|
import { eventCallbackManager } from './index.uts';
|
|
// 导入 EventCallBack 类型
|
|
import type { EventCallBack } from '../interface.uts';
|
|
|
|
/**
|
|
* 推送消息接收器类
|
|
* 继承自极光推送的JPushMessageReceiver
|
|
*/
|
|
export class PushMessageReceiver extends JPushMessageReceiver {
|
|
private static readonly TAG: string = "U-PushMessageReceiver";
|
|
|
|
constructor() {
|
|
super();
|
|
Log.e(PushMessageReceiver.TAG, "PushMessageReceiver constructor called");
|
|
}
|
|
|
|
/**
|
|
* 将JPushMessage转换为JSON字符串
|
|
*/
|
|
private convertJPushMessageToJson(jPushMessage: JPushMessage): string {
|
|
try {
|
|
const jsonObject = new JSONObject();
|
|
jsonObject.put("alias", jPushMessage.getAlias());
|
|
jsonObject.put("tags", jPushMessage.getTags());
|
|
jsonObject.put("pros", jPushMessage.getPros());
|
|
jsonObject.put("checkTag", jPushMessage.getCheckTag());
|
|
jsonObject.put("errorCode", jPushMessage.getErrorCode());
|
|
jsonObject.put("tagCheckStateResult", jPushMessage.getTagCheckStateResult());
|
|
jsonObject.put("isTagCheckOperator", jPushMessage.isTagCheckOperator());
|
|
jsonObject.put("sequence", jPushMessage.getSequence());
|
|
jsonObject.put("mobileNumber", jPushMessage.getMobileNumber());
|
|
jsonObject.put("protoType", jPushMessage.getProtoType());
|
|
jsonObject.put("action", jPushMessage.getAction());
|
|
return jsonObject.toString();
|
|
} catch (e) {
|
|
Log.e(PushMessageReceiver.TAG, "convertJPushMessageToJson error: " + e.toString());
|
|
return "{}";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 将CustomMessage转换为JSON字符串
|
|
*/
|
|
private convertCustomMessageToJson(customMessage: CustomMessage): string {
|
|
try {
|
|
const jsonObject = new JSONObject();
|
|
jsonObject.put("messageId", customMessage.messageId);
|
|
jsonObject.put("extra", customMessage.extra);
|
|
jsonObject.put("message", customMessage.message);
|
|
jsonObject.put("contentType", customMessage.contentType);
|
|
jsonObject.put("title", customMessage.title);
|
|
jsonObject.put("senderId", customMessage.senderId);
|
|
jsonObject.put("appId", customMessage.appId);
|
|
jsonObject.put("platform", customMessage.platform);
|
|
return jsonObject.toString();
|
|
} catch (e) {
|
|
Log.e(PushMessageReceiver.TAG, "convertCustomMessageToJson error: " + e.toString());
|
|
return "{}";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 将NotificationMessage转换为JSON字符串
|
|
*/
|
|
private convertNotificationMessageToJson(message: NotificationMessage): string {
|
|
try {
|
|
const jsonObject = new JSONObject();
|
|
jsonObject.put("appkey", message.appkey);
|
|
jsonObject.put("msgId", message.msgId);
|
|
jsonObject.put("notificationContent", message.notificationContent);
|
|
jsonObject.put("notificationAlertType", message.notificationAlertType);
|
|
jsonObject.put("notificationTitle", message.notificationTitle);
|
|
jsonObject.put("notificationSmallIcon", message.notificationSmallIcon);
|
|
jsonObject.put("notificationLargeIcon", message.notificationLargeIcon);
|
|
jsonObject.put("notificationExtras", message.notificationExtras);
|
|
jsonObject.put("notificationStyle", message.notificationStyle);
|
|
jsonObject.put("notificationBuilderId", message.notificationBuilderId);
|
|
jsonObject.put("notificationBigText", message.notificationBigText);
|
|
jsonObject.put("notificationBigPicPath", message.notificationBigPicPath);
|
|
jsonObject.put("notificationInbox", message.notificationInbox);
|
|
jsonObject.put("notificationPriority", message.notificationPriority);
|
|
jsonObject.put("notificationImportance", message.notificationImportance);
|
|
jsonObject.put("notificationCategory", message.notificationCategory);
|
|
jsonObject.put("notificationId", message.notificationId);
|
|
jsonObject.put("developerArg0", message.developerArg0);
|
|
jsonObject.put("platform", message.platform);
|
|
jsonObject.put("appId", message.appId);
|
|
jsonObject.put("notificationType", message.notificationType);
|
|
jsonObject.put("notificationChannelId", message.notificationChannelId);
|
|
jsonObject.put("displayForeground", message.displayForeground);
|
|
jsonObject.put("_webPagePath", message._webPagePath);
|
|
jsonObject.put("isRichPush", message.isRichPush);
|
|
jsonObject.put("richType", message.richType);
|
|
jsonObject.put("deeplink", message.deeplink);
|
|
jsonObject.put("failedAction", message.failedAction);
|
|
jsonObject.put("failedLink", message.failedLink);
|
|
jsonObject.put("targetPkgName", message.targetPkgName);
|
|
jsonObject.put("sspWxAppId", message.sspWxAppId);
|
|
jsonObject.put("sspWmOriginId", message.sspWmOriginId);
|
|
jsonObject.put("sspWmType", message.sspWmType);
|
|
jsonObject.put("isWmDeepLink", message.isWmDeepLink);
|
|
jsonObject.put("inAppMsgType", message.inAppMsgType);
|
|
jsonObject.put("inAppMsgShowType", message.inAppMsgShowType);
|
|
jsonObject.put("inAppMsgShowPos", message.inAppMsgShowPos);
|
|
jsonObject.put("inAppMsgTitle", message.inAppMsgTitle);
|
|
jsonObject.put("inAppMsgContentBody", message.inAppMsgContentBody);
|
|
jsonObject.put("inAppType", message.inAppType);
|
|
jsonObject.put("inAppShowTarget", message.inAppShowTarget);
|
|
jsonObject.put("inAppClickAction", message.inAppClickAction);
|
|
jsonObject.put("inAppExtras", message.inAppExtras);
|
|
return jsonObject.toString();
|
|
} catch (e) {
|
|
Log.e(PushMessageReceiver.TAG, "convertNotificationMessageToJson error: " + e.toString());
|
|
return "{}";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 将CmdMessage转换为JSON字符串
|
|
*/
|
|
private convertCmdMessageToJson(cmdMessage: CmdMessage): string {
|
|
try {
|
|
const jsonObject = new JSONObject();
|
|
jsonObject.put("cmd", cmdMessage.cmd);
|
|
jsonObject.put("errorCode", cmdMessage.errorCode);
|
|
jsonObject.put("msg", cmdMessage.msg);
|
|
if (cmdMessage.cmd == 2013) {
|
|
const typedExtra = SubscriptionResultCodec.fromBundle(cmdMessage.extra);
|
|
jsonObject.put("extra", typedExtra);
|
|
jsonObject.put("commandData", typedExtra);
|
|
if (typedExtra.has("channels")) jsonObject.put("channels", typedExtra.get("channels"));
|
|
} else if (cmdMessage.cmd == 2012) {
|
|
// 小米订阅消息需要向 UTS 层透传结构化的 channel 操作结果
|
|
jsonObject.put("extra", this.convertBundleToJson(cmdMessage.extra));
|
|
} else {
|
|
// 保持历史命令的 extra 序列化行为,避免改变既有回调语义
|
|
jsonObject.put("extra", cmdMessage.extra);
|
|
}
|
|
return jsonObject.toString();
|
|
} catch (e) {
|
|
Log.e(PushMessageReceiver.TAG, "convertCmdMessageToJson error: " + e.toString());
|
|
return "{}";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 将Bundle转换为JSONObject
|
|
*/
|
|
private convertBundleToJson(bundle: Bundle | null): JSONObject {
|
|
try {
|
|
const jsonObject = new JSONObject();
|
|
if (bundle != null) {
|
|
// 使用 ArrayList 来收集 Bundle 的键,避免 iterator() 歧义
|
|
const keySet = bundle.keySet();
|
|
const keyList = new ArrayList<string>(keySet);
|
|
// 在 Kotlin 中 size 是属性,不是方法,所以直接访问
|
|
const size: Int = keyList.size;
|
|
for (let i: Int = 0; i < size; i++) {
|
|
const key = keyList.get(i);
|
|
jsonObject.put(key, bundle.get(key));
|
|
}
|
|
}
|
|
return jsonObject;
|
|
} catch (e) {
|
|
Log.e(PushMessageReceiver.TAG, "convertBundleToJson error: " + e.toString());
|
|
return new JSONObject();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 触发事件回调的通用方法
|
|
*/
|
|
private triggerEventCallback(eventName: string, eventData: string): void {
|
|
const event: EventCallBack = {
|
|
eventName: eventName,
|
|
eventData: eventData
|
|
};
|
|
eventCallbackManager.triggerCallBack(event);
|
|
}
|
|
|
|
/**
|
|
* 收到自定义消息
|
|
*/
|
|
override onMessage(context: Context, customMessage: CustomMessage): void {
|
|
super.onMessage(context, customMessage);
|
|
Log.e(PushMessageReceiver.TAG, "[onMessage] ");
|
|
|
|
// 触发自定义消息回调
|
|
this.triggerEventCallback('onCustomMessage', this.convertCustomMessageToJson(customMessage));
|
|
}
|
|
|
|
/**
|
|
* 属性操作结果回调
|
|
*/
|
|
override onPropertyOperatorResult(context: Context, jPushMessage: JPushMessage): void {
|
|
super.onPropertyOperatorResult(context, jPushMessage);
|
|
Log.e(PushMessageReceiver.TAG, "[onPropertyOperatorResult] ");
|
|
|
|
// 触发属性操作结果回调
|
|
this.triggerEventCallback('onPropertyOperatorResult', this.convertJPushMessageToJson(jPushMessage));
|
|
}
|
|
|
|
/**
|
|
* 通知点击回调
|
|
*/
|
|
override onNotifyMessageOpened(context: Context, message: NotificationMessage): void {
|
|
Log.e(PushMessageReceiver.TAG, "[onNotifyMessageOpened] ");
|
|
super.onNotifyMessageOpened(context, message);
|
|
|
|
// 触发通知点击回调
|
|
this.triggerEventCallback('onClickMessage', this.convertNotificationMessageToJson(message));
|
|
|
|
// 官方接收器只分发事件。先缓存点击,再通过系统入口恢复现有任务或冷启动应用。
|
|
const launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
|
|
if (launchIntent != null) {
|
|
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
|
|
context.startActivity(launchIntent);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 通知展示回调
|
|
*/
|
|
override onNotifyMessageArrived(context: Context, message: NotificationMessage): void {
|
|
Log.e(PushMessageReceiver.TAG, "[onNotifyMessageArrived] ");
|
|
super.onNotifyMessageArrived(context, message);
|
|
|
|
// 触发通知展示回调
|
|
this.triggerEventCallback('onNotifyMessageArrived', this.convertNotificationMessageToJson(message));
|
|
}
|
|
|
|
/**
|
|
* 通知被清除回调
|
|
*/
|
|
override onNotifyMessageDismiss(context: Context, message: NotificationMessage): void {
|
|
super.onNotifyMessageDismiss(context, message);
|
|
Log.e(PushMessageReceiver.TAG, "[onNotifyMessageDismiss] ");
|
|
|
|
// 触发通知被清除回调
|
|
this.triggerEventCallback('onNotifyMessageDismiss', this.convertNotificationMessageToJson(message));
|
|
}
|
|
|
|
/**
|
|
* 注册ID回调
|
|
*/
|
|
override onRegister(context: Context, registrationId: string): void {
|
|
super.onRegister(context, registrationId);
|
|
Log.e(PushMessageReceiver.TAG, "[onRegister] " + registrationId);
|
|
|
|
// 触发注册回调
|
|
this.triggerEventCallback('onRegister', registrationId);
|
|
}
|
|
|
|
/**
|
|
* 连接状态回调
|
|
*/
|
|
override onConnected(context: Context, isConnected: boolean): void {
|
|
super.onConnected(context, isConnected);
|
|
Log.e(PushMessageReceiver.TAG, "[onConnected] " + isConnected);
|
|
|
|
// 触发连接状态回调
|
|
this.triggerEventCallback('onConnected', isConnected.toString());
|
|
}
|
|
|
|
/**
|
|
* 命令结果回调
|
|
*/
|
|
override onCommandResult(context: Context, cmdMessage: CmdMessage): void {
|
|
super.onCommandResult(context, cmdMessage);
|
|
Log.e(PushMessageReceiver.TAG, "[onCommandResult] ");
|
|
|
|
// 触发命令结果回调
|
|
this.triggerEventCallback('onCommandResult', this.convertCmdMessageToJson(cmdMessage));
|
|
}
|
|
|
|
/**
|
|
* 标签操作结果回调
|
|
*/
|
|
override onTagOperatorResult(context: Context, jPushMessage: JPushMessage): void {
|
|
Log.e(PushMessageReceiver.TAG, "[onTagOperatorResult] ");
|
|
// TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage);
|
|
super.onTagOperatorResult(context, jPushMessage);
|
|
|
|
// 触发标签操作结果回调
|
|
this.triggerEventCallback('onTagOperatorResult', this.convertJPushMessageToJson(jPushMessage));
|
|
}
|
|
|
|
/**
|
|
* 检查标签操作结果回调
|
|
*/
|
|
override onCheckTagOperatorResult(context: Context, jPushMessage: JPushMessage): void {
|
|
Log.e(PushMessageReceiver.TAG, "[onCheckTagOperatorResult] ");
|
|
// TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage);
|
|
super.onCheckTagOperatorResult(context, jPushMessage);
|
|
|
|
// 触发检查标签操作结果回调
|
|
this.triggerEventCallback('onTagOperatorResult', this.convertJPushMessageToJson(jPushMessage));
|
|
}
|
|
|
|
/**
|
|
* 别名操作结果回调
|
|
*/
|
|
override onAliasOperatorResult(context: Context, jPushMessage: JPushMessage): void {
|
|
Log.e(PushMessageReceiver.TAG, "[onAliasOperatorResult] ");
|
|
// TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context, jPushMessage);
|
|
super.onAliasOperatorResult(context, jPushMessage);
|
|
|
|
// 触发别名操作结果回调
|
|
this.triggerEventCallback('onAliasOperatorResult', this.convertJPushMessageToJson(jPushMessage));
|
|
}
|
|
|
|
/**
|
|
* 手机号操作结果回调
|
|
*/
|
|
override onMobileNumberOperatorResult(context: Context, jPushMessage: JPushMessage): void {
|
|
super.onMobileNumberOperatorResult(context, jPushMessage);
|
|
Log.e(PushMessageReceiver.TAG, "[onMobileNumberOperatorResult] ");
|
|
// TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage);
|
|
|
|
// 触发手机号操作结果回调
|
|
this.triggerEventCallback('onMobileNumberOperatorResult', this.convertJPushMessageToJson(jPushMessage));
|
|
}
|
|
|
|
/**
|
|
* 通知设置检查回调
|
|
*/
|
|
override onNotificationSettingsCheck(context: Context, isOn: boolean, source: Int): void {
|
|
super.onNotificationSettingsCheck(context, isOn, source);
|
|
Log.e(PushMessageReceiver.TAG, "[onNotificationSettingsCheck] isOn:" + isOn + ",source:" + source);
|
|
|
|
// 触发通知设置检查回调
|
|
try {
|
|
const jsonObject = new JSONObject();
|
|
jsonObject.put("isOn", isOn);
|
|
jsonObject.put("source", source);
|
|
this.triggerEventCallback('onNotificationSettingsCheck', jsonObject.toString());
|
|
} catch (e) {
|
|
Log.e(PushMessageReceiver.TAG, "onNotificationSettingsCheck JSON error: " + e.toString());
|
|
this.triggerEventCallback('onNotificationSettingsCheck', '{"isOn":false,"source":0}');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 是否需要显示通知
|
|
*/
|
|
override isNeedShowNotification(context: Context, notificationMessage: NotificationMessage, processName: string): boolean {
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 通知未显示回调
|
|
*/
|
|
override onNotifyMessageUnShow(context: Context, message: NotificationMessage): void {
|
|
super.onNotifyMessageUnShow(context, message);
|
|
Log.e(PushMessageReceiver.TAG, "[onNotifyMessageUnShow] message:");
|
|
|
|
// 触发通知未显示回调
|
|
this.triggerEventCallback('onNotifyMessageUnShow', this.convertNotificationMessageToJson(message));
|
|
}
|
|
|
|
/**
|
|
* 应用内消息展示回调
|
|
*/
|
|
override onInAppMessageShow(context: Context, message: NotificationMessage): void {
|
|
super.onInAppMessageShow(context, message);
|
|
Log.e(PushMessageReceiver.TAG, "[onInAppMessageShow], ");
|
|
|
|
// 触发应用内消息展示回调
|
|
this.triggerEventCallback('onInAppMessageShow', this.convertNotificationMessageToJson(message));
|
|
}
|
|
|
|
/**
|
|
* 应用内消息点击回调
|
|
*/
|
|
override onInAppMessageClick(context: Context, message: NotificationMessage): void {
|
|
super.onInAppMessageClick(context, message);
|
|
Log.e(PushMessageReceiver.TAG, "[onInAppMessageClick], ");
|
|
|
|
// 触发应用内消息点击回调
|
|
this.triggerEventCallback('onInAppMessageClick', this.convertNotificationMessageToJson(message));
|
|
}
|
|
|
|
/**
|
|
* 拉取地理围栏列表的回调
|
|
*/
|
|
override onGeofenceReceived(context: Context, geofences: string): void {
|
|
super.onGeofenceReceived(context, geofences);
|
|
Log.e(PushMessageReceiver.TAG, "onGeofenceReceived: " + geofences);
|
|
|
|
// 触发地理围栏列表回调
|
|
this.triggerEventCallback('onGeofenceReceived', geofences);
|
|
}
|
|
|
|
/**
|
|
* 多操作按钮点击回调
|
|
*/
|
|
override onMultiActionClicked(context: Context, intent: Intent): void {
|
|
super.onMultiActionClicked(context, intent);
|
|
Log.e(PushMessageReceiver.TAG, "[onMultiActionClicked] 用户点击了通知栏按钮");
|
|
|
|
// 触发多操作按钮点击回调
|
|
try {
|
|
const jsonObject = new JSONObject();
|
|
jsonObject.put("action", intent.getAction());
|
|
jsonObject.put("extras", this.convertBundleToJson(intent.getExtras()));
|
|
this.triggerEventCallback('onMultiActionClicked', jsonObject.toString());
|
|
} catch (e) {
|
|
Log.e(PushMessageReceiver.TAG, "onMultiActionClicked JSON error: " + e.toString());
|
|
this.triggerEventCallback('onMultiActionClicked', '{"action":"","extras":{}}');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 触发地理围栏回调
|
|
*/
|
|
override onGeofenceRegion(context: Context, geofence: string, longitude: Double, latitude: Double): void {
|
|
super.onGeofenceRegion(context, geofence, longitude, latitude);
|
|
Log.e(PushMessageReceiver.TAG, "onGeofenceRegion: " + geofence);
|
|
|
|
// 触发地理围栏回调
|
|
try {
|
|
const jsonObject = new JSONObject();
|
|
jsonObject.put("geofence", geofence);
|
|
jsonObject.put("longitude", longitude);
|
|
jsonObject.put("latitude", latitude);
|
|
this.triggerEventCallback('onGeofenceRegion', jsonObject.toString());
|
|
} catch (e) {
|
|
Log.e(PushMessageReceiver.TAG, "onGeofenceRegion JSON error: " + e.toString());
|
|
this.triggerEventCallback('onGeofenceRegion', '{"geofence":"","longitude":0,"latitude":0}');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* VOIP 透传消息回调(小米/荣耀/OPPO/VIVO 厂商通道)
|
|
*/
|
|
override onVoipMessage(context: Context, voipDataMessage: VoipDataMessage): void {
|
|
super.onVoipMessage(context, voipDataMessage);
|
|
Log.e(PushMessageReceiver.TAG, "[onVoipMessage] ");
|
|
|
|
try {
|
|
const jsonObject = new JSONObject();
|
|
jsonObject.put("messageId", voipDataMessage.getMessageId());
|
|
jsonObject.put("extraData", voipDataMessage.getExtraData());
|
|
jsonObject.put("platform", voipDataMessage.getPlatform());
|
|
jsonObject.put("voipSupportStatus", voipDataMessage.getVoipSupportStatus());
|
|
this.triggerEventCallback('onVoipMessage', jsonObject.toString());
|
|
} catch (e) {
|
|
Log.e(PushMessageReceiver.TAG, "onVoipMessage JSON error: " + e.toString());
|
|
this.triggerEventCallback('onVoipMessage', '{"messageId":"","extraData":"","platform":0}');
|
|
}
|
|
}
|
|
}
|