Skip to content

Commit

Permalink
fix(firebase_messaging): getInitialMessage returns notification onc…
Browse files Browse the repository at this point in the history
…e & only if pressed for `iOS`. (#7634)
  • Loading branch information
russellwheatley committed Mar 10, 2022
1 parent fb64643 commit 85739b4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 57 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// File generated by FlutterFire CLI.
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;

class DefaultFirebaseOptions {
static FirebaseOptions get currentPlatform {
if (kIsWeb) {
return web;
}
// ignore: missing_enum_constant_in_switch
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return android;
case TargetPlatform.iOS:
return ios;
case TargetPlatform.macOS:
return macos;
}

throw UnsupportedError(
'DefaultFirebaseOptions are not supported for this platform.',
);
}

static const FirebaseOptions web = FirebaseOptions(
apiKey: 'AIzaSyAgUhHU8wSJgO5MVNy95tMT07NEjzMOfz0',
appId: '1:448618578101:web:a8ebc6ab724e6baaac3efc',
messagingSenderId: '448618578101',
projectId: 'react-native-firebase-testing',
authDomain: 'react-native-firebase-testing.firebaseapp.com',
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
storageBucket: 'react-native-firebase-testing.appspot.com',
measurementId: 'G-RF9GF9MQ1F',
);

static const FirebaseOptions android = FirebaseOptions(
apiKey: 'AIzaSyCuu4tbv9CwwTudNOweMNstzZHIDBhgJxA',
appId: '1:448618578101:android:0568e76dc62e89bcac3efc',
messagingSenderId: '448618578101',
projectId: 'react-native-firebase-testing',
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
storageBucket: 'react-native-firebase-testing.appspot.com',
);

static const FirebaseOptions ios = FirebaseOptions(
apiKey: 'AIzaSyAHAsf51D0A407EklG1bs-5wA7EbyfNFg0',
appId: '1:448618578101:ios:0b11ed8263232715ac3efc',
messagingSenderId: '448618578101',
projectId: 'react-native-firebase-testing',
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
storageBucket: 'react-native-firebase-testing.appspot.com',
androidClientId:
'448618578101-a9p7bj5jlakabp22fo3cbkj7nsmag24e.apps.googleusercontent.com',
iosClientId:
'448618578101-evbjdqq9co9v29pi8jcua8bm7kr4smuu.apps.googleusercontent.com',
iosBundleId: 'io.flutter.plugins.firebase.messaging',
);

static const FirebaseOptions macos = FirebaseOptions(
apiKey: 'AIzaSyAHAsf51D0A407EklG1bs-5wA7EbyfNFg0',
appId: '1:448618578101:ios:0b11ed8263232715ac3efc',
messagingSenderId: '448618578101',
projectId: 'react-native-firebase-testing',
databaseURL: 'https://react-native-firebase-testing.firebaseio.com',
storageBucket: 'react-native-firebase-testing.appspot.com',
androidClientId:
'448618578101-a9p7bj5jlakabp22fo3cbkj7nsmag24e.apps.googleusercontent.com',
iosClientId:
'448618578101-evbjdqq9co9v29pi8jcua8bm7kr4smuu.apps.googleusercontent.com',
iosBundleId: 'io.flutter.plugins.firebase.messaging',
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:convert';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_messaging_example/firebase_config.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Expand All @@ -17,6 +16,7 @@ import 'message.dart';
import 'message_list.dart';
import 'permissions.dart';
import 'token_monitor.dart';
import 'firebase_options.dart';

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

Expand All @@ -38,12 +38,7 @@ late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: const FirebaseOptions(
apiKey: 'AIzaSyAHAsf51D0A407EklG1bs-5wA7EbyfNFg0',
appId: '1:448618578101:ios:0b11ed8263232715ac3efc',
messagingSenderId: '448618578101',
projectId: 'react-native-firebase-testing',
),
options: DefaultFirebaseOptions.currentPlatform,
);

// Set the background messaging handler early on, as a named top-level function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ - (void)messaging:(nonnull FIRMessaging *)messaging

- (void)application_onDidFinishLaunchingNotification:(nonnull NSNotification *)notification {
// Setup UIApplicationDelegate.
NSDictionary *remoteNotification =
notification.userInfo[UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification != nil) {
// If remoteNotification exists, it is the notification that opened the app.
_initialNotification =
[FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification];
}

#if TARGET_OS_OSX
// For macOS we use swizzling to intercept as addApplicationDelegate does not exist on the macOS
// registrar Flutter implementation.
Expand Down Expand Up @@ -333,9 +341,6 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
NSDictionary *notificationDict =
[FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification];
[_channel invokeMethod:@"Messaging#onMessageOpenedApp" arguments:notificationDict];
@synchronized(self) {
_initialNotification = notificationDict;
}
}

// Forward on to any other delegates.
Expand Down

2 comments on commit 85739b4

@GKPK
Copy link

@GKPK GKPK commented on 85739b4 Mar 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In iOS(14.4), when the app is in the terminate\force close state and device receives a push notification, the onBackgroundMessage and getInitialMessage both gets called, though user has not tapped on the notification. Hope this is not the expected behaviour.

@jorgeroncero
Copy link
Contributor

@jorgeroncero jorgeroncero commented on 85739b4 Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm what @GKPK commented. in iOS (15.4) getInitialMessage gets called when opening the app even when I don't tap the notification.

Please sign in to comment.