From 99c7ba1302a1b94b7a0f07f508cfbe3efb392414 Mon Sep 17 00:00:00 2001 From: luvletter2333 Date: Wed, 17 Nov 2021 20:23:59 +0800 Subject: [PATCH] Refine Push services (thanks Tfoss Team) --- .../telegram/messenger/ApplicationLoader.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java b/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java index d45e866ba..9080f2bf7 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java @@ -54,6 +54,7 @@ import tw.nekomimi.nekogram.utils.UIUtil; import static android.os.Build.VERSION.SDK_INT; public class ApplicationLoader extends Application { + private static PendingIntent pendingIntent; @SuppressLint("StaticFieldLeak") public static volatile Context applicationContext; @@ -417,23 +418,37 @@ public class ApplicationLoader extends Application { ConnectionsManager.getInstance(UserConfig.selectedAccount).setPushConnectionEnabled(true); } if (enabled) { - try { - UIUtil.runOnUIThread(() -> { + UIUtil.runOnUIThread(() -> { + try { + Log.d("TFOSS", "Trying to start push service every 10 minutes"); + // Telegram-FOSS: unconditionally enable push service + AlarmManager am = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE); + Intent i = new Intent(applicationContext, NotificationsService.class); + pendingIntent = PendingIntent.getBroadcast(applicationContext, 0, i, 0); + + am.cancel(pendingIntent); + am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 60 * 1000, pendingIntent); + Log.d("TFOSS", "Starting push service..."); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { applicationContext.startForegroundService(new Intent(applicationContext, NotificationsService.class)); } else { applicationContext.startService(new Intent(applicationContext, NotificationsService.class)); } - }); - } catch (Throwable e) { - Log.d("TFOSS", "Failed to start push service"); - } + } catch (Throwable e) { + Log.d("TFOSS", "Failed to start push service"); + } + }); + } else UIUtil.runOnUIThread(() -> { applicationContext.stopService(new Intent(applicationContext, NotificationsService.class)); + PendingIntent pintent = PendingIntent.getService(applicationContext, 0, new Intent(applicationContext, NotificationsService.class), 0); AlarmManager alarm = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE); alarm.cancel(pintent); + if (pendingIntent != null) { + alarm.cancel(pendingIntent); + } }); }