diff options
5 files changed, 39 insertions, 13 deletions
diff --git a/src/main/java/de/pixart/messenger/services/EventReceiver.java b/src/main/java/de/pixart/messenger/services/EventReceiver.java index a77db569e..03bf68ed9 100644 --- a/src/main/java/de/pixart/messenger/services/EventReceiver.java +++ b/src/main/java/de/pixart/messenger/services/EventReceiver.java @@ -13,6 +13,7 @@ import de.pixart.messenger.utils.Compatibility; public class EventReceiver extends BroadcastReceiver { public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts"; + public static final String EXTRA_NEEDS_FOREGROUND_SERVICE = "needs_foreground_service"; @Override public void onReceive(final Context context, final Intent originalIntent) { @@ -26,6 +27,7 @@ public class EventReceiver extends BroadcastReceiver { if (action.equals("ui") || hasEnabledAccounts(context)) { try { if (Compatibility.runsAndTargetsTwentySix(context)) { + intentForService.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true); ContextCompat.startForegroundService(context, intentForService); } else { context.startService(intentForService); @@ -42,4 +44,4 @@ public class EventReceiver extends BroadcastReceiver { return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS, true); } -} +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index c0c3c7dec..9d0071af2 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -581,6 +581,11 @@ public class XmppConnectionService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { final String action = intent == null ? null : intent.getAction(); + final boolean needsForegroundService = intent != null && intent.getBooleanExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, false); + if (needsForegroundService) { + Log.d(Config.LOGTAG, "toggle forced foreground service after receiving event"); + toggleForegroundService(true); + } String pushedAccountHash = null; boolean interactive = false; if (action != null) { @@ -1243,8 +1248,12 @@ public class XmppConnectionService extends Service { } public void toggleForegroundService() { + toggleForegroundService(false); + } + + private void toggleForegroundService(boolean force) { final boolean status; - if (mForceForegroundService.get() || (Compatibility.keepForegroundService(this)/* && hasEnabledAccounts()*/)) { + if (force || mForceForegroundService.get() || (Compatibility.keepForegroundService(this)/* && hasEnabledAccounts()*/)) { startForeground(NotificationService.FOREGROUND_NOTIFICATION_ID, this.mNotificationService.createForegroundNotification()); status = true; } else { diff --git a/src/standardPush/java/de/pixart/messenger/services/InstanceIdService.java b/src/standardPush/java/de/pixart/messenger/services/InstanceIdService.java index d812105aa..465f974e6 100644 --- a/src/standardPush/java/de/pixart/messenger/services/InstanceIdService.java +++ b/src/standardPush/java/de/pixart/messenger/services/InstanceIdService.java @@ -13,12 +13,13 @@ public class InstanceIdService extends FirebaseInstanceIdService { @Override public void onTokenRefresh() { - Intent intent = new Intent(this, XmppConnectionService.class); + final Intent intent = new Intent(this, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); - try { + if (Compatibility.runsAndTargetsTwentySix(this)) { + intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true); + ContextCompat.startForegroundService(this, intent); + } else { startService(intent); - } catch (Exception e) { - Log.e(Config.LOGTAG, "unable to refresh FCM token", e); } } -} +}
\ No newline at end of file diff --git a/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java b/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java index 9368913bc..5706a487a 100644 --- a/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java +++ b/src/standardPush/java/de/pixart/messenger/services/MaintenanceReceiver.java @@ -3,6 +3,7 @@ package de.pixart.messenger.services; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.support.v4.content.ContextCompat; import android.util.Log; import com.google.firebase.iid.FirebaseInstanceId; @@ -10,6 +11,7 @@ import com.google.firebase.iid.FirebaseInstanceId; import java.io.IOException; import de.pixart.messenger.Config; +import de.pixart.messenger.utils.Compatibility; public class MaintenanceReceiver extends BroadcastReceiver { @Override @@ -25,9 +27,14 @@ public class MaintenanceReceiver extends BroadcastReceiver { new Thread(() -> { try { FirebaseInstanceId.getInstance().deleteInstanceId(); - Intent intent = new Intent(context, XmppConnectionService.class); + final Intent intent = new Intent(context, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH); - context.startService(intent); + if (Compatibility.runsAndTargetsTwentySix(context)) { + intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true); + ContextCompat.startForegroundService(context, intent); + } else { + context.startService(intent); + } } catch (IOException e) { Log.d(Config.LOGTAG, "unable to renew instance token", e); } diff --git a/src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java b/src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java index 816d11118..88b60e348 100644 --- a/src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java +++ b/src/standardPush/java/de/pixart/messenger/services/PushMessageReceiver.java @@ -1,6 +1,7 @@ package de.pixart.messenger.services; import android.content.Intent; +import android.support.v4.content.ContextCompat; import android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; @@ -9,6 +10,7 @@ import com.google.firebase.messaging.RemoteMessage; import java.util.Map; import de.pixart.messenger.Config; +import de.pixart.messenger.utils.Compatibility; public class PushMessageReceiver extends FirebaseMessagingService { @@ -18,10 +20,15 @@ public class PushMessageReceiver extends FirebaseMessagingService { Log.d(Config.LOGTAG, "PushMessageReceiver ignored message because no accounts are enabled"); return; } - Map<String, String> data = message.getData(); - Intent intent = new Intent(this, XmppConnectionService.class); + final Map<String, String> data = message.getData(); + final Intent intent = new Intent(this, XmppConnectionService.class); intent.setAction(XmppConnectionService.ACTION_FCM_MESSAGE_RECEIVED); intent.putExtra("account", data.get("account")); - startService(intent); + if (Compatibility.runsAndTargetsTwentySix(this)) { + intent.putExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, true); + ContextCompat.startForegroundService(this, intent); + } else { + startService(intent); + } } -} +}
\ No newline at end of file |