Skip to content

Commit 85739b4

Browse files
fix(firebase_messaging): getInitialMessage returns notification once & only if pressed for iOS. (#7634)
1 parent fb64643 commit 85739b4

File tree

4 files changed

+84
-57
lines changed

4 files changed

+84
-57
lines changed

packages/firebase_messaging/firebase_messaging/example/lib/firebase_config.dart

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// File generated by FlutterFire CLI.
2+
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
3+
import 'package:flutter/foundation.dart'
4+
show defaultTargetPlatform, kIsWeb, TargetPlatform;
5+
6+
class DefaultFirebaseOptions {
7+
static FirebaseOptions get currentPlatform {
8+
if (kIsWeb) {
9+
return web;
10+
}
11+
// ignore: missing_enum_constant_in_switch
12+
switch (defaultTargetPlatform) {
13+
case TargetPlatform.android:
14+
return android;
15+
case TargetPlatform.iOS:
16+
return ios;
17+
case TargetPlatform.macOS:
18+
return macos;
19+
}
20+
21+
throw UnsupportedError(
22+
'DefaultFirebaseOptions are not supported for this platform.',
23+
);
24+
}
25+
26+
static const FirebaseOptions web = FirebaseOptions(
27+
apiKey: 'AIzaSyAgUhHU8wSJgO5MVNy95tMT07NEjzMOfz0',
28+
appId: '1:448618578101:web:a8ebc6ab724e6baaac3efc',
29+
messagingSenderId: '448618578101',
30+
projectId: 'react-native-firebase-testing',
31+
authDomain: 'react-native-firebase-testing.firebaseapp.com',
32+
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
33+
storageBucket: 'react-native-firebase-testing.appspot.com',
34+
measurementId: 'G-RF9GF9MQ1F',
35+
);
36+
37+
static const FirebaseOptions android = FirebaseOptions(
38+
apiKey: 'AIzaSyCuu4tbv9CwwTudNOweMNstzZHIDBhgJxA',
39+
appId: '1:448618578101:android:0568e76dc62e89bcac3efc',
40+
messagingSenderId: '448618578101',
41+
projectId: 'react-native-firebase-testing',
42+
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
43+
storageBucket: 'react-native-firebase-testing.appspot.com',
44+
);
45+
46+
static const FirebaseOptions ios = FirebaseOptions(
47+
apiKey: 'AIzaSyAHAsf51D0A407EklG1bs-5wA7EbyfNFg0',
48+
appId: '1:448618578101:ios:0b11ed8263232715ac3efc',
49+
messagingSenderId: '448618578101',
50+
projectId: 'react-native-firebase-testing',
51+
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
52+
storageBucket: 'react-native-firebase-testing.appspot.com',
53+
androidClientId:
54+
'448618578101-a9p7bj5jlakabp22fo3cbkj7nsmag24e.apps.googleusercontent.com',
55+
iosClientId:
56+
'448618578101-evbjdqq9co9v29pi8jcua8bm7kr4smuu.apps.googleusercontent.com',
57+
iosBundleId: 'io.flutter.plugins.firebase.messaging',
58+
);
59+
60+
static const FirebaseOptions macos = FirebaseOptions(
61+
apiKey: 'AIzaSyAHAsf51D0A407EklG1bs-5wA7EbyfNFg0',
62+
appId: '1:448618578101:ios:0b11ed8263232715ac3efc',
63+
messagingSenderId: '448618578101',
64+
projectId: 'react-native-firebase-testing',
65+
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
66+
storageBucket: 'react-native-firebase-testing.appspot.com',
67+
androidClientId:
68+
'448618578101-a9p7bj5jlakabp22fo3cbkj7nsmag24e.apps.googleusercontent.com',
69+
iosClientId:
70+
'448618578101-evbjdqq9co9v29pi8jcua8bm7kr4smuu.apps.googleusercontent.com',
71+
iosBundleId: 'io.flutter.plugins.firebase.messaging',
72+
);
73+
}

packages/firebase_messaging/firebase_messaging/example/lib/main.dart

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'dart:convert';
77

88
import 'package:firebase_core/firebase_core.dart';
99
import 'package:firebase_messaging/firebase_messaging.dart';
10-
import 'package:firebase_messaging_example/firebase_config.dart';
1110
import 'package:flutter/foundation.dart';
1211
import 'package:flutter/material.dart';
1312
import 'package:http/http.dart' as http;
@@ -17,6 +16,7 @@ import 'message.dart';
1716
import 'message_list.dart';
1817
import 'permissions.dart';
1918
import 'token_monitor.dart';
19+
import 'firebase_options.dart';
2020

2121
/// Define a top-level named handler which background/terminated messages will
2222
/// call.
@@ -25,7 +25,7 @@ import 'token_monitor.dart';
2525
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
2626
// If you're going to use other Firebase services in the background, such as Firestore,
2727
// make sure you call `initializeApp` before using other Firebase services.
28-
await Firebase.initializeApp(options: DefaultFirebaseConfig.platformOptions);
28+
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
2929
print('Handling a background message ${message.messageId}');
3030
}
3131

@@ -38,12 +38,7 @@ late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
3838
Future<void> main() async {
3939
WidgetsFlutterBinding.ensureInitialized();
4040
await Firebase.initializeApp(
41-
options: const FirebaseOptions(
42-
apiKey: 'AIzaSyAHAsf51D0A407EklG1bs-5wA7EbyfNFg0',
43-
appId: '1:448618578101:ios:0b11ed8263232715ac3efc',
44-
messagingSenderId: '448618578101',
45-
projectId: 'react-native-firebase-testing',
46-
),
41+
options: DefaultFirebaseOptions.currentPlatform,
4742
);
4843

4944
// Set the background messaging handler early on, as a named top-level function

packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ - (void)messaging:(nonnull FIRMessaging *)messaging
194194

195195
- (void)application_onDidFinishLaunchingNotification:(nonnull NSNotification *)notification {
196196
// Setup UIApplicationDelegate.
197+
NSDictionary *remoteNotification =
198+
notification.userInfo[UIApplicationLaunchOptionsRemoteNotificationKey];
199+
if (remoteNotification != nil) {
200+
// If remoteNotification exists, it is the notification that opened the app.
201+
_initialNotification =
202+
[FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification];
203+
}
204+
197205
#if TARGET_OS_OSX
198206
// For macOS we use swizzling to intercept as addApplicationDelegate does not exist on the macOS
199207
// registrar Flutter implementation.
@@ -333,9 +341,6 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
333341
NSDictionary *notificationDict =
334342
[FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification];
335343
[_channel invokeMethod:@"Messaging#onMessageOpenedApp" arguments:notificationDict];
336-
@synchronized(self) {
337-
_initialNotification = notificationDict;
338-
}
339344
}
340345

341346
// Forward on to any other delegates.

0 commit comments

Comments
 (0)