Skip to content

Commit

Permalink
[TF][PUSH] notification in foregroung on Oreo+
Browse files Browse the repository at this point in the history
  • Loading branch information
thermatk committed Sep 15, 2019
1 parent fcd3cf4 commit 169ccaa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Notifications.md
@@ -0,0 +1,11 @@
# Notifications

Since [Android 8.0 Oreo, Google doesn't allow apps to run in the background anymore](https://developer.android.com/about/versions/oreo/background#services), requiring all apps which were previously keeping background connection to exclusively use its Firebase push messaging service.

As one can't use Google's push messaging in a FOSS app, Telegram-FOSS has to show you a notification to keep the background service running. Otherwise, you wouldn't be notified about new messages.

Sadly, if the app would set the notification to lower priority (to hide it a bit in the lower part of the notification screen), you would immediately get a system notification about Telegram "using battery", which is confusing and is the reason for this not being the default. Despite Google's misleading warnings, there is no difference in battery usage between v4.6 in "true background" and v4.9+ with notification.

## Make it better

You may still lower the priority of the notification channel or even hide it altogether manually (make a long tap on the notification). You will then receive the misleading system notification, which [may be disabled as well with another long tap](https://9to5google.com/2017/10/26/how-to-disable-android-oreo-using-battery-notification-android-basics/).
Expand Up @@ -21,6 +21,7 @@
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.os.PowerManager;
import android.telephony.TelephonyManager;
Expand Down Expand Up @@ -196,7 +197,11 @@ public static void startPushService() {
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000, pendingIntent);
try {
Log.d("TFOSS", "Starting push service...");
applicationContext.startService(new Intent(applicationContext, NotificationsService.class));
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
Expand Up @@ -8,17 +8,41 @@

package org.telegram.messenger;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import androidx.core.app.NotificationCompat;

public class NotificationsService extends Service {

@Override
public void onCreate() {
super.onCreate();
ApplicationLoader.postInitApplication();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String CHANNEL_ID = "push_service_channel";
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,"Push Notifications Service",NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
Intent explainIntent = new Intent("android.intent.action.VIEW");
explainIntent.setData(Uri.parse("https://github.com/Telegram-FOSS-Team/Telegram-FOSS/blob/master/Notifications.md"));
PendingIntent explainPendingIntent = PendingIntent.getActivity(this, 0, explainIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentIntent(explainPendingIntent)
.setShowWhen(false)
.setOngoing(true)
.setSmallIcon(R.drawable.notification)
.setContentText("Push service: tap to learn more").build();
startForeground(9999,notification);
}
}

@Override
Expand Down

0 comments on commit 169ccaa

Please sign in to comment.