From 610bbe79555c23863c7ac7f3f06f484e229e0025 Mon Sep 17 00:00:00 2001 From: Vu Le Date: Mon, 23 Oct 2023 17:37:02 -0700 Subject: [PATCH] slight tweak to Notifications --- lib/action/notification_action.dart | 11 ++++++----- lib/framework/notification_manager.dart | 20 ++++++++++++++------ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/action/notification_action.dart b/lib/action/notification_action.dart index 218ccb164..39c3fa8b4 100644 --- a/lib/action/notification_action.dart +++ b/lib/action/notification_action.dart @@ -1,4 +1,3 @@ - import 'dart:developer'; import 'package:ensemble/framework/action.dart'; @@ -12,7 +11,8 @@ import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/cupertino.dart'; class GetDeviceTokenAction extends EnsembleAction { - GetDeviceTokenAction({super.initiator, required this.onSuccess, this.onError}); + GetDeviceTokenAction( + {super.initiator, required this.onSuccess, this.onError}); EnsembleAction? onSuccess; EnsembleAction? onError; @@ -20,7 +20,7 @@ class GetDeviceTokenAction extends EnsembleAction { factory GetDeviceTokenAction.fromMap({dynamic payload}) { if (payload is Map) { EnsembleAction? successAction = - EnsembleAction.fromYaml(payload['onSuccess']); + EnsembleAction.fromYaml(payload['onSuccess']); if (successAction == null) { throw LanguageError("onSuccess() is required for Get Token Action"); } @@ -41,7 +41,8 @@ class GetDeviceTokenAction extends EnsembleAction { } if (deviceToken == null && onError != null) { return ScreenController().executeAction(context, onError!, - event: EnsembleEvent(initiator)); + event: EnsembleEvent(initiator, + error: 'Unable to get the device token.')); } } -} \ No newline at end of file +} diff --git a/lib/framework/notification_manager.dart b/lib/framework/notification_manager.dart index c41708092..c0b8b5648 100644 --- a/lib/framework/notification_manager.dart +++ b/lib/framework/notification_manager.dart @@ -1,10 +1,12 @@ import 'dart:developer'; +import 'dart:io' show Platform; import 'package:ensemble/ensemble.dart'; import 'package:ensemble/screen_controller.dart'; import 'package:ensemble/util/utils.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; +import 'package:flutter/foundation.dart'; /// Firebase Push Notification handler class NotificationManager { @@ -35,18 +37,24 @@ class NotificationManager { String? deviceToken; try { // request permission - await FirebaseMessaging.instance.requestPermission( + NotificationSettings settings = await FirebaseMessaging.instance.requestPermission( alert: true, badge: true, sound: true, ); - // need to get APNS token first - await FirebaseMessaging.instance.getAPNSToken(); + if (settings.authorizationStatus == AuthorizationStatus.authorized) { + // on iOS we need to get APNS token first + if (!kIsWeb && Platform.isIOS) { + await FirebaseMessaging.instance.getAPNSToken(); + } + + // get device token + deviceToken = await FirebaseMessaging.instance.getToken(); + return deviceToken; + } + - // then get device token - deviceToken = await FirebaseMessaging.instance.getToken(); - return deviceToken; } on Exception catch (e) { log('Error getting device token: ${e.toString()}'); }