Skip to content

Commit 10cb3af

Browse files
GolbinexTimothy Redaelli
authored andcommitted
UnifiedPush support PR by @Golbinex (#306)
* [TF][PUSH] implemented a simple UnifiedPush support This simple implementation only triggers the networks to resume when an event is received, so Telegram can get the events by itself. This is not like the other push providers where the change information is processed by PushListenerController directly, but it still saves battery since the process disconnects from Telegram servers as soon as the updates are processed. * [MG] add a longclick listener to UnifiedPush Distributors menu This prints informations to know if UnifiedPush is working or not. * [MG] encode the url to pass to p2p.belloworld.it This is needed if urls includes query (like UP-FCM). This also means the proxy had to be a little more complex, since nginx didn't do url decoding: https://gist.github.com/drizzt/93d253bca4f64fae7df2a4544a98c08d --------- Co-authored-by: Timothy Redaelli <timothy@fsfe.org>
1 parent bfb8467 commit 10cb3af

9 files changed

Lines changed: 284 additions & 8 deletions

File tree

TMessagesProj/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ apply plugin: 'kotlin-android'
77
repositories {
88
mavenCentral()
99
google()
10+
maven {
11+
url 'https://www.jitpack.io'
12+
content {
13+
includeModule 'com.github.UnifiedPush', 'android-connector'
14+
}
15+
}
1016
}
1117

1218
configurations {
@@ -50,6 +56,7 @@ dependencies {
5056
because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib")
5157
}
5258
}
59+
implementation 'com.github.UnifiedPush:android-connector:2.3.1'
5360
implementation 'org.osmdroid:osmdroid-android:6.1.14'
5461

5562
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

TMessagesProj/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,15 @@ e <intent-filter>
639639
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
640640
</intent-filter>
641641
</receiver>
642+
643+
<receiver android:exported="true" android:enabled="true" android:name="org.telegram.messenger.UnifiedPushReceiver">
644+
<intent-filter>
645+
<action android:name="org.unifiedpush.android.connector.MESSAGE"/>
646+
<action android:name="org.unifiedpush.android.connector.UNREGISTERED"/>
647+
<action android:name="org.unifiedpush.android.connector.NEW_ENDPOINT"/>
648+
<action android:name="org.unifiedpush.android.connector.REGISTRATION_FAILED"/>
649+
</intent-filter>
650+
</receiver>
642651

643652
<service
644653
android:name=".ContactsWidgetService"

TMessagesProj/src/main/java/org/telegram/messenger/ApplicationLoader.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static PushListenerController.IPushListenerServiceProvider getPushProvide
100100
}
101101

102102
protected PushListenerController.IPushListenerServiceProvider onCreatePushProvider() {
103-
return PushListenerController.GooglePushListenerServiceProvider.INSTANCE;
103+
return PushListenerController.UnifiedPushListenerServiceProvider.INSTANCE;
104104
}
105105

106106
public static String getApplicationId() {
@@ -227,7 +227,7 @@ public void onReceive(Context context, Intent intent) {
227227
}
228228

229229
ApplicationLoader app = (ApplicationLoader) ApplicationLoader.applicationContext;
230-
//app.initPushServices();
230+
app.initPushServices();
231231
if (BuildVars.LOGS_ENABLED) {
232232
FileLog.d("app initied");
233233
}
@@ -396,7 +396,6 @@ public void onConfigurationChanged(Configuration newConfig) {
396396
e.printStackTrace();
397397
}
398398
}
399-
/*
400399
private void initPushServices() {
401400
AndroidUtilities.runOnUIThread(() -> {
402401
if (getPushProvider().hasServices()) {
@@ -410,7 +409,7 @@ private void initPushServices() {
410409
}
411410
}, 1000);
412411
}
413-
412+
/*
414413
private boolean checkPlayServices() {
415414
try {
416415
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

TMessagesProj/src/main/java/org/telegram/messenger/PushListenerController.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,26 @@
2020
import org.telegram.tgnet.SerializedData;
2121
import org.telegram.tgnet.TLRPC;
2222

23+
import org.unifiedpush.android.connector.UnifiedPush;
24+
2325
import java.lang.annotation.Retention;
2426
import java.lang.annotation.RetentionPolicy;
2527
import java.util.ArrayList;
2628
import java.util.Arrays;
29+
import java.util.List;
2730
import java.util.Locale;
2831
import java.util.concurrent.CountDownLatch;
2932

3033
@Keep
3134
public class PushListenerController {
3235
public static final int PUSH_TYPE_FIREBASE = 2,
36+
PUSH_TYPE_SIMPLE = 4,
3337
PUSH_TYPE_HUAWEI = 13;
3438

3539
@Retention(RetentionPolicy.SOURCE)
3640
@IntDef({
3741
PUSH_TYPE_FIREBASE,
42+
PUSH_TYPE_SIMPLE,
3843
PUSH_TYPE_HUAWEI
3944
})
4045
public @interface PushType {}
@@ -62,7 +67,7 @@ public static void sendRegistrationToServer(@PushType int pushType, String token
6267
if (userConfig.getClientUserId() != 0) {
6368
final int currentAccount = a;
6469
if (sendStat) {
65-
String tag = pushType == PUSH_TYPE_FIREBASE ? "fcm" : "hcm";
70+
String tag = pushType == PUSH_TYPE_FIREBASE ? "fcm" : (pushType == PUSH_TYPE_HUAWEI ? "hcm" : "up");
6671
TLRPC.TL_help_saveAppLog req = new TLRPC.TL_help_saveAppLog();
6772
TLRPC.TL_inputAppEvent event = new TLRPC.TL_inputAppEvent();
6873
event.time = SharedConfig.pushStringGetTimeStart;
@@ -90,7 +95,7 @@ public static void sendRegistrationToServer(@PushType int pushType, String token
9095
}
9196

9297
public static void processRemoteMessage(@PushType int pushType, String data, long time) {
93-
String tag = pushType == PUSH_TYPE_FIREBASE ? "FCM" : "HCM";
98+
String tag = pushType == PUSH_TYPE_FIREBASE ? "FCM" : (pushType == PUSH_TYPE_HUAWEI ? "HCM" : "UP");
9499
if (BuildVars.LOGS_ENABLED) {
95100
FileLog.d(tag + " PRE START PROCESSING");
96101
}
@@ -1617,4 +1622,59 @@ public boolean hasServices() {
16171622
return hasServices;*/
16181623
}
16191624
}
1625+
public final static class UnifiedPushListenerServiceProvider implements IPushListenerServiceProvider {
1626+
public final static UnifiedPushListenerServiceProvider INSTANCE = new UnifiedPushListenerServiceProvider();
1627+
1628+
private UnifiedPushListenerServiceProvider(){};
1629+
1630+
@Override
1631+
public boolean hasServices() {
1632+
return !UnifiedPush.getDistributors(ApplicationLoader.applicationContext, new ArrayList()).isEmpty();
1633+
}
1634+
1635+
@Override
1636+
public String getLogTitle() {
1637+
return "UnifiedPush";
1638+
}
1639+
1640+
@Override
1641+
public void onRequestPushToken() {
1642+
String currentPushString = SharedConfig.pushString;
1643+
if (!TextUtils.isEmpty(currentPushString)) {
1644+
if (BuildVars.DEBUG_PRIVATE_VERSION && BuildVars.LOGS_ENABLED) {
1645+
FileLog.d("UnifiedPush endpoint = " + currentPushString);
1646+
}
1647+
} else {
1648+
if (BuildVars.LOGS_ENABLED) {
1649+
FileLog.d("No UnifiedPush string found");
1650+
}
1651+
}
1652+
Utilities.globalQueue.postRunnable(() -> {
1653+
try {
1654+
SharedConfig.pushStringGetTimeStart = SystemClock.elapsedRealtime();
1655+
SharedConfig.saveConfig();
1656+
if (UnifiedPush.getAckDistributor(ApplicationLoader.applicationContext) == null) {
1657+
List<String> distributors = UnifiedPush.getDistributors(ApplicationLoader.applicationContext, new ArrayList<>());
1658+
if (distributors.size() > 0) {
1659+
String distributor = distributors.get(0);
1660+
UnifiedPush.saveDistributor(ApplicationLoader.applicationContext, distributor);
1661+
}
1662+
}
1663+
UnifiedPush.registerApp(
1664+
ApplicationLoader.applicationContext,
1665+
"default",
1666+
new ArrayList<>(),
1667+
"Telegram Simple Push"
1668+
);
1669+
} catch (Throwable e) {
1670+
FileLog.e(e);
1671+
}
1672+
});
1673+
}
1674+
1675+
@Override
1676+
public int getPushType() {
1677+
return PUSH_TYPE_SIMPLE;
1678+
}
1679+
}
16201680
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package org.telegram.messenger;
2+
3+
import android.content.Context;
4+
import android.os.SystemClock;
5+
6+
import org.telegram.tgnet.ConnectionsManager;
7+
import org.unifiedpush.android.connector.MessagingReceiver;
8+
import org.unifiedpush.android.connector.UnifiedPush;
9+
10+
import java.io.UnsupportedEncodingException;
11+
import java.net.URLEncoder;
12+
import java.util.concurrent.CountDownLatch;
13+
14+
public class UnifiedPushReceiver extends MessagingReceiver {
15+
16+
private static long lastReceivedNotification = 0;
17+
private static long numOfReceivedNotifications = 0;
18+
19+
public static long getLastReceivedNotification() {
20+
return lastReceivedNotification;
21+
}
22+
23+
public static long getNumOfReceivedNotifications() {
24+
return numOfReceivedNotifications;
25+
}
26+
27+
@Override
28+
public void onNewEndpoint(Context context, String endpoint, String instance){
29+
Utilities.globalQueue.postRunnable(() -> {
30+
SharedConfig.pushStringGetTimeEnd = SystemClock.elapsedRealtime();
31+
32+
String savedDistributor = UnifiedPush.getSavedDistributor(context);
33+
34+
if (savedDistributor.equals("io.heckel.ntfy")) {
35+
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, endpoint);
36+
} else {
37+
try {
38+
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, "https://p2p.belloworld.it/" + URLEncoder.encode(endpoint, "UTF-8"));
39+
} catch (UnsupportedEncodingException e) {
40+
FileLog.e(e);
41+
}
42+
}
43+
});
44+
}
45+
46+
@Override
47+
public void onMessage(Context context, byte[] message, String instance){
48+
final long receiveTime = SystemClock.elapsedRealtime();
49+
final CountDownLatch countDownLatch = new CountDownLatch(1);
50+
51+
lastReceivedNotification = SystemClock.elapsedRealtime();
52+
numOfReceivedNotifications++;
53+
54+
AndroidUtilities.runOnUIThread(() -> {
55+
if (BuildVars.LOGS_ENABLED) {
56+
FileLog.d("UP PRE INIT APP");
57+
}
58+
ApplicationLoader.postInitApplication();
59+
if (BuildVars.LOGS_ENABLED) {
60+
FileLog.d("UP POST INIT APP");
61+
}
62+
Utilities.stageQueue.postRunnable(() -> {
63+
if (BuildVars.LOGS_ENABLED) {
64+
FileLog.d("UP START PROCESSING");
65+
}
66+
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
67+
if (UserConfig.getInstance(a).isClientActivated()) {
68+
ConnectionsManager.onInternalPushReceived(a);
69+
ConnectionsManager.getInstance(a).resumeNetworkMaybe();
70+
}
71+
}
72+
countDownLatch.countDown();
73+
});
74+
});
75+
Utilities.globalQueue.postRunnable(()-> {
76+
try {
77+
countDownLatch.await();
78+
} catch (Throwable ignore) {
79+
80+
}
81+
if (BuildVars.DEBUG_VERSION) {
82+
FileLog.d("finished UP service, time = " + (SystemClock.elapsedRealtime() - receiveTime));
83+
}
84+
});
85+
}
86+
87+
@Override
88+
public void onRegistrationFailed(Context context, String instance){
89+
if (BuildVars.LOGS_ENABLED) {
90+
FileLog.d("Failed to get endpoint");
91+
}
92+
SharedConfig.pushStringStatus = "__UNIFIEDPUSH_FAILED__";
93+
Utilities.globalQueue.postRunnable(() -> {
94+
SharedConfig.pushStringGetTimeEnd = SystemClock.elapsedRealtime();
95+
96+
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, null);
97+
});
98+
}
99+
100+
@Override
101+
public void onUnregistered(Context context, String instance){
102+
SharedConfig.pushStringStatus = "__UNIFIEDPUSH_FAILED__";
103+
Utilities.globalQueue.postRunnable(() -> {
104+
SharedConfig.pushStringGetTimeEnd = SystemClock.elapsedRealtime();
105+
106+
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, null);
107+
});
108+
}
109+
}

0 commit comments

Comments
 (0)