diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ad3f3c4..0d3146b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/receivers/RegisterBroadcastReceiver.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/receivers/RegisterBroadcastReceiver.kt index 46c568a..f6b7dac 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/receivers/RegisterBroadcastReceiver.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/receivers/RegisterBroadcastReceiver.kt @@ -43,13 +43,13 @@ class RegisterBroadcastReceiver : BroadcastReceiver() { ACTION_UNREGISTER ->{ Log.i(TAG,"UNREGISTER") val connectorToken = intent.getStringExtra(EXTRA_TOKEN)?: "" - val application = intent.getStringExtra(EXTRA_APPLICATION)?: return + val application = getDb(context!!).getPackageName(connectorToken) if (application.isBlank()) { return } if (connectorToken !in delQueue) { delQueue.add(connectorToken) - sendUnregistered(context!!.applicationContext, connectorToken) + sendUnregistered(context.applicationContext, connectorToken) ApiUtils().deleteApp(context.applicationContext, connectorToken) { val db = getDb(context.applicationContext) db.unregisterApp(connectorToken) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt index 3113ff2..23b7695 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt @@ -33,6 +33,15 @@ fun createForegroundNotification(context: Context): Notification { notificationManager.createNotificationChannel(channel) } + val notificationIntent = Intent(context, MainActivity::class.java) + + notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP + + val intent = PendingIntent.getActivity( + context, 0, + notificationIntent, 0 + ) + val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder( context, notificationChannelId @@ -44,6 +53,7 @@ fun createForegroundNotification(context: Context): Notification { .setSmallIcon(R.drawable.ic_launcher_notification) .setTicker(context.getString(R.string.listening_notif_ticker)) .setPriority(Notification.PRIORITY_LOW) // for under android 26 compatibility + .setContentIntent(intent) .build() } @@ -65,7 +75,7 @@ fun createWarningNotification(context: Context) { notificationManager.createNotificationChannel(channel) } - val notificationIntent: Intent = Intent(context, MainActivity::class.java) + val notificationIntent = Intent(context, MainActivity::class.java) notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt index 764ccfb..265172f 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt @@ -16,7 +16,15 @@ import com.nextcloud.android.sso.ui.UiExceptionManager import org.unifiedpush.distributor.nextpush.api.ApiUtils import org.unifiedpush.distributor.nextpush.account.ssoAccount +import android.net.Network + +import android.net.ConnectivityManager +import android.net.ConnectivityManager.NetworkCallback +import java.lang.Exception + + private const val TAG = "StartService" +var isServiceStarted = false fun startListener(context: Context){ Log.d(TAG, "Starting the Listener") @@ -30,7 +38,6 @@ fun startListener(context: Context){ class StartService: Service(){ - private var isServiceStarted = false private var wakeLock: PowerManager.WakeLock? = null private var api = ApiUtils() @@ -46,6 +53,7 @@ class StartService: Service(){ } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + registerNetworkCallback() startService() // by returning this we make sure the service is restarted if the system kills the service return START_STICKY @@ -77,5 +85,25 @@ class StartService: Service(){ api.sync(this) } + + private fun registerNetworkCallback() { + try { + val connectivityManager = + this.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager + connectivityManager.registerDefaultNetworkCallback(object : NetworkCallback() { + override fun onAvailable(network: Network) { + Log.d(TAG, "Network is CONNECTED") + startService() + } + + override fun onLost(network: Network) { + Log.d(TAG, "Network is DISCONNECTED") + } + }) + } catch (e: Exception) { + e.printStackTrace() + } + } + }