Skip to content

Commit

Permalink
fix(messaging, ios): prevent getInitialMessage from being null at the…
Browse files Browse the repository at this point in the history
… start of the app
  • Loading branch information
Lyokone committed Nov 21, 2022
1 parent ab39d09 commit 22b77b1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
Expand Up @@ -9,14 +9,14 @@ import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:http/http.dart' as http;

import 'firebase_options.dart';
import 'message.dart';
import 'message_list.dart';
import 'permissions.dart';
import 'token_monitor.dart';
import 'firebase_options.dart';

/// Working example of FirebaseMessaging.
/// Please use this in order to verify messages are working in foreground, background & terminated state.
Expand Down Expand Up @@ -171,11 +171,20 @@ class Application extends StatefulWidget {

class _Application extends State<Application> {
String? _token;
String? initialMessage;

@override
void initState() {
super.initState();

FirebaseMessaging.instance.getInitialMessage().then(
(value) => setState(
() {
initialMessage = value?.data.toString();
},
),
);

FirebaseMessaging.onMessage.listen(showFlutterNotification);

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
Expand Down Expand Up @@ -289,13 +298,17 @@ class _Application extends State<Application> {
child: Column(
children: [
MetaCard('Permissions', Permissions()),
MetaCard('Initial Message', Text(initialMessage ?? 'None')),
MetaCard(
'FCM Token',
TokenMonitor((token) {
_token = token;
return token == null
? const CircularProgressIndicator()
: Text(token, style: const TextStyle(fontSize: 12));
: SelectableText(
token,
style: const TextStyle(fontSize: 12),
);
}),
),
ElevatedButton(
Expand Down
Expand Up @@ -22,6 +22,13 @@ @implementation FLTFirebaseMessagingPlugin {
NSObject<FlutterPluginRegistrar> *_registrar;
NSData *_apnsToken;
NSDictionary *_initialNotification;

// Used to track if everything as been initialized before answering
// to the initialNotification request
BOOL _initialNotificationGathered;
BOOL _initialNotificationIdGathered;
FLTFirebaseMethodCallResult *_initialNotificationResult;

NSString *_initialNoticationID;
NSString *_notificationOpenedAppID;

Expand All @@ -43,6 +50,8 @@ - (instancetype)initWithFlutterMethodChannel:(FlutterMethodChannel *)channel
andFlutterPluginRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
self = [super init];
if (self) {
_initialNotificationGathered = NO;
_initialNotificationIdGathered = NO;
_channel = channel;
_registrar = registrar;
// Application
Expand Down Expand Up @@ -109,7 +118,11 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)flutter
[self ensureAPNSTokenSetting];

if ([@"Messaging#getInitialMessage" isEqualToString:call.method]) {
methodCallResult.success([self copyInitialNotification]);
if (_initialNotificationGathered && _initialNotificationIdGathered) {
methodCallResult.success([self copyInitialNotification]);
} else {
_initialNotificationResult = methodCallResult;
}
} else if ([@"Messaging#deleteToken" isEqualToString:call.method]) {
[self messagingDeleteToken:call.arguments withMethodCallResult:methodCallResult];
} else if ([@"Messaging#getAPNSToken" isEqualToString:call.method]) {
Expand Down Expand Up @@ -207,6 +220,11 @@ - (void)application_onDidFinishLaunchingNotification:(nonnull NSNotification *)n
[FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification];
_initialNoticationID = remoteNotification[@"gcm.message_id"];
}
_initialNotificationGathered = YES;
if (_initialNotificationResult != nil && _initialNotificationIdGathered) {
_initialNotificationResult.success([self copyInitialNotification]);
_initialNotificationResult = nil;
}

#if TARGET_OS_OSX
// For macOS we use swizzling to intercept as addApplicationDelegate does not exist on the macOS
Expand Down Expand Up @@ -346,6 +364,12 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
[_channel invokeMethod:@"Messaging#onMessageOpenedApp" arguments:notificationDict];
}

_initialNotificationIdGathered = YES;
if (_initialNotificationResult != nil && _initialNotificationGathered) {
_initialNotificationResult.success([self copyInitialNotification]);
_initialNotificationResult = nil;
}

// Forward on to any other delegates.
if (_originalNotificationCenterDelegate != nil &&
_originalNotificationCenterDelegateRespondsTo.didReceiveNotificationResponse) {
Expand Down Expand Up @@ -1005,7 +1029,6 @@ - (nullable NSDictionary *)copyInitialNotification {
if (_initialNotification != nil &&
[_initialNoticationID isEqualToString:_notificationOpenedAppID]) {
NSDictionary *initialNotificationCopy = [_initialNotification copy];
_initialNotification = nil;
return initialNotificationCopy;
}
}
Expand Down

0 comments on commit 22b77b1

Please sign in to comment.