Skip to content

Commit

Permalink
fix: Fixed a bug that Apns token was not retrieved.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Dec 25, 2023
1 parent 5f8ed8a commit 3c1b32b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ class PushNotification extends MasamuneControllerBase<PushNotificationValue,
_completer = Completer();
try {
_onLink = onLink;
await getToken(onRetrievedToken: onRetrievedToken);
_listenResponse = await adapter.listen(
onMessage: _onMessage,
onMessageOpenedApp: _onMessageOpenedApp,
);
await getToken(onRetrievedToken: onRetrievedToken);
_listening = true;
_sendLog(PushNotificationLoggerEvent.listen, parameters: {});
_completer?.complete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class FirebasePushNotificationMasamuneAdapter
}) async {
await FirebaseCore.initialize(options: options);
await _messaging.setAutoInitEnabled(true);
await _getApnsToken().timeout(const Duration(seconds: 30));
// ignore: cancel_subscriptions
final onMessageSubscription = FirebaseMessaging.onMessage.listen(
(message) => _onMessage(message, onMessage),
Expand Down Expand Up @@ -204,6 +205,19 @@ class FirebasePushNotificationMasamuneAdapter
);
}

Future<void> _getApnsToken() async {
if (UniversalPlatform.isIOS || UniversalPlatform.isMacOS) {
String? apnsToken;
do {
apnsToken = await _messaging.getAPNSToken();
if (apnsToken != null) {
break;
}
await Future.delayed(const Duration(seconds: 1));
} while (apnsToken == null);
}
}

Future<void> _onMessage(RemoteMessage message,
Future<void> Function(PushNotificationValue value) onMessage) async {
final data = message.data;
Expand Down
2 changes: 1 addition & 1 deletion packages/masamune_notification_firebase/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ packages:
path: "../masamune_notification"
relative: true
source: path
version: "2.3.7"
version: "2.3.8"
masamune_scheduler:
dependency: "direct overridden"
description:
Expand Down

0 comments on commit 3c1b32b

Please sign in to comment.