Skip to content

Commit

Permalink
perf: Push Service More Type
Browse files Browse the repository at this point in the history
InApp、FCM、Unified Push

Co-authored-by: Timothy Redaelli <[email protected]>
  • Loading branch information
omg-xtao and Timothy Redaelli committed Mar 16, 2024
1 parent 8c78b97 commit 9dd6335
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 11 deletions.
11 changes: 11 additions & 0 deletions TMessagesProj/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
apply plugin: "com.android.application"
apply plugin: "kotlin-android"

repositories {
maven {
url 'https://www.jitpack.io'
content {
includeModule 'com.github.UnifiedPush', 'android-connector'
}
}
}

def verName = "10.9.1"
def verCode = 1163

Expand Down Expand Up @@ -312,6 +321,8 @@ dependencies {
implementation 'com.google.mlkit:language-id:17.0.5'
// add for emoji
implementation 'com.jaredrummler:truetypeparser-light:1.0.0'
// add for up
implementation 'com.github.UnifiedPush:android-connector:2.3.1'
}

apply plugin: "com.google.gms.google-services"
Expand Down
8 changes: 8 additions & 0 deletions TMessagesProj/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,14 @@
<!-- android:permission="android.permission.BIND_REMOTEVIEWS"-->
<!-- android:exported="false" />-->

<receiver android:exported="true" android:enabled="true" android:name=".UnifiedPushReceiver">
<intent-filter>
<action android:name="org.unifiedpush.android.connector.MESSAGE"/>
<action android:name="org.unifiedpush.android.connector.UNREGISTERED"/>
<action android:name="org.unifiedpush.android.connector.NEW_ENDPOINT"/>
<action android:name="org.unifiedpush.android.connector.REGISTRATION_FAILED"/>
</intent-filter>
</receiver>
<service android:name=".GcmPushListenerService"
android:exported="true">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import tw.nekomimi.nekogram.NekoXConfig;
import tw.nekomimi.nekogram.parts.SignturesKt;
import tw.nekomimi.nekogram.utils.FileUtil;
import xyz.nextalone.nagram.NaConfig;

import static android.os.Build.VERSION.SDK_INT;

Expand Down Expand Up @@ -363,6 +364,13 @@ private static void startPushServiceInternal() {
if (enabled) {
AndroidUtilities.runOnUIThread(() -> {
try {
Log.d("TFOSS", "Starting push service...");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && NaConfig.INSTANCE.getPushServiceTypeInAppDialog().Bool()) {
applicationContext.startForegroundService(new Intent(applicationContext, NotificationsService.class));
} else {
applicationContext.startService(new Intent(applicationContext, NotificationsService.class));
}

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);
Expand All @@ -371,13 +379,6 @@ private static void startPushServiceInternal() {

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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

import androidx.core.app.NotificationCompat;

import xyz.nextalone.nagram.NaConfig;

public class NotificationsService extends Service {

@Override
public void onCreate() {
super.onCreate();
ApplicationLoader.postInitApplication();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && NaConfig.INSTANCE.getPushServiceTypeInAppDialog().Bool()) {
String CHANNEL_ID = "push_service_channel";
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, LocaleController.getString("NekoXPushService", R.string.NekoXPushService), NotificationManager.IMPORTANCE_DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
import java.util.Locale;
import java.util.concurrent.CountDownLatch;

import xyz.nextalone.nagram.NaConfig;

public class PushListenerController {
public static final int PUSH_TYPE_FIREBASE = 2,
PUSH_TYPE_SIMPLE = 4,
PUSH_TYPE_HUAWEI = 13;

@Retention(RetentionPolicy.SOURCE)
@IntDef({
PUSH_TYPE_FIREBASE,
PUSH_TYPE_SIMPLE,
PUSH_TYPE_HUAWEI
})
public @interface PushType {}
Expand Down Expand Up @@ -56,7 +60,7 @@ public static void sendRegistrationToServer(@PushType int pushType, String token
if (userConfig.getClientUserId() != 0) {
final int currentAccount = a;
if (sendStat) {
String tag = pushType == PUSH_TYPE_FIREBASE ? "fcm" : "hcm";
String tag = pushType == PUSH_TYPE_FIREBASE ? "fcm" : (pushType == PUSH_TYPE_HUAWEI ? "hcm" : "up");
TLRPC.TL_help_saveAppLog req = new TLRPC.TL_help_saveAppLog();
TLRPC.TL_inputAppEvent event = new TLRPC.TL_inputAppEvent();
event.time = SharedConfig.pushStringGetTimeStart;
Expand Down Expand Up @@ -84,7 +88,7 @@ public static void sendRegistrationToServer(@PushType int pushType, String token
}

public static void processRemoteMessage(@PushType int pushType, String data, long time) {
String tag = pushType == PUSH_TYPE_FIREBASE ? "FCM" : "HCM";
String tag = pushType == PUSH_TYPE_FIREBASE ? "FCM" : (pushType == PUSH_TYPE_HUAWEI ? "HCM" : "UP");
if (BuildVars.LOGS_ENABLED) {
FileLog.d(tag + " PRE START PROCESSING");
}
Expand Down Expand Up @@ -1435,7 +1439,20 @@ public int getPushType() {
public static IPushListenerServiceProvider getProvider() {
if (instance != null)
return instance;
instance = new GooglePushListenerServiceProvider();
switch (NaConfig.INSTANCE.getPushServiceType().Int()) {
case 1: {
instance = new GooglePushListenerServiceProvider();
break;
}
case 2: {
instance = new UnifiedPushListenerServiceProvider();
break;
}
default: {
instance = new DummyPushProvider();
break;
}
}
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.telegram.messenger;

import android.os.SystemClock;
import android.text.TextUtils;

import org.unifiedpush.android.connector.UnifiedPush;

import java.util.ArrayList;
import java.util.List;

public class UnifiedPushListenerServiceProvider implements PushListenerController.IPushListenerServiceProvider {
public UnifiedPushListenerServiceProvider(){};

@Override
public boolean hasServices() {
return !UnifiedPush.getDistributors(ApplicationLoader.applicationContext, new ArrayList<>()).isEmpty();
}

@Override
public String getLogTitle() {
return "UnifiedPush";
}

@Override
public void onRequestPushToken() {
String currentPushString = SharedConfig.pushString;
if (!TextUtils.isEmpty(currentPushString)) {
if (BuildVars.DEBUG_PRIVATE_VERSION && BuildVars.LOGS_ENABLED) {
FileLog.d("UnifiedPush endpoint = " + currentPushString);
}
} else {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("No UnifiedPush string found");
}
}
Utilities.globalQueue.postRunnable(() -> {
try {
SharedConfig.pushStringGetTimeStart = SystemClock.elapsedRealtime();
SharedConfig.saveConfig();
if (UnifiedPush.getAckDistributor(ApplicationLoader.applicationContext) == null) {
List<String> distributors = UnifiedPush.getDistributors(ApplicationLoader.applicationContext, new ArrayList<>());
if (distributors.size() > 0) {
String distributor = distributors.get(0);
UnifiedPush.saveDistributor(ApplicationLoader.applicationContext, distributor);
}
}
UnifiedPush.registerApp(
ApplicationLoader.applicationContext,
"default",
new ArrayList<>(),
"Telegram Simple Push"
);
} catch (Throwable e) {
FileLog.e(e);
}
});
}

@Override
public int getPushType() {
return PushListenerController.PUSH_TYPE_SIMPLE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.telegram.messenger;

import android.content.Context;
import android.os.SystemClock;

import androidx.annotation.NonNull;

import org.telegram.tgnet.ConnectionsManager;

import org.unifiedpush.android.connector.MessagingReceiver;
import org.unifiedpush.android.connector.UnifiedPush;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.CountDownLatch;

import xyz.nextalone.nagram.NaConfig;

public class UnifiedPushReceiver extends MessagingReceiver {

private static long lastReceivedNotification = 0;
private static long numOfReceivedNotifications = 0;

public static long getLastReceivedNotification() {
return lastReceivedNotification;
}

public static long getNumOfReceivedNotifications() {
return numOfReceivedNotifications;
}

@Override
public void onNewEndpoint(Context context, String endpoint, String instance){
Utilities.globalQueue.postRunnable(() -> {
SharedConfig.pushStringGetTimeEnd = SystemClock.elapsedRealtime();

String savedDistributor = UnifiedPush.getSavedDistributor(context);

if (savedDistributor.equals("io.heckel.ntfy")) {
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, endpoint);
return;
}

try {
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, NaConfig.INSTANCE.getPushServiceTypeUnifiedGateway().String() + URLEncoder.encode(endpoint, "UTF-8"));
} catch (UnsupportedEncodingException e) {
FileLog.e(e);
}
});
}

@Override
public void onMessage(Context context, byte[] message, String instance){
final long receiveTime = SystemClock.elapsedRealtime();
final CountDownLatch countDownLatch = new CountDownLatch(1);

lastReceivedNotification = SystemClock.elapsedRealtime();
numOfReceivedNotifications++;

AndroidUtilities.runOnUIThread(() -> {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("UP PRE INIT APP");
}
ApplicationLoader.postInitApplication();
if (BuildVars.LOGS_ENABLED) {
FileLog.d("UP POST INIT APP");
}
Utilities.stageQueue.postRunnable(() -> {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("UP START PROCESSING");
}
for (int a : SharedConfig.activeAccounts) {
if (UserConfig.getInstance(a).isClientActivated()) {
ConnectionsManager.onInternalPushReceived(a);
ConnectionsManager.getInstance(a).resumeNetworkMaybe();
}
}
countDownLatch.countDown();
});
});
Utilities.globalQueue.postRunnable(()-> {
try {
countDownLatch.await();
} catch (Throwable ignore) {

}
if (BuildVars.DEBUG_VERSION) {
FileLog.d("finished UP service, time = " + (SystemClock.elapsedRealtime() - receiveTime));
}
});
}

@Override
public void onRegistrationFailed(Context context, String instance){
if (BuildVars.LOGS_ENABLED) {
FileLog.d("Failed to get endpoint");
}
SharedConfig.pushStringStatus = "__UNIFIEDPUSH_FAILED__";
Utilities.globalQueue.postRunnable(() -> {
SharedConfig.pushStringGetTimeEnd = SystemClock.elapsedRealtime();

PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, null);
});
}

@Override
public void onUnregistered(Context context, String instance){
SharedConfig.pushStringStatus = "__UNIFIEDPUSH_FAILED__";
Utilities.globalQueue.postRunnable(() -> {
SharedConfig.pushStringGetTimeEnd = SystemClock.elapsedRealtime();

PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, null);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.util.OpenPgpApi;
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ApplicationLoader;
import org.telegram.messenger.BuildVars;
import org.telegram.messenger.ContactsController;
import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessagesController;
import org.telegram.messenger.NotificationCenter;
import org.telegram.messenger.NotificationsService;
import org.telegram.messenger.R;
import org.telegram.messenger.SharedConfig;
import org.telegram.messenger.UserConfig;
Expand Down Expand Up @@ -236,6 +238,16 @@ public class NekoGeneralSettingsActivity extends BaseNekoXSettingsActivity {
private final AbstractConfigCell displayPersianCalendarByLatinRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.displayPersianCalendarByLatin));
private final AbstractConfigCell divider7 = cellGroup.appendCell(new ConfigCellDivider());

private final AbstractConfigCell headerPushService = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString("Notifications", R.string.Notifications)));
private final AbstractConfigCell pushServiceTypeRow = cellGroup.appendCell(new ConfigCellSelectBox(null, NaConfig.INSTANCE.getPushServiceType(), new String[]{
LocaleController.getString(R.string.PushServiceTypeInApp),
LocaleController.getString(R.string.PushServiceTypeFCM),
LocaleController.getString(R.string.PushServiceTypeUnified),
}, null));
private final AbstractConfigCell pushServiceTypeInAppDialogRow = cellGroup.appendCell(new ConfigCellTextCheck(NaConfig.INSTANCE.getPushServiceTypeInAppDialog()));
private final AbstractConfigCell pushServiceTypeUnifiedGatewayRow = cellGroup.appendCell(new ConfigCellTextInput(null, NaConfig.INSTANCE.getPushServiceTypeUnifiedGateway(), null, null, (input) -> input.isEmpty() ? (String) NaConfig.INSTANCE.getPushServiceTypeUnifiedGateway().defaultValue : input));
private final AbstractConfigCell divider8 = cellGroup.appendCell(new ConfigCellDivider());

private final AbstractConfigCell headerAutoDownload = cellGroup.appendCell(new ConfigCellHeader(LocaleController.getString("AutoDownload")));
private final AbstractConfigCell win32Row = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.disableAutoDownloadingWin32Executable));
private final AbstractConfigCell archiveRow = cellGroup.appendCell(new ConfigCellTextCheck(NekoConfig.disableAutoDownloadingArchive));
Expand Down Expand Up @@ -476,6 +488,13 @@ public void onItemClick(int id) {
cell.setEnabled(true);
}
listAdapter.notifyItemChanged(cellGroup.rows.indexOf(translationProviderRow));
} else if (key.equals(NaConfig.INSTANCE.getPushServiceType().getKey())) {
restartTooltip.showWithAction(0, UndoView.ACTION_NEED_RESATRT, null, null);
} else if (key.equals(NaConfig.INSTANCE.getPushServiceTypeInAppDialog().getKey())) {
ApplicationLoader.applicationContext.stopService(new Intent(ApplicationLoader.applicationContext, NotificationsService.class));
ApplicationLoader.startPushService();
} else if (key.equals(NaConfig.INSTANCE.getPushServiceTypeUnifiedGateway().getKey())) {
restartTooltip.showWithAction(0, UndoView.ACTION_NEED_RESATRT, null, null);
}
};

Expand Down
Loading

0 comments on commit 9dd6335

Please sign in to comment.