Add quick way to permanently hide android background service notification
Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
parent
9759add62a
commit
969f8f4d8a
|
@ -8,6 +8,7 @@ import android.app.Notification;
|
||||||
import android.app.NotificationChannel;
|
import android.app.NotificationChannel;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
|
import android.provider.Settings;
|
||||||
|
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.core.app.NotificationManagerCompat;
|
import androidx.core.app.NotificationManagerCompat;
|
||||||
|
@ -30,6 +31,10 @@ public class ForegroundService extends Service {
|
||||||
Context context = getApplicationContext();
|
Context context = getApplicationContext();
|
||||||
// Create the NotificationChannel, but only on API 26+ because
|
// Create the NotificationChannel, but only on API 26+ because
|
||||||
// the NotificationChannel class is new and not in the support library
|
// the NotificationChannel class is new and not in the support library
|
||||||
|
|
||||||
|
Intent intent = null;
|
||||||
|
String notificationContentText = null;
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
NotificationManager notificationManager =
|
NotificationManager notificationManager =
|
||||||
context.getSystemService(NotificationManager.class);
|
context.getSystemService(NotificationManager.class);
|
||||||
|
@ -38,31 +43,49 @@ public class ForegroundService extends Service {
|
||||||
NotificationManager.IMPORTANCE_HIGH);
|
NotificationManager.IMPORTANCE_HIGH);
|
||||||
channel.setShowBadge(false);
|
channel.setShowBadge(false);
|
||||||
notificationManager.createNotificationChannel(channel);
|
notificationManager.createNotificationChannel(channel);
|
||||||
}
|
|
||||||
Class intentClass;
|
// Create intent that takes the user to the notification channel settings so they can hide it
|
||||||
String packageName = context.getPackageName();
|
|
||||||
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
|
intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
|
||||||
String className = launchIntent.getComponent().getClassName();
|
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
|
||||||
try {
|
intent.putExtra(Settings.EXTRA_CHANNEL_ID, CHANNEL_ID);
|
||||||
intentClass = Class.forName(className);
|
|
||||||
} catch (ClassNotFoundException e) {
|
notificationContentText = context.getResources().getString(R.string.tap_to_hide_notification);
|
||||||
e.printStackTrace();
|
|
||||||
return 0;
|
} else {
|
||||||
|
|
||||||
|
// For older versions of android intent takes the user to the Status app
|
||||||
|
|
||||||
|
Class intentClass;
|
||||||
|
String packageName = context.getPackageName();
|
||||||
|
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
|
||||||
|
String className = launchIntent.getComponent().getClassName();
|
||||||
|
try {
|
||||||
|
intentClass = Class.forName(className);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
intent = new Intent(context, intentClass);
|
||||||
|
intent.addCategory(Intent.CATEGORY_BROWSABLE);
|
||||||
|
intent.setAction(Intent.ACTION_VIEW);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
|
|
||||||
|
notificationContentText = context.getResources().getString(R.string.keep_status_running);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Intent intent = new Intent(context, intentClass);
|
|
||||||
intent.addCategory(Intent.CATEGORY_BROWSABLE);
|
|
||||||
intent.setAction(Intent.ACTION_VIEW);
|
|
||||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
|
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
|
||||||
Intent stopIntent = new Intent(PushNotificationHelper.ACTION_TAP_STOP);
|
Intent stopIntent = new Intent(PushNotificationHelper.ACTION_TAP_STOP);
|
||||||
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 0, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
|
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 0, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
|
||||||
|
|
||||||
String content = context.getResources().getString(R.string.keep_status_running);
|
|
||||||
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
|
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
|
||||||
.setSmallIcon(R.drawable.ic_stat_notify_status)
|
.setSmallIcon(R.drawable.ic_stat_notify_status)
|
||||||
.setContentTitle(context.getResources().getString(R.string.background_service_opened))
|
.setContentTitle(context.getResources().getString(R.string.background_service_opened))
|
||||||
.setContentText(content)
|
.setContentText(notificationContentText)
|
||||||
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
|
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
|
||||||
.setContentIntent(pendingIntent)
|
.setContentIntent(pendingIntent)
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
<resources>
|
<resources>
|
||||||
<string name="status_service">Status Service</string>
|
<string name="status_service">Status Background Service</string>
|
||||||
<string name="stop">STOP</string>
|
<string name="stop">STOP</string>
|
||||||
|
<string name="tap_to_hide_notification">Tap to hide this notification permanently</string>
|
||||||
<string name="keep_status_running">Keep Status running to receive notifications</string>
|
<string name="keep_status_running">Keep Status running to receive notifications</string>
|
||||||
<string name="background_service_opened">Background notification service opened</string>
|
<string name="background_service_opened">Background service for notifications is running</string>
|
||||||
<string name="channel_description">Get notifications on new messages and mentions</string>
|
<string name="channel_description">Get notifications on new messages and mentions</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue