Commit 58d76642 authored by leichao.gao's avatar leichao.gao

支持延长通知停留时间

parent 2b50c48c
......@@ -24,6 +24,7 @@ import java.util.UUID
class MyApplication : BaseApplication() {
companion object {
@JvmField
var PAUSED_VALUE = 0
}
......
......@@ -10,6 +10,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isVisible
import com.test.easy.easycleanerjunk.databinding.ActivityLayoutSplashBinding
import com.test.easy.easycleanerjunk.fcm.CloseNotificationReceiver
import com.test.easy.easycleanerjunk.fcm.NotificationUtil
import com.test.easy.easycleanerjunk.helps.BaseActivity
import com.test.easy.easycleanerjunk.helps.ConfigHelper
import com.test.easy.easycleanerjunk.helps.ads.AdmobUtils
......@@ -47,6 +48,8 @@ class NewSplashActivity : BaseActivity<ActivityLayoutSplashBinding>(),
cancelIntent.action = CloseNotificationReceiver.action
cancelIntent.putExtra(CloseNotificationReceiver.id, jumpType)
sendBroadcast(cancelIntent)
}else{
NotificationUtil.stopNotificationHandler()
}
mProgressManager = ProgressManager(binding, this)
......
......@@ -14,6 +14,7 @@ public class CloseNotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
NotificationUtil.stopNotificationHandler();
if (action != null && action.equals("CANCEL_NOTIFICATION")) {
int notificationId = intent.getIntExtra("NOTIFICATION_ID", 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
......
......@@ -21,6 +21,9 @@ public class FcmService extends FirebaseMessagingService {
int lockS = remoteMessage.getData().get("lockS") != null ? Integer.parseInt(remoteMessage.getData().get("lockS")) : 0;
SPUtils.getInstance().put("actionS",actionS);
SPUtils.getInstance().put("open",open);
SPUtils.getInstance().put("num",num);
SPUtils.getInstance().put("delay",delay);
NotificationUtil.sendNotification(MyApplication.context);
......
......@@ -7,10 +7,13 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.widget.RemoteViews;
import androidx.core.app.NotificationCompat;
import com.test.easy.easycleanerjunk.MyApplication;
import com.test.easy.easycleanerjunk.R;
import com.test.easy.easycleanerjunk.activity.splash.NewSplashActivity;
import com.test.easy.easycleanerjunk.bean.ConfigBean;
......@@ -41,12 +44,18 @@ public class NotificationUtil {
// Create an Intent for the activity you want to open when the notification is clicked
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent deleteIntent = new Intent(context, CloseNotificationReceiver.class);
deleteIntent.setAction("DELETE_NOTIFICATION");
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context, 0, deleteIntent, PendingIntent.FLAG_MUTABLE);
// Create the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel_id")
.setSmallIcon(R.drawable.easycleanjunk_noticeicon)
.setContentTitle("title")
.setContentText("message")
.setContentIntent(pendingIntent)
.setDeleteIntent(deletePendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContent(remoteViews)
.setCustomHeadsUpContentView(remoteViews)
......@@ -54,21 +63,68 @@ public class NotificationUtil {
.setCustomContentView(remoteViews)
.setAutoCancel(true);
// Show the notification
if (SPUtils.getInstance().getInt("actionS",0) == 1) {
notificationManager.notify(intent.getIntExtra("actionId",0), builder.build());
if (SPUtils.getInstance().getInt("actionS", 0) == 1) {
notificationManager.notify(intent.getIntExtra("actionId", 0), builder.build());
} else {
notificationManager.notify(0, builder.build());
}
}
private static HandlerThread handlerThread;
private static Handler handler;
public static void sendNotification(Context context) {
int actionId = getNextNotificationId();
sendNotification(context, actionId);
int open = SPUtils.getInstance().getInt("open", 0);
int num = SPUtils.getInstance().getInt("num", 0);
long delay = SPUtils.getInstance().getLong("delay", 0);
if (open == 1) {
handlerThread = new HandlerThread("NotificationHandlerThread");
handlerThread.start();
// 创建 Handler
handler = new Handler(handlerThread.getLooper());
for (int i = 1; i <= num; i++) {
final long time = i * delay;
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (MyApplication.PAUSED_VALUE == 1) {
handler.removeCallbacksAndMessages(null);
return;
}
if (MyApplication.PAUSED_VALUE != 1) {
sendNotification(context, actionId);
}
}
}, time);
}
}
}
public static void stopNotificationHandler() {
// 停止 HandlerThread
if (handler != null) {
handler.removeCallbacksAndMessages(null);
}
if (handlerThread != null) {
handlerThread.quit();
handlerThread = null;
}
handler = null;
}
public static void sendNotification(Context context, int actionId) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_common_notify);
//功能触发push actionId 主动发送
String desc;
String btn;
int icon;
int actionId = getNextNotificationId();
if (actionId == ConfigBean.ID_JUNK_CLEAN_PUSH) {
icon = R.drawable.cleanjunk;
desc = "Clean up remaining junk files";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment