Skip to content

Commit 7b0a911

Browse files
committed
* Updated example app
* Updated documentation * Updated some dependencies to latest release
1 parent b11a9c3 commit 7b0a911

File tree

9 files changed

+117
-59
lines changed

9 files changed

+117
-59
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## [1.1.0] - 25/10/2022
2+
3+
* Updated example app
4+
* Updated documentation
5+
* Updated some dependencies to latest release
6+
17
## [1.0.9] - 04/08/2022
28

39
* Updated dependencies

example/android/app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ apply plugin: 'kotlin-android'
3232
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
3333

3434
android {
35-
compileSdkVersion flutter.compileSdkVersion
35+
compileSdkVersion 33
3636

3737
compileOptions {
3838
sourceCompatibility JavaVersion.VERSION_1_8
@@ -50,7 +50,7 @@ android {
5050
defaultConfig {
5151
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
5252
applicationId "com.example.notifications_handler_demo"
53-
targetSdkVersion flutter.targetSdkVersion
53+
targetSdkVersion 33
5454
minSdkVersion flutterMinSdkVersion.toInteger()
5555
versionCode flutterVersionCode.toInteger()
5656
versionName flutterVersionName

example/lib/screens/splash_screen.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class SplashScreen extends StatefulWidget {
1010
}) : super(key: key);
1111

1212
@override
13+
// ignore: library_private_types_in_public_api
1314
_SplashScreenState createState() => _SplashScreenState();
1415
}
1516

@@ -18,6 +19,9 @@ class _SplashScreenState extends State<SplashScreen> {
1819
void initState() {
1920
(() async {
2021
await Future.delayed(Duration.zero);
22+
23+
if (!mounted) return;
24+
2125
Navigator.pushReplacementNamed(
2226
context,
2327
HomeScreen.id,

example/pubspec.lock

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4+
_flutterfire_internals:
5+
dependency: transitive
6+
description:
7+
name: _flutterfire_internals
8+
url: "https://pub.dartlang.org"
9+
source: hosted
10+
version: "1.0.5"
411
args:
512
dependency: transitive
613
description:
@@ -43,6 +50,20 @@ packages:
4350
url: "https://pub.dartlang.org"
4451
source: hosted
4552
version: "1.1.0"
53+
cloud_firestore_platform_interface:
54+
dependency: transitive
55+
description:
56+
name: cloud_firestore_platform_interface
57+
url: "https://pub.dartlang.org"
58+
source: hosted
59+
version: "5.8.2"
60+
cloud_firestore_web:
61+
dependency: transitive
62+
description:
63+
name: cloud_firestore_web
64+
url: "https://pub.dartlang.org"
65+
source: hosted
66+
version: "3.0.2"
4667
collection:
4768
dependency: transitive
4869
description:
@@ -63,7 +84,7 @@ packages:
6384
name: easy_container
6485
url: "https://pub.dartlang.org"
6586
source: hosted
66-
version: "1.0.3"
87+
version: "1.0.4"
6788
fake_async:
6889
dependency: transitive
6990
description:
@@ -91,49 +112,49 @@ packages:
91112
name: firebase_core
92113
url: "https://pub.dartlang.org"
93114
source: hosted
94-
version: "1.15.0"
115+
version: "2.1.0"
95116
firebase_core_platform_interface:
96117
dependency: transitive
97118
description:
98119
name: firebase_core_platform_interface
99120
url: "https://pub.dartlang.org"
100121
source: hosted
101-
version: "4.5.0"
122+
version: "4.5.1"
102123
firebase_core_web:
103124
dependency: transitive
104125
description:
105126
name: firebase_core_web
106127
url: "https://pub.dartlang.org"
107128
source: hosted
108-
version: "1.7.1"
129+
version: "2.0.0"
109130
firebase_messaging:
110131
dependency: transitive
111132
description:
112133
name: firebase_messaging
113134
url: "https://pub.dartlang.org"
114135
source: hosted
115-
version: "12.0.1"
136+
version: "14.0.2"
116137
firebase_messaging_platform_interface:
117138
dependency: transitive
118139
description:
119140
name: firebase_messaging_platform_interface
120141
url: "https://pub.dartlang.org"
121142
source: hosted
122-
version: "4.1.0"
143+
version: "4.2.3"
123144
firebase_messaging_web:
124145
dependency: transitive
125146
description:
126147
name: firebase_messaging_web
127148
url: "https://pub.dartlang.org"
128149
source: hosted
129-
version: "3.1.0"
150+
version: "3.2.3"
130151
firebase_notifications_handler:
131152
dependency: "direct main"
132153
description:
133154
path: ".."
134155
relative: true
135156
source: path
136-
version: "1.0.9"
157+
version: "1.1.0"
137158
flutter:
138159
dependency: "direct main"
139160
description: flutter
@@ -145,28 +166,28 @@ packages:
145166
name: flutter_lints
146167
url: "https://pub.dartlang.org"
147168
source: hosted
148-
version: "1.0.4"
169+
version: "2.0.1"
149170
flutter_local_notifications:
150171
dependency: transitive
151172
description:
152173
name: flutter_local_notifications
153174
url: "https://pub.dartlang.org"
154175
source: hosted
155-
version: "9.7.0"
176+
version: "12.0.2"
156177
flutter_local_notifications_linux:
157178
dependency: transitive
158179
description:
159180
name: flutter_local_notifications_linux
160181
url: "https://pub.dartlang.org"
161182
source: hosted
162-
version: "0.5.0+1"
183+
version: "1.0.0"
163184
flutter_local_notifications_platform_interface:
164185
dependency: transitive
165186
description:
166187
name: flutter_local_notifications_platform_interface
167188
url: "https://pub.dartlang.org"
168189
source: hosted
169-
version: "5.0.0"
190+
version: "6.0.0"
170191
flutter_test:
171192
dependency: "direct dev"
172193
description: flutter
@@ -204,7 +225,7 @@ packages:
204225
name: lints
205226
url: "https://pub.dartlang.org"
206227
source: hosted
207-
version: "1.0.1"
228+
version: "2.0.1"
208229
matcher:
209230
dependency: transitive
210231
description:
@@ -363,7 +384,7 @@ packages:
363384
name: timezone
364385
url: "https://pub.dartlang.org"
365386
source: hosted
366-
version: "0.8.0"
387+
version: "0.9.0"
367388
typed_data:
368389
dependency: transitive
369390
description:
@@ -400,5 +421,5 @@ packages:
400421
source: hosted
401422
version: "5.3.1"
402423
sdks:
403-
dart: ">=2.17.0-0 <3.0.0"
424+
dart: ">=2.17.0 <3.0.0"
404425
flutter: ">=2.8.1"

example/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ dependencies:
1212
flutter:
1313
sdk: flutter
1414

15-
firebase_core: ^1.15.0
16-
easy_container: ^1.0.3
15+
firebase_core: ^2.1.0
16+
easy_container: ^1.0.4
1717
firebase_notifications_handler:
1818
path: "../"
1919

2020
dev_dependencies:
2121
flutter_test:
2222
sdk: flutter
2323

24-
flutter_lints: ^1.0.4
24+
flutter_lints: ^2.0.1
2525

2626
flutter:
2727
uses-material-design: true

lib/src/service.dart

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import 'dart:convert';
2-
import 'dart:io';
32

43
import 'package:firebase_messaging/firebase_messaging.dart';
54
import 'package:firebase_notifications_handler/src/app_state.dart';
65
import 'package:firebase_notifications_handler/src/constants.dart';
76
import 'package:firebase_notifications_handler/src/image_downloader.dart';
87
import 'package:flutter/cupertino.dart'
98
show GlobalKey, NavigatorState, debugPrint;
10-
import 'package:flutter/foundation.dart' show kIsWeb;
119
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
1210

1311
/// Internal implementation class
@@ -58,7 +56,7 @@ class PushNotificationService {
5856
/// {@macro openedAppFromNotification}
5957
static bool _openedAppFromNotification = false;
6058

61-
/// [_openedAppFromNotification] getter.
59+
/// {@macro openedAppFromNotification}
6260
static bool get openedAppFromNotification => _openedAppFromNotification;
6361

6462
/// {@macro notificationIdCallback}
@@ -107,8 +105,7 @@ class PushNotificationService {
107105

108106
if (navigatorKey != null) _navigatorKey = navigatorKey;
109107

110-
/// Required only for iOS
111-
if (!kIsWeb && Platform.isIOS) await _fcm.requestPermission();
108+
await _fcm.requestPermission();
112109

113110
_fcmToken = await initializeFCMToken(vapidKey: vapidKey);
114111

@@ -163,13 +160,13 @@ class PushNotificationService {
163160
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
164161
const initializationSettings = InitializationSettings(
165162
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
166-
// iOS: IOSInitializationSettings(
167-
// onDidReceiveLocalNotification: (id, title, body, payload) async {},
168-
// ),
163+
iOS: DarwinInitializationSettings(),
169164
);
170165
await flutterLocalNotificationsPlugin.initialize(
171166
initializationSettings,
172-
onSelectNotification: (String? payload) async {
167+
onDidReceiveNotificationResponse: (details) {
168+
final payload = details.payload;
169+
173170
if (_onTap != null) {
174171
_onTap!(
175172
navigatorKey,
@@ -179,13 +176,14 @@ class PushNotificationService {
179176
}
180177
},
181178
);
179+
182180
return flutterLocalNotificationsPlugin;
183181
}
184182

185183
/// [_notificationHandler] implementation
186184
static Future<void> _notificationHandler(
187185
RemoteMessage message, {
188-
AppState? appState,
186+
required AppState appState,
189187
}) async {
190188
_enableLogs ??= Constants.enableLogs;
191189
if (_enableLogs!) {
@@ -244,7 +242,7 @@ class PushNotificationService {
244242
enableVibration: true,
245243
);
246244

247-
final iOsSpecifics = IOSNotificationDetails(sound: _customSound);
245+
final iOsSpecifics = DarwinNotificationDetails(sound: _customSound);
248246

249247
final notificationPlatformSpecifics = NotificationDetails(
250248
android: androidSpecifics,
@@ -263,6 +261,7 @@ class PushNotificationService {
263261
notificationPlatformSpecifics,
264262
payload: jsonEncode(message.data),
265263
);
264+
266265
if (_onOpenNotificationArrive != null) {
267266
_onOpenNotificationArrive!(_navigatorKey, message.data);
268267
}
@@ -271,7 +270,7 @@ class PushNotificationService {
271270
/// if AppState is open, do not handle onTap here because it will trigger as soon as
272271
/// notification arrives, instead handle in initialize method in onSelectNotification callback.
273272
else if (_onTap != null) {
274-
_onTap!(_navigatorKey, appState!, message.data);
273+
_onTap!(_navigatorKey, appState, message.data);
275274
}
276275
}
277276
}

lib/src/widget.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class FirebaseNotificationsHandler extends StatefulWidget {
3737
static bool get openedAppFromNotification =>
3838
PushNotificationService.openedAppFromNotification;
3939

40-
/// Returns the default FCM token for this device.
40+
/// On web, a [vapidKey] is required to fetch the default FCM token for the device.
41+
/// The fcm token can be accessed from the [onFCMTokenInitialize] or [onFCMTokenUpdate] callbacks.
4142
final String? vapidKey;
4243

4344
/// {@template customSound}
@@ -46,9 +47,9 @@ class FirebaseNotificationsHandler extends StatefulWidget {
4647
///
4748
/// .
4849
///
49-
/// Android: Add the audio file in android/app/src/main/res/raw/___audio_file_here___
50+
/// Android: Add the audio file in android/app/src/main/res/raw/___audio-file-here___
5051
///
51-
/// iOS: Add the audio file in Runner/Resources/___audio_file_here___
52+
/// iOS: Add the audio file in Runner/Resources/___audio-file-here___
5253
///
5354
/// .
5455
///
@@ -58,7 +59,7 @@ class FirebaseNotificationsHandler extends StatefulWidget {
5859
/// android:name="com.google.firebase.messaging.default_notification_channel_id"
5960
/// android:value="ID" />
6061
///
61-
/// Pass in the same ID in the [channelId] parameter.
62+
/// Pass in the same "ID" in the [channelId] parameter.
6263
/// {@endtemplate}
6364
final String? customSound;
6465

@@ -160,7 +161,7 @@ class FirebaseNotificationsHandler extends StatefulWidget {
160161
/// the [navigatorKey.currentState] of the navigator.
161162
///
162163
/// * [AppState] is an enum which provides the app state when the
163-
/// notification was tapped.
164+
/// notification arrived.
164165
///
165166
/// * [payload] is the payload passed to the notification in the 'data'
166167
/// parameter when creating the notification.
@@ -279,12 +280,23 @@ class _FirebaseNotificationsHandlerState
279280
);
280281

281282
if (!mounted) return;
282-
widget.onFCMTokenInitialize?.call(context, token);
283+
284+
final navKey =
285+
widget.defaultNavigatorKey ?? PushNotificationService.navigatorKey;
286+
287+
widget.onFCMTokenInitialize?.call(
288+
navKey.currentContext ?? context,
289+
token,
290+
);
283291

284292
PushNotificationService.onTokenRefresh.listen((token) {
285-
widget.onFCMTokenUpdate?.call(context, token);
293+
widget.onFCMTokenUpdate?.call(
294+
navKey.currentContext ?? context,
295+
token,
296+
);
286297
});
287298
}();
299+
288300
super.initState();
289301
}
290302

0 commit comments

Comments
 (0)