forked from NextAlone/Nagram
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
InApp、FCM、Unified Push Co-authored-by: Timothy Redaelli <[email protected]>
- Loading branch information
Showing
11 changed files
with
277 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
TMessagesProj/src/main/java/org/telegram/messenger/UnifiedPushListenerServiceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
TMessagesProj/src/main/java/org/telegram/messenger/UnifiedPushReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.