Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Fixed opening url from notification issue

Michael Rademakerdb7a70e1d55f
diff --git a/console/android/GenericApp/app/build.gradle b/console/android/GenericApp/app/build.gradle
index b6387cd07..67debeeef 100644
--- a/console/android/GenericApp/app/build.gradle
+++ b/console/android/GenericApp/app/build.gradle
@@ -11,7 +11,7 @@ android {
applicationId "io.openremote.app"
minSdkVersion 25
targetSdkVersion 29
- versionCode 17
+ versionCode 18
versionName "1.0.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
diff --git a/console/android/GenericApp/app/src/main/java/io/openremote/app/service/NotificationResource.kt b/console/android/GenericApp/app/src/main/java/io/openremote/app/service/NotificationResource.kt
index 3fdf1d735..58219c3c2 100644
--- a/console/android/GenericApp/app/src/main/java/io/openremote/app/service/NotificationResource.kt
+++ b/console/android/GenericApp/app/src/main/java/io/openremote/app/service/NotificationResource.kt
@@ -12,7 +12,7 @@ class NotificationResource(context: Context) {
private val sharedPreferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context)
- fun executeRequest(httpMethod: String, appUrl: String, data: String) {
+ fun executeRequest(httpMethod: String, appUrl: String, data: String?) {
val project = sharedPreferences.getString("project", null)
val realm = sharedPreferences.getString("realm", null)
@@ -26,13 +26,14 @@ class NotificationResource(context: Context) {
requestMethod = httpMethod
setRequestProperty("Accept", "application/json")
+ if (!data.isNullOrBlank()) {
+ setRequestProperty("Content-Type", "application/json")
+ doOutput = true
- setRequestProperty("Content-Type", "application/json")
- doOutput = true
-
- val outputWriter = outputStream.bufferedWriter()
- outputWriter.write(data)
- outputWriter.flush()
+ val outputWriter = outputStream.bufferedWriter()
+ outputWriter.write(data)
+ outputWriter.flush()
+ }
}
}
}
@@ -62,7 +63,11 @@ class NotificationResource(context: Context) {
}
}
- fun notificationAcknowledged(notificationId: Long, fcmToken: String?, acknowledgement: String) {
+ fun notificationAcknowledged(
+ notificationId: Long,
+ fcmToken: String?,
+ acknowledgement: String?
+ ) {
val project = sharedPreferences.getString("project", null)
val realm = sharedPreferences.getString("realm", null)
@@ -82,7 +87,7 @@ class NotificationResource(context: Context) {
val outputWriter = outputStream.bufferedWriter()
outputWriter.write(
- "{\"acknowledgement\": \"$acknowledgement\"}"
+ "{\"acknowledgement\": \"${acknowledgement ?: ""}\"}"
)
outputWriter.flush()
}
diff --git a/console/android/GenericApp/app/src/main/java/io/openremote/app/service/ORFirebaseMessagingService.kt b/console/android/GenericApp/app/src/main/java/io/openremote/app/service/ORFirebaseMessagingService.kt
index 5d3be29ee..4bb2baed8 100644
--- a/console/android/GenericApp/app/src/main/java/io/openremote/app/service/ORFirebaseMessagingService.kt
+++ b/console/android/GenericApp/app/src/main/java/io/openremote/app/service/ORFirebaseMessagingService.kt
@@ -207,8 +207,7 @@ class ORFirebaseMessagingService : com.google.firebase.messaging.FirebaseMessagi
actionIntent.putExtra("notificationId", notificationId)
actionIntent.putExtra("acknowledgement", acknowledgement)
actionIntent.action = System.currentTimeMillis().toString()
- if (ORAlertAction?.url != null && !ORAlertAction.url.isNullOrEmpty()
- ) {
+ if (ORAlertAction?.url != null && ORAlertAction.url.isNotEmpty()) {
actionIntent.putExtra("appUrl", ORAlertAction.url)
actionIntent.putExtra("httpMethod", ORAlertAction.httpMethod)
actionIntent.putExtra("silent", ORAlertAction.isSilent)
diff --git a/console/android/GenericApp/app/src/main/java/io/openremote/app/service/ORMessagingActionService.kt b/console/android/GenericApp/app/src/main/java/io/openremote/app/service/ORMessagingActionService.kt
index 1a046ab5b..9a159dc25 100644
--- a/console/android/GenericApp/app/src/main/java/io/openremote/app/service/ORMessagingActionService.kt
+++ b/console/android/GenericApp/app/src/main/java/io/openremote/app/service/ORMessagingActionService.kt
@@ -1,75 +1,68 @@
-package io.openremote.app.service;
+package io.openremote.app.service
-import android.app.IntentService;
-import android.app.NotificationManager;
-import android.app.Service;
-import android.content.Context;
-import android.content.Intent;
-import android.net.Uri;
-import android.text.TextUtils;
+import android.app.IntentService
+import android.content.Intent
+import android.app.NotificationManager
+import android.net.Uri
+import io.openremote.app.R
+import android.text.TextUtils
+import io.openremote.app.ui.MainActivity
-import androidx.annotation.Nullable;
-
-import io.openremote.app.R;
-
-
-public class ORMessagingActionService extends IntentService {
-
- private NotificationResource notificationResource;
-
- public ORMessagingActionService() {
- super("org.openremote.android.ORMessagingActionService");
- }
-
- @Override
- public void onCreate() {
- super.onCreate();
- notificationResource = new NotificationResource(getApplicationContext());
+class ORMessagingActionService : IntentService("org.openremote.android.ORMessagingActionService") {
+ private var notificationResource: NotificationResource? = null
+ override fun onCreate() {
+ super.onCreate()
+ notificationResource = NotificationResource(applicationContext)
}
- @Override
- protected void onHandleIntent(@Nullable Intent intent) {
- Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
- this.sendBroadcast(it);
-
- Long notificationId = intent.getLongExtra("notificationId", 0L);
- String acknowledgement = intent.getStringExtra("acknowledgement");
-
- NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
- if (manager != null) {
- manager.cancel(notificationId.hashCode());
- }
-
- String consoleId = getSharedPreferences(getApplicationContext().getString(R.string.OR_CONSOLE_NAME), Context.MODE_PRIVATE).getString("consoleId", "");
+ override fun onHandleIntent(intent: Intent?) {
+ val it = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
+ this.sendBroadcast(it)
+ val notificationId = intent!!.getLongExtra("notificationId", 0L)
+ val acknowledgement = intent.getStringExtra("acknowledgement")
+ val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
+ manager.cancel(java.lang.Long.hashCode(notificationId))
+ val consoleId = getSharedPreferences(
+ applicationContext.getString(R.string.OR_CONSOLE_NAME),
+ MODE_PRIVATE
+ ).getString("consoleId", "")
if (!TextUtils.isEmpty(consoleId)) {
- notificationResource.notificationAcknowledged(notificationId, consoleId, acknowledgement);
+ notificationResource!!.notificationAcknowledged(
+ notificationId,
+ consoleId,
+ acknowledgement
+ )
}
+ val appUrl = intent.getStringExtra("appUrl")
+ if (!appUrl.isNullOrBlank()) {
+ val openInBrowser = intent.getBooleanExtra("openInBrowser", false)
+ val silent = intent.getBooleanExtra("silent", false)
+ val data = intent.getStringExtra("data")
+ var httpMethod = intent.getStringExtra("httpMethod")
+ httpMethod = if (TextUtils.isEmpty(httpMethod)) "GET" else httpMethod
- String appUrl = intent.getStringExtra("appUrl");
-
- if (!TextUtils.isEmpty(appUrl)) {
- boolean openInBrowser = intent.getBooleanExtra("openInBrowser", false);
- boolean silent = intent.getBooleanExtra("silent", false);
- String data = intent.getStringExtra("data");
- String httpMethod = intent.getStringExtra("httpMethod");
- httpMethod = TextUtils.isEmpty(httpMethod) ? "GET" : httpMethod;
-
- if (openInBrowser) {
- // Don't load the app just send straight to browser
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- i.setData(Uri.parse(appUrl));
- startActivity(i);
- } else if (silent) {
- // Do silent HTTP request
- notificationResource.executeRequest(httpMethod, appUrl, data);
- } else {
- Intent activityIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage(getApplicationContext().getPackageName());
- activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
- Intent.FLAG_ACTIVITY_SINGLE_TOP);
- activityIntent.putExtra("appUrl", appUrl);
- startActivity(activityIntent);
+ when {
+ openInBrowser -> {
+ // Don't load the app just send straight to browser
+ val i = Intent(Intent.ACTION_VIEW)
+ i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ i.data = Uri.parse(appUrl)
+ startActivity(i)
+ }
+ silent -> {
+ // Do silent HTTP request
+ notificationResource!!.executeRequest(httpMethod!!, appUrl, data)
+ }
+ else -> {
+ val activityIntent = Intent(applicationContext, MainActivity::class.java)
+ activityIntent.addFlags(
+ Intent.FLAG_ACTIVITY_NEW_TASK or
+ Intent.FLAG_ACTIVITY_SINGLE_TOP
+ )
+ activityIntent.putExtra("appUrl", appUrl)
+ startActivity(activityIntent)
+ }
}
}
}
-}
+}
\ No newline at end of file
diff --git a/console/android/GenericApp/app/src/main/java/io/openremote/app/ui/MainActivity.kt b/console/android/GenericApp/app/src/main/java/io/openremote/app/ui/MainActivity.kt
index 7c24f64d1..6a78d6214 100644
--- a/console/android/GenericApp/app/src/main/java/io/openremote/app/ui/MainActivity.kt
+++ b/console/android/GenericApp/app/src/main/java/io/openremote/app/ui/MainActivity.kt
@@ -35,6 +35,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.firebase.iid.FirebaseInstanceId
import io.openremote.app.R
import io.openremote.app.models.ORAppConfig
+import io.openremote.app.network.ApiManager
import io.openremote.app.service.GeofenceProvider
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONException
@@ -103,23 +104,44 @@ class MainActivity : Activity() {
initializeWebView()
}
- openIntentUrl(intent)
progressBar = findViewById(R.id.webProgressBar)
progressBar?.max = 100
progressBar?.progress = 1
- }
-
- override fun onNewIntent(intent: Intent) {
- openIntentUrl(intent)
- }
- private fun openIntentUrl(intent: Intent) {
if (intent.hasExtra(APP_CONFIG_KEY)) {
appConfig = jacksonObjectMapper().readValue(
intent.getStringExtra(APP_CONFIG_KEY),
ORAppConfig::class.java
)
}
+
+ if (appConfig == null) {
+ sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
+
+ val project = sharedPreferences!!.getString("project", null)
+ val realm = sharedPreferences!!.getString("realm", null)
+
+ if (!project.isNullOrBlank() && !realm.isNullOrBlank()) {
+ val apiManager = ApiManager("https://${project}.openremote.io/api/$realm")
+ apiManager.getAppConfig { statusCode, appConfig, error ->
+ if (statusCode in 200..299) {
+ this.appConfig = appConfig
+ processAppConfig()
+ }
+ openIntentUrl(intent)
+ }
+ }
+ } else {
+ processAppConfig()
+ openIntentUrl(intent)
+ }
+ }
+
+ override fun onNewIntent(intent: Intent) {
+ openIntentUrl(intent)
+ }
+
+ private fun processAppConfig() {
if (appConfig != null) {
if (appConfig!!.menuEnabled && !appConfig!!.links.isNullOrEmpty()) {
@@ -159,8 +181,9 @@ class MainActivity : Activity() {
}
floatingActionButton.layoutParams = layoutParams
floatingActionButton.setImageResource(R.drawable.ic_menu)
- if(!appConfig!!.primaryColor.isNullOrBlank()) {
- floatingActionButton.backgroundTintList = ColorStateList.valueOf(Color.parseColor(appConfig!!.primaryColor!!))
+ if (!appConfig!!.primaryColor.isNullOrBlank()) {
+ floatingActionButton.backgroundTintList =
+ ColorStateList.valueOf(Color.parseColor(appConfig!!.primaryColor!!))
}
floatingActionButton.setOnClickListener {
// We are showing only toast message. However, you can do anything you need.
@@ -177,24 +200,33 @@ class MainActivity : Activity() {
activity_web.addView(floatingActionButton)
}
- LOG.fine("Loading web view: ${appConfig!!.initialUrl}")
- loadUrl(appConfig!!.initialUrl)
- } else if (!intent.hasExtra("appUrl")) {
- val url = clientUrl
- LOG.fine("Loading web view: $url")
- loadUrl(url)
- } else {
- var url = clientUrl
- val intentUrl = intent.getStringExtra("appUrl")
- if (intentUrl != null) {
- url = if (intentUrl.startsWith("http") || intentUrl.startsWith("https")) {
- intentUrl
- } else {
- url + intentUrl
+ }
+ }
+
+ private fun openIntentUrl(intent: Intent) {
+ when {
+ intent.hasExtra("appUrl") -> {
+ val url = intent.getStringExtra("appUrl")
+ LOG.fine("Loading web view: $url")
+ loadUrl(url)
+ }
+ appConfig != null -> {
+ LOG.fine("Loading web view: ${appConfig!!.initialUrl}")
+ loadUrl(appConfig!!.initialUrl)
+ }
+ else -> {
+ var url = clientUrl
+ val intentUrl = intent.getStringExtra("appUrl")
+ if (intentUrl != null) {
+ url = if (intentUrl.startsWith("http") || intentUrl.startsWith("https")) {
+ intentUrl
+ } else {
+ url + intentUrl
+ }
}
+ LOG.fine("Loading web view: $url")
+ loadUrl(url)
}
- LOG.fine("Loading web view: $url")
- loadUrl(url)
}
}