stop app when pressing stop button on notifications

This commit is contained in:
yenda 2020-06-15 16:21:22 +02:00
parent fd874c14df
commit 51198f5b69
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
2 changed files with 20 additions and 9 deletions

View File

@ -52,6 +52,8 @@ public class ForegroundService extends Service {
intent.setAction(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 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(NewMessageSignalHandler.ACTION_TAP_STOP);
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 0, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
String content = "Keep Status running to receive notifications"; String content = "Keep Status running to receive notifications";
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
@ -61,6 +63,7 @@ public class ForegroundService extends Service {
.setPriority(NotificationCompat.PRIORITY_HIGH) .setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE) .setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.addAction(R.drawable.ic_stat_notify_status, "STOP", stopPendingIntent)
.build(); .build();
// the id of the foreground notification MUST NOT be 0 // the id of the foreground notification MUST NOT be 0
startForeground(1, notification); startForeground(1, notification);

View File

@ -46,6 +46,7 @@ public class NewMessageSignalHandler {
//in notificationActionReceiver. //in notificationActionReceiver.
public static final String ACTION_DELETE_NOTIFICATION = "im.status.ethereum.module.DELETE_NOTIFICATION"; public static final String ACTION_DELETE_NOTIFICATION = "im.status.ethereum.module.DELETE_NOTIFICATION";
public static final String ACTION_TAP_NOTIFICATION = "im.status.ethereum.module.TAP_NOTIFICATION"; public static final String ACTION_TAP_NOTIFICATION = "im.status.ethereum.module.TAP_NOTIFICATION";
public static final String ACTION_TAP_STOP = "im.status.ethereum.module.TAP_STOP";
private static final String GROUP_STATUS_MESSAGES = "im.status.notifications.message"; private static final String GROUP_STATUS_MESSAGES = "im.status.notifications.message";
private static final String CHANNEL_NAME = "Status"; private static final String CHANNEL_NAME = "Status";
private static final String CHANNEL_DESCRIPTION = "Get notifications on new messages and mentions"; private static final String CHANNEL_DESCRIPTION = "Get notifications on new messages and mentions";
@ -65,15 +66,21 @@ public class NewMessageSignalHandler {
private final BroadcastReceiver notificationActionReceiver = new BroadcastReceiver() { private final BroadcastReceiver notificationActionReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String chatId = intent.getExtras().getString("im.status.ethereum.chatId"); if (intent.getAction() == ACTION_TAP_NOTIFICATION ||
if (intent.getAction() == ACTION_TAP_NOTIFICATION) { intent.getAction() == ACTION_DELETE_NOTIFICATION) {
context.startActivity(getOpenAppIntent(chatId)); String chatId = intent.getExtras().getString("im.status.ethereum.chatId");
} if (intent.getAction() == ACTION_TAP_NOTIFICATION) {
removeChat(chatId); context.startActivity(getOpenAppIntent(chatId));
// clean up the group notifications when there is no }
// more unread chats removeChat(chatId);
if (chats.size() == 0) { // clean up the group notifications when there is no
notificationManager.cancelAll(); // more unread chats
if (chats.size() == 0) {
notificationManager.cancelAll();
}}
if (intent.getAction() == ACTION_TAP_STOP) {
stop();
System.exit(0);
} }
Log.e(TAG, "intent received: " + intent.getAction()); Log.e(TAG, "intent received: " + intent.getAction());
} }
@ -83,6 +90,7 @@ public class NewMessageSignalHandler {
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_DELETE_NOTIFICATION); filter.addAction(ACTION_DELETE_NOTIFICATION);
filter.addAction(ACTION_TAP_NOTIFICATION); filter.addAction(ACTION_TAP_NOTIFICATION);
filter.addAction(ACTION_TAP_STOP);
context.registerReceiver(notificationActionReceiver, filter); context.registerReceiver(notificationActionReceiver, filter);
Log.e(TAG, "Broadcast Receiver registered"); Log.e(TAG, "Broadcast Receiver registered");
} }