Skip to content

Commit

Permalink
slight tweak to Notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
vusters committed Oct 24, 2023
1 parent 46e77ef commit 610bbe7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
11 changes: 6 additions & 5 deletions lib/action/notification_action.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'dart:developer';

import 'package:ensemble/framework/action.dart';
Expand All @@ -12,15 +11,16 @@ 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;

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");
}
Expand All @@ -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.'));
}
}
}
}
20 changes: 14 additions & 6 deletions lib/framework/notification_manager.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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()}');
}
Expand Down

0 comments on commit 610bbe7

Please sign in to comment.