新增:Gotify发送通道(自主推送通知服务)

This commit is contained in:
pppscn 2022-01-21 16:12:49 +08:00
parent 43922b28ce
commit 8d7797b43e
7 changed files with 84 additions and 62 deletions

View File

@ -113,6 +113,10 @@ public class PhoneStateReceiver extends BroadcastReceiver {
SmsVo smsVo = new SmsVo(phoneNumber, name + context.getString(R.string.calling), new Date(), simInfo);
Log.d(TAG, "send_msg" + smsVo.toString());
SendUtil.send_msg(context, smsVo, simId, "call");
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.phone, simId, name + context.getString(R.string.calling), phoneNumber));
//SmsHubApi
if (SettingUtil.getSwitchEnableSmsHubApi()) {
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.phone, simId, name + context.getString(R.string.calling), phoneNumber));
}
}
}

View File

@ -8,11 +8,14 @@ import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import androidx.annotation.RequiresApi;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.model.vo.SmsVo;
import com.idormy.sms.forwarder.sender.SendUtil;
import com.idormy.sms.forwarder.utils.*;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SimUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import java.util.ArrayList;
import java.util.Date;
@ -79,14 +82,22 @@ public class SmsBroadcastReceiver extends BroadcastReceiver {
mobileToContent.put(mobile, content);
}
List<SmsHubVo> smsHubVos = new ArrayList<>();
for (String mobile : mobileToContent.keySet()) {
smsVoList.add(new SmsVo(mobile, mobileToContent.get(mobile), date, simInfo));
smsHubVos.add(new SmsHubVo(SmsHubVo.Type.sms, simId, mobileToContent.get(mobile), mobile));
}
SmsHubActionHandler.putData(smsHubVos.toArray(new SmsHubVo[0]));
Log.d(TAG, "短信:" + smsVoList);
SendUtil.send_msg_list(context, smsVoList, simId, "sms");
//SmsHubApi
if (SettingUtil.getSwitchEnableSmsHubApi()) {
List<SmsHubVo> smsHubVos = new ArrayList<>();
for (String mobile : mobileToContent.keySet()) {
smsHubVos.add(new SmsHubVo(SmsHubVo.Type.sms, simId, mobileToContent.get(mobile), mobile));
}
SmsHubActionHandler.putData(smsHubVos.toArray(new SmsHubVo[0]));
}
}
} catch (Throwable throwable) {

View File

@ -5,6 +5,7 @@ import static com.idormy.sms.forwarder.model.SenderModel.TYPE_BARK;
import static com.idormy.sms.forwarder.model.SenderModel.TYPE_DINGDING;
import static com.idormy.sms.forwarder.model.SenderModel.TYPE_EMAIL;
import static com.idormy.sms.forwarder.model.SenderModel.TYPE_FEISHU;
import static com.idormy.sms.forwarder.model.SenderModel.TYPE_GOTIFY;
import static com.idormy.sms.forwarder.model.SenderModel.TYPE_PUSHPLUS;
import static com.idormy.sms.forwarder.model.SenderModel.TYPE_QYWX_APP;
import static com.idormy.sms.forwarder.model.SenderModel.TYPE_QYWX_GROUP_ROBOT;
@ -26,6 +27,7 @@ import com.idormy.sms.forwarder.model.vo.BarkSettingVo;
import com.idormy.sms.forwarder.model.vo.DingDingSettingVo;
import com.idormy.sms.forwarder.model.vo.EmailSettingVo;
import com.idormy.sms.forwarder.model.vo.FeiShuSettingVo;
import com.idormy.sms.forwarder.model.vo.GotifySettingVo;
import com.idormy.sms.forwarder.model.vo.LogVo;
import com.idormy.sms.forwarder.model.vo.PushPlusSettingVo;
import com.idormy.sms.forwarder.model.vo.QYWXAppSettingVo;
@ -63,6 +65,7 @@ public class SendUtil {
String key = "SIM" + simId;
List<RuleModel> ruleList = RuleUtil.getRule(null, key, type);
if (!ruleList.isEmpty()) {
Log.d(TAG, ruleList.toString());
SenderUtil.init(context);
for (RuleModel ruleModel : ruleList) {
//规则匹配发现需要发送
@ -356,6 +359,21 @@ public class SendUtil {
}
break;
case TYPE_GOTIFY:
//try phrase json setting
if (senderModel.getJsonSetting() != null) {
GotifySettingVo gotifySettingVo = JSON.parseObject(senderModel.getJsonSetting(), GotifySettingVo.class);
if (gotifySettingVo != null) {
try {
SenderGotifyMsg.sendMsg(logId, handError, gotifySettingVo, smsVo.getMobile(), smsVo.getSmsVoForSend(smsTemplate, regexReplace));
} catch (Exception e) {
LogUtil.updateLog(logId, 0, e.getMessage());
Log.e(TAG, "senderSendMsg: gotify error " + e.getMessage());
}
}
}
break;
default:
break;
}

View File

@ -7,14 +7,10 @@ import androidx.annotation.NonNull;
import com.idormy.sms.forwarder.model.vo.GotifySettingVo;
import com.idormy.sms.forwarder.utils.LogUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
@ -23,7 +19,6 @@ import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
@SuppressWarnings({"ResultOfMethodCallIgnored"})
public class SenderGotifyMsg extends SenderBaseMsg {
static final String TAG = "SenderGotifyMsg";
@ -33,62 +28,40 @@ public class SenderGotifyMsg extends SenderBaseMsg {
//具体消息内容
if (message == null || message.isEmpty()) return;
final RequestBody formBody = new FormBody.Builder()
RequestBody formBody = new FormBody.Builder()
.add("title", title)
.add("message", message)
.add("priority", gotifySettingVo.getPriority())
.build();
final String requestUrl = gotifySettingVo.getWebServer();
String requestUrl = gotifySettingVo.getWebServer();
Log.i(TAG, "requestUrl:" + requestUrl);
Observable
.create((ObservableEmitter<Object> emitter) -> {
Toast(handError, TAG, "开始请求接口...");
Request request = new Request.Builder().url(requestUrl).post(formBody).build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
LogUtil.updateLog(logId, 0, e.getMessage());
Toast(handError, TAG, "发送失败:" + e.getMessage());
}
final Request request = new Request.Builder()
.url(requestUrl)
.post(formBody)
.build();
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
final String responseStr = Objects.requireNonNull(response.body()).string();
Log.d(TAG, "Response" + response.code() + "" + responseStr);
Toast(handError, TAG, "发送状态:" + responseStr);
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
LogUtil.updateLog(logId, 0, e.getMessage());
Toast(handError, TAG, "发送失败:" + e.getMessage());
emitter.onError(new RuntimeException("请求接口异常..."));
}
//TODO:粗略解析是否发送成功
if (response.code() == 200) {
LogUtil.updateLog(logId, 2, responseStr);
} else {
LogUtil.updateLog(logId, 1, responseStr);
}
}
});
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
final String responseStr = Objects.requireNonNull(response.body()).string();
Log.d(TAG, "Response" + response.code() + "" + responseStr);
Toast(handError, TAG, "发送状态:" + responseStr);
//TODO:粗略解析是否发送成功
if (response.code() == 200) {
LogUtil.updateLog(logId, 2, responseStr);
} else {
LogUtil.updateLog(logId, 0, responseStr);
}
}
});
}).retryWhen((Observable<Throwable> errorObservable) -> errorObservable
.zipWith(Observable.just(
SettingUtil.getRetryDelayTime(1),
SettingUtil.getRetryDelayTime(2),
SettingUtil.getRetryDelayTime(3),
SettingUtil.getRetryDelayTime(4),
SettingUtil.getRetryDelayTime(5)
), (Throwable e, Integer time) -> time)
.flatMap((Integer delay) -> {
Toast(handError, TAG, "请求接口异常," + delay + "秒后重试");
return Observable.timer(delay, TimeUnit.SECONDS);
}))
.subscribe(System.out::println);
}
}

View File

@ -6,12 +6,18 @@ import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.utils.*;
import com.idormy.sms.forwarder.utils.HttpUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import java.util.*;
import java.util.List;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
/**
* 主动发送短信轮询任务
*
* @author xxc
* 2022/1/10 9:53
*/

View File

@ -178,7 +178,11 @@ public class BatteryService extends Service {
SmsVo smsVo = new SmsVo("88888888", msg, new Date(), "电池状态监听");
Log.d(TAG, "send_msg" + smsVo.toString());
SendUtil.send_msg(context, smsVo, 1, "app");
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.phone, null, msg, "电池状态监听"));
//SmsHubApi
if (SettingUtil.getSwitchEnableSmsHubApi()) {
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.phone, null, msg, "电池状态监听"));
}
} catch (Exception e) {
Log.e(TAG, "getLog e:" + e.getMessage());
}

View File

@ -11,7 +11,9 @@ import androidx.annotation.RequiresApi;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.model.vo.SmsVo;
import com.idormy.sms.forwarder.sender.SendUtil;
import com.idormy.sms.forwarder.utils.*;
import com.idormy.sms.forwarder.utils.CommonUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -85,7 +87,11 @@ public class NotifyService extends NotificationListenerService {
SmsVo smsVo = new SmsVo(packageName, text, new Date(), title);
Log.d(TAG, "send_msg" + smsVo.toString());
SendUtil.send_msg(this, smsVo, 1, "app");
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.app, null, text, packageName));
//SmsHubApi
if (SettingUtil.getSwitchEnableSmsHubApi()) {
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.app, null, text, packageName));
}
} catch (Exception e) {
Log.e(TAG, "onNotificationPosted:", e);
}