Skip to content

armgit / core

Public Imported from GitHub
0

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

Almost finished FCM implementation. Still need to do background messages for Android

Michael Rademaker765039f4b74e
diff --git a/console/generic_app/android/app/src/main/AndroidManifest.xml b/console/generic_app/android/app/src/main/AndroidManifest.xml
index 64e1831fa..989aeb26f 100644
--- a/console/generic_app/android/app/src/main/AndroidManifest.xml
+++ b/console/generic_app/android/app/src/main/AndroidManifest.xml
@@ -39,6 +39,10 @@
android:name="flutterEmbedding"
android:value="2" />
+ <meta-data
+ android:name="com.google.firebase.messaging.default_notification_channel_id"
+ android:value="@string/notification_channel_id" />
+
<receiver android:name="io.flutter.plugins.geofencing.GeofencingBroadcastReceiver"
android:enabled="true" android:exported="true"/>
<service android:name="io.flutter.plugins.geofencing.GeofencingService"
diff --git a/console/generic_app/android/app/src/main/kotlin/io/openremote/generic_app/Application.kt b/console/generic_app/android/app/src/main/kotlin/io/openremote/generic_app/Application.kt
index 8c3791a5d..c458f08dc 100644
--- a/console/generic_app/android/app/src/main/kotlin/io/openremote/generic_app/Application.kt
+++ b/console/generic_app/android/app/src/main/kotlin/io/openremote/generic_app/Application.kt
@@ -1,10 +1,12 @@
package io.openremote.generic_app
+import android.app.NotificationChannel
+import android.app.NotificationManager
+import android.content.Context
+import android.os.Build
import io.flutter.app.FlutterApplication
-import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
-import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
import io.flutter.plugins.geofencing.GeofencingService
@@ -13,6 +15,22 @@ class Application : FlutterApplication(), PluginRegistrantCallback {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this)
GeofencingService.setPluginRegistrant(this)
+
+ // Create the NotificationChannel, but only on API 26+ because
+ // the NotificationChannel class is new and not in the support library
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ val channelID = getString(R.string.notification_channel_id)
+ val name = getString(R.string.notification_channel_name)
+ val descriptionText = getString(R.string.notification_channel_desc)
+ val importance = NotificationManager.IMPORTANCE_HIGH
+ val channel = NotificationChannel(channelID, name, importance).apply {
+ description = descriptionText
+ }
+ // Register the channel with the system
+ val notificationManager: NotificationManager =
+ getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
+ notificationManager.createNotificationChannel(channel)
+ }
}
override fun registerWith(registry: PluginRegistry) {
diff --git a/console/generic_app/android/app/src/main/res/values/strings.xml b/console/generic_app/android/app/src/main/res/values/strings.xml
index 8d06dc15c..94bfa4e40 100644
--- a/console/generic_app/android/app/src/main/res/values/strings.xml
+++ b/console/generic_app/android/app/src/main/res/values/strings.xml
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="starting_page">Change project</string>
-</resources>
\ No newline at end of file
+ <string name="notification_channel_id" translatable="false">or_fcm_id</string>
+ <string name="notification_channel_name">OR notifications</string>
+ <string name="notification_channel_desc">Receive information about your project.</string>
+</resources>
diff --git a/console/generic_app/lib/main.dart b/console/generic_app/lib/main.dart
index f21ce8b85..7b75248b6 100644
--- a/console/generic_app/lib/main.dart
+++ b/console/generic_app/lib/main.dart
@@ -1,9 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
-import 'package:generic_app/services/PushNotificationService.dart';
import 'package:generic_app/ui/SplashPage.dart';
-import 'package:shared_preferences/shared_preferences.dart';
import 'generated/l10n.dart';
@@ -11,8 +9,6 @@ void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) async {
- PushNotificationService pushNotificationService = new PushNotificationService();
- await pushNotificationService.initialise();
runApp(new ORApp());
});
}
diff --git a/console/generic_app/lib/services/PushNotificationService.dart b/console/generic_app/lib/services/PushNotificationService.dart
index 7b2e4ed01..6069815cd 100644
--- a/console/generic_app/lib/services/PushNotificationService.dart
+++ b/console/generic_app/lib/services/PushNotificationService.dart
@@ -9,24 +9,19 @@ import 'package:generic_app/ConsoleProviders/StorageProvider.dart';
import 'package:generic_app/events/OpenWebPageEvent.dart';
import 'package:generic_app/models/AlertAction.dart';
import 'package:generic_app/models/AlertButton.dart';
+import 'package:url_launcher/url_launcher.dart';
import 'EventBusService.dart';
-Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> remoteMessage) async {
- await PushNotificationService.handleNotification(remoteMessage, false, false);
-}
-
class PushNotificationService {
static FirebaseMessaging _fcm;
static BuildContext _context;
static init({@required BuildContext context}) {
- _fcm = FirebaseMessaging();
_context = context;
- }
- Future initialise() async {
+ _fcm = FirebaseMessaging();
_fcm.configure(
onMessage: (Map<String, dynamic> remoteMessage) async {
print("onMessage: $remoteMessage");
@@ -40,13 +35,15 @@ class PushNotificationService {
print("onResume: $remoteMessage");
await handleNotification(remoteMessage, true, false);
},
- onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
+ onBackgroundMessage: Platform.isIOS ? null : handleBackgroundNotification,
);
}
- static Future handleNotification(Map<String, dynamic> remoteMessage, bool onResume, bool onLaunch) async {
- Map<dynamic, dynamic> messageData = remoteMessage["data"] ??
- remoteMessage;
+ static Future handleBackgroundNotification(Map<String, dynamic> remoteMessage) => handleNotification(remoteMessage, true, true);
+
+ static Future handleNotification(
+ Map<String, dynamic> remoteMessage, bool onResume, bool onLaunch) async {
+ Map<dynamic, dynamic> messageData = remoteMessage["data"] ?? remoteMessage;
if (messageData != null) {
bool isSilent = !messageData.containsKey("or-title");
@@ -74,15 +71,12 @@ class PushNotificationService {
// Check for action (to be executed when notification is clicked)
if (messageData.containsKey("action")) {
- Map<dynamic, dynamic> actionJson = messageData["action"];
+ String actionJson = messageData["action"];
if (actionJson != null && actionJson.isNotEmpty) {
- action = AlertAction.fromJson(actionJson);
-
- if(onResume || onLaunch) {
- if(action.openInBrowser) {
-
- }
+ action = AlertAction.fromJson(jsonDecode(actionJson));
+ if (onResume || onLaunch) {
+ performAlertAction(action);
}
}
}
@@ -90,37 +84,48 @@ class PushNotificationService {
// Check for buttons
if (messageData.containsKey("buttons")) {
String buttonsJson = messageData["buttons"];
-
if (buttonsJson != null && buttonsJson.isNotEmpty) {
- buttons = (jsonDecode(buttonsJson) as List).map((e) => AlertButton.fromJson(e));
+ buttons = List.from(jsonDecode(buttonsJson)).map((e) => AlertButton.fromJson(e)).toList();
}
}
AwesomeDialog(
- context: _context,
- dialogType: DialogType.INFO,
+ context: _context,
+ dialogType: DialogType.INFO,
animType: AnimType.BOTTOMSLIDE,
- title: title,
- desc: body,
- btnOkText: buttons != null && buttons.isNotEmpty ? buttons[0].title : null,
- btnCancelText: buttons != null && buttons.isNotEmpty ? buttons[1].title: null,
- btnOkOnPress: () {
- if (buttons != null && buttons.isNotEmpty) {
- AlertButton button = buttons[0];
- if (button.action.url != null && button.action.url.isNotEmpty) {
- if (button.action.openInBrowser) {
-
- } else {
- EventBusService
- .getInstance()
- .eventBus
- .fire(new OpenWebPageEvent(button.action.url));
- }
+ title: title,
+ desc: body,
+ btnOkText: buttons != null && buttons.isNotEmpty ? buttons[0].title : "Ok",
+ btnCancelText: buttons != null && buttons.isNotEmpty ? buttons[1].title : null,
+ btnOkOnPress: () async {
+ if (buttons != null && buttons.isNotEmpty) {
+ performAlertAction(buttons[0].action);
+ }
+ else if (action!= null) {
+ performAlertAction(action);
+ }
+ },
+ btnCancelOnPress: () {
+ if (buttons != null && buttons.isNotEmpty) {
+ performAlertAction(buttons[1].action);
}
}
- },
- btnCancelOnPress: () {}
- );
+ ).show();
+ }
+ }
+ }
+
+ static void performAlertAction(AlertAction action) async {
+ if (action.url != null && action.url.isNotEmpty) {
+ if (action.openInBrowser) {
+ if (await canLaunch(action.url)) {
+ await launch(action.url);
+ }
+ } else {
+ EventBusService
+ .getInstance()
+ .eventBus
+ .fire(new OpenWebPageEvent(action.url));
}
}
}
diff --git a/console/generic_app/lib/ui/WebViewPage.dart b/console/generic_app/lib/ui/WebViewPage.dart
index f15bb28bb..a01b641c7 100644
--- a/console/generic_app/lib/ui/WebViewPage.dart
+++ b/console/generic_app/lib/ui/WebViewPage.dart
@@ -9,6 +9,8 @@ import 'package:generic_app/config/CurrentConsoleAppConfig.dart';
import 'package:generic_app/events/OpenWebPageEvent.dart';
import 'package:generic_app/models/LinkConfig.dart';
import 'package:generic_app/services/EventBusService.dart';
+import 'package:generic_app/services/PushNotificationService.dart';
+import 'package:url_launcher/url_launcher.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WebViewPage extends StatefulWidget {
@@ -35,8 +37,9 @@ class _WebViewPageState extends State<WebViewPage> {
.eventBus
.on<OpenWebPageEvent>()
.listen((event) {
-
+ _controller.loadUrl(event.url);
});
+ PushNotificationService.init(context: context);
super.initState();
}
@@ -56,7 +59,15 @@ class _WebViewPageState extends State<WebViewPage> {
javascriptChannels: <JavascriptChannel>[
_postMessageJavascriptChannel(context),
].toSet(),
- navigationDelegate: (navigationRequest) {
+ navigationDelegate: (navigationRequest) async {
+ if (navigationRequest.url.startsWith("webbrowser")) {
+ String navUrl =
+ navigationRequest.url.replaceAll("webbrowser", "https");
+ if (await canLaunch(navUrl)) {
+ await launch(navUrl);
+ }
+ return NavigationDecision.prevent;
+ }
return NavigationDecision.navigate;
},
);
diff --git a/console/generic_app/pubspec.lock b/console/generic_app/pubspec.lock
index b54f723cc..646bfa759 100644
--- a/console/generic_app/pubspec.lock
+++ b/console/generic_app/pubspec.lock
@@ -511,6 +511,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
+ platform_detect:
+ dependency: transitive
+ description:
+ name: platform_detect
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "1.4.0"
plugin_platform_interface:
dependency: transitive
description:
@@ -740,6 +747,48 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
+ url_launcher:
+ dependency: "direct main"
+ description:
+ name: url_launcher
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "5.6.0"
+ url_launcher_linux:
+ dependency: transitive
+ description:
+ name: url_launcher_linux
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.0.1+1"
+ url_launcher_macos:
+ dependency: transitive
+ description:
+ name: url_launcher_macos
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.0.1+7"
+ url_launcher_platform_interface:
+ dependency: transitive
+ description:
+ name: url_launcher_platform_interface
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "1.0.8"
+ url_launcher_web:
+ dependency: transitive
+ description:
+ name: url_launcher_web
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.1.3+2"
+ url_launcher_windows:
+ dependency: transitive
+ description:
+ name: url_launcher_windows
+ url: "https://pub.dartlang.org"
+ source: hosted
+ version: "0.0.1+1"
vector_math:
dependency: transitive
description:
diff --git a/console/generic_app/pubspec.yaml b/console/generic_app/pubspec.yaml
index 717bf0d8a..3b90885ae 100644
--- a/console/generic_app/pubspec.yaml
+++ b/console/generic_app/pubspec.yaml
@@ -39,6 +39,7 @@ dependencies:
show_up_animation: ^1.0.2
awesome_dialog: ^1.2.0
event_bus: ^1.1.1
+ url_launcher: ^5.4.2
dev_dependencies:
flutter_test: