diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-23 15:08:11 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-23 15:08:11 +1200 |
commit | 7186acaed8370ca179b250a7b45e4940937ca57f (patch) | |
tree | 2ba12c24646795a9de45708ca29debe550f1f3c4 /app/src/main | |
parent | d05ce23bbae5adce5ed59468a50c8e9378517e75 (diff) | |
download | etbsa-traccar-manager-android-7186acaed8370ca179b250a7b45e4940937ca57f.tar.gz etbsa-traccar-manager-android-7186acaed8370ca179b250a7b45e4940937ca57f.tar.bz2 etbsa-traccar-manager-android-7186acaed8370ca179b250a7b45e4940937ca57f.zip |
Implement push notifications
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/org/traccar/manager/MainFragment.java | 54 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 | ||||
-rw-r--r-- | app/src/main/res/values/values.xml | 3 |
3 files changed, 56 insertions, 2 deletions
diff --git a/app/src/main/java/org/traccar/manager/MainFragment.java b/app/src/main/java/org/traccar/manager/MainFragment.java index 6e099ad..4642415 100644 --- a/app/src/main/java/org/traccar/manager/MainFragment.java +++ b/app/src/main/java/org/traccar/manager/MainFragment.java @@ -17,14 +17,20 @@ package org.traccar.manager; import android.app.Activity; import android.content.ActivityNotFoundException; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.ApplicationInfo; import android.content.res.AssetManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.annotation.Nullable; +import android.support.v4.content.LocalBroadcastManager; import android.view.View; +import android.webkit.JavascriptInterface; import android.webkit.MimeTypeMap; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; @@ -42,16 +48,37 @@ import java.util.Map; public class MainFragment extends WebViewFragment { + public final static String EVENT_LOGIN = "eventLogin"; + public final static String EVENT_TOKEN = "eventToken"; + public final static String KEY_TOKEN = "keyToken"; + private final static int REQUEST_FILE_CHOOSER = 1; private AssetManager assetManager; + private LocalBroadcastManager broadcastManager; + + public class AppInterface { + + @JavascriptInterface + public void postMessage(String message) { + if (message.contains("login")) { + broadcastManager.sendBroadcast(new Intent(EVENT_LOGIN)); + } + } + + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + assetManager = getActivity().getAssets(); + broadcastManager = LocalBroadcastManager.getInstance(getActivity()); + } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - assetManager = getActivity().getAssets(); - if ((getActivity().getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE) != 0) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDebuggingEnabled(true); @@ -60,6 +87,7 @@ public class MainFragment extends WebViewFragment { getWebView().setWebViewClient(webViewClient); getWebView().setWebChromeClient(webChromeClient); + getWebView().addJavascriptInterface(new AppInterface(), "appInterface"); WebSettings webSettings = getWebView().getSettings(); webSettings.setJavaScriptEnabled(true); @@ -73,6 +101,28 @@ public class MainFragment extends WebViewFragment { getWebView().loadUrl(url); } + private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String token = intent.getStringExtra(KEY_TOKEN); + String code = "updateNotificationToken && updateNotificationToken('" + token + "')"; + getWebView().evaluateJavascript(code, null); + } + }; + + @Override + public void onStart() { + super.onStart(); + IntentFilter intentFilter = new IntentFilter(EVENT_TOKEN); + broadcastManager.registerReceiver(broadcastReceiver, intentFilter); + } + + @Override + public void onStop() { + super.onStop(); + broadcastManager.unregisterReceiver(broadcastReceiver); + } + public String getMimeType(String url) { String extension = MimeTypeMap.getFileExtensionFromUrl(url); if (extension != null) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0f91e69..2ae7947 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,4 +5,5 @@ <string name="button_start">Start</string> <string name="error_connection">Server connection failed</string> <string name="file_browser">File Browser</string> + <string name="notification_channel">Default</string> </resources> diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml new file mode 100644 index 0000000..d16eb65 --- /dev/null +++ b/app/src/main/res/values/values.xml @@ -0,0 +1,3 @@ +<resources> + <string name="notification_channel_id">default</string> +</resources> |