diff --git a/CHANGELOG.md b/CHANGELOG.md index f9c34af..98046ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [1.1.0] - 25/10/2022 + +* Updated example app +* Updated documentation +* Updated some dependencies to latest release + ## [1.0.9] - 04/08/2022 * Updated dependencies diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index 78d0de3..665b67e 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -32,7 +32,7 @@ apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion flutter.compileSdkVersion + compileSdkVersion 33 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -50,7 +50,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.notifications_handler_demo" - targetSdkVersion flutter.targetSdkVersion + targetSdkVersion 33 minSdkVersion flutterMinSdkVersion.toInteger() versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/example/lib/screens/splash_screen.dart b/example/lib/screens/splash_screen.dart index 72aad4e..c691bcb 100644 --- a/example/lib/screens/splash_screen.dart +++ b/example/lib/screens/splash_screen.dart @@ -10,6 +10,7 @@ class SplashScreen extends StatefulWidget { }) : super(key: key); @override + // ignore: library_private_types_in_public_api _SplashScreenState createState() => _SplashScreenState(); } @@ -18,6 +19,9 @@ class _SplashScreenState extends State { void initState() { (() async { await Future.delayed(Duration.zero); + + if (!mounted) return; + Navigator.pushReplacementNamed( context, HomeScreen.id, diff --git a/example/pubspec.lock b/example/pubspec.lock index cf25d97..dee151f 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,6 +1,13 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + _flutterfire_internals: + dependency: transitive + description: + name: _flutterfire_internals + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.5" args: dependency: transitive description: @@ -43,6 +50,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.0" + cloud_firestore_platform_interface: + dependency: transitive + description: + name: cloud_firestore_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "5.8.2" + cloud_firestore_web: + dependency: transitive + description: + name: cloud_firestore_web + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.2" collection: dependency: transitive description: @@ -63,7 +84,7 @@ packages: name: easy_container url: "https://pub.dartlang.org" source: hosted - version: "1.0.3" + version: "1.0.4" fake_async: dependency: transitive description: @@ -91,49 +112,49 @@ packages: name: firebase_core url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "2.1.0" firebase_core_platform_interface: dependency: transitive description: name: firebase_core_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "4.5.0" + version: "4.5.1" firebase_core_web: dependency: transitive description: name: firebase_core_web url: "https://pub.dartlang.org" source: hosted - version: "1.7.1" + version: "2.0.0" firebase_messaging: dependency: transitive description: name: firebase_messaging url: "https://pub.dartlang.org" source: hosted - version: "12.0.1" + version: "14.0.2" firebase_messaging_platform_interface: dependency: transitive description: name: firebase_messaging_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "4.1.0" + version: "4.2.3" firebase_messaging_web: dependency: transitive description: name: firebase_messaging_web url: "https://pub.dartlang.org" source: hosted - version: "3.1.0" + version: "3.2.3" firebase_notifications_handler: dependency: "direct main" description: path: ".." relative: true source: path - version: "1.0.9" + version: "1.1.0" flutter: dependency: "direct main" description: flutter @@ -145,28 +166,28 @@ packages: name: flutter_lints url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.0.1" flutter_local_notifications: dependency: transitive description: name: flutter_local_notifications url: "https://pub.dartlang.org" source: hosted - version: "9.7.0" + version: "12.0.2" flutter_local_notifications_linux: dependency: transitive description: name: flutter_local_notifications_linux url: "https://pub.dartlang.org" source: hosted - version: "0.5.0+1" + version: "1.0.0" flutter_local_notifications_platform_interface: dependency: transitive description: name: flutter_local_notifications_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "5.0.0" + version: "6.0.0" flutter_test: dependency: "direct dev" description: flutter @@ -204,7 +225,7 @@ packages: name: lints url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "2.0.1" matcher: dependency: transitive description: @@ -363,7 +384,7 @@ packages: name: timezone url: "https://pub.dartlang.org" source: hosted - version: "0.8.0" + version: "0.9.0" typed_data: dependency: transitive description: @@ -400,5 +421,5 @@ packages: source: hosted version: "5.3.1" sdks: - dart: ">=2.17.0-0 <3.0.0" + dart: ">=2.17.0 <3.0.0" flutter: ">=2.8.1" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 9b56f7c..666eca1 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -12,8 +12,8 @@ dependencies: flutter: sdk: flutter - firebase_core: ^1.15.0 - easy_container: ^1.0.3 + firebase_core: ^2.1.0 + easy_container: ^1.0.4 firebase_notifications_handler: path: "../" @@ -21,7 +21,7 @@ dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^1.0.4 + flutter_lints: ^2.0.1 flutter: uses-material-design: true diff --git a/lib/src/service.dart b/lib/src/service.dart index a74bd2b..d49fdea 100644 --- a/lib/src/service.dart +++ b/lib/src/service.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'dart:io'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:firebase_notifications_handler/src/app_state.dart'; @@ -7,7 +6,6 @@ import 'package:firebase_notifications_handler/src/constants.dart'; import 'package:firebase_notifications_handler/src/image_downloader.dart'; import 'package:flutter/cupertino.dart' show GlobalKey, NavigatorState, debugPrint; -import 'package:flutter/foundation.dart' show kIsWeb; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; /// Internal implementation class @@ -58,7 +56,7 @@ class PushNotificationService { /// {@macro openedAppFromNotification} static bool _openedAppFromNotification = false; - /// [_openedAppFromNotification] getter. + /// {@macro openedAppFromNotification} static bool get openedAppFromNotification => _openedAppFromNotification; /// {@macro notificationIdCallback} @@ -107,8 +105,7 @@ class PushNotificationService { if (navigatorKey != null) _navigatorKey = navigatorKey; - /// Required only for iOS - if (!kIsWeb && Platform.isIOS) await _fcm.requestPermission(); + await _fcm.requestPermission(); _fcmToken = await initializeFCMToken(vapidKey: vapidKey); @@ -163,13 +160,13 @@ class PushNotificationService { final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); const initializationSettings = InitializationSettings( android: AndroidInitializationSettings('@mipmap/ic_launcher'), - // iOS: IOSInitializationSettings( - // onDidReceiveLocalNotification: (id, title, body, payload) async {}, - // ), + iOS: DarwinInitializationSettings(), ); await flutterLocalNotificationsPlugin.initialize( initializationSettings, - onSelectNotification: (String? payload) async { + onDidReceiveNotificationResponse: (details) { + final payload = details.payload; + if (_onTap != null) { _onTap!( navigatorKey, @@ -179,13 +176,14 @@ class PushNotificationService { } }, ); + return flutterLocalNotificationsPlugin; } /// [_notificationHandler] implementation static Future _notificationHandler( RemoteMessage message, { - AppState? appState, + required AppState appState, }) async { _enableLogs ??= Constants.enableLogs; if (_enableLogs!) { @@ -244,7 +242,7 @@ class PushNotificationService { enableVibration: true, ); - final iOsSpecifics = IOSNotificationDetails(sound: _customSound); + final iOsSpecifics = DarwinNotificationDetails(sound: _customSound); final notificationPlatformSpecifics = NotificationDetails( android: androidSpecifics, @@ -263,6 +261,7 @@ class PushNotificationService { notificationPlatformSpecifics, payload: jsonEncode(message.data), ); + if (_onOpenNotificationArrive != null) { _onOpenNotificationArrive!(_navigatorKey, message.data); } @@ -271,7 +270,7 @@ class PushNotificationService { /// if AppState is open, do not handle onTap here because it will trigger as soon as /// notification arrives, instead handle in initialize method in onSelectNotification callback. else if (_onTap != null) { - _onTap!(_navigatorKey, appState!, message.data); + _onTap!(_navigatorKey, appState, message.data); } } } diff --git a/lib/src/widget.dart b/lib/src/widget.dart index 944dfcd..81de667 100644 --- a/lib/src/widget.dart +++ b/lib/src/widget.dart @@ -37,7 +37,8 @@ class FirebaseNotificationsHandler extends StatefulWidget { static bool get openedAppFromNotification => PushNotificationService.openedAppFromNotification; - /// Returns the default FCM token for this device. + /// On web, a [vapidKey] is required to fetch the default FCM token for the device. + /// The fcm token can be accessed from the [onFCMTokenInitialize] or [onFCMTokenUpdate] callbacks. final String? vapidKey; /// {@template customSound} @@ -46,9 +47,9 @@ class FirebaseNotificationsHandler extends StatefulWidget { /// /// . /// - /// Android: Add the audio file in android/app/src/main/res/raw/___audio_file_here___ + /// Android: Add the audio file in android/app/src/main/res/raw/___audio-file-here___ /// - /// iOS: Add the audio file in Runner/Resources/___audio_file_here___ + /// iOS: Add the audio file in Runner/Resources/___audio-file-here___ /// /// . /// @@ -58,7 +59,7 @@ class FirebaseNotificationsHandler extends StatefulWidget { /// android:name="com.google.firebase.messaging.default_notification_channel_id" /// android:value="ID" /> /// - /// Pass in the same ID in the [channelId] parameter. + /// Pass in the same "ID" in the [channelId] parameter. /// {@endtemplate} final String? customSound; @@ -160,7 +161,7 @@ class FirebaseNotificationsHandler extends StatefulWidget { /// the [navigatorKey.currentState] of the navigator. /// /// * [AppState] is an enum which provides the app state when the - /// notification was tapped. + /// notification arrived. /// /// * [payload] is the payload passed to the notification in the 'data' /// parameter when creating the notification. @@ -279,12 +280,23 @@ class _FirebaseNotificationsHandlerState ); if (!mounted) return; - widget.onFCMTokenInitialize?.call(context, token); + + final navKey = + widget.defaultNavigatorKey ?? PushNotificationService.navigatorKey; + + widget.onFCMTokenInitialize?.call( + navKey.currentContext ?? context, + token, + ); PushNotificationService.onTokenRefresh.listen((token) { - widget.onFCMTokenUpdate?.call(context, token); + widget.onFCMTokenUpdate?.call( + navKey.currentContext ?? context, + token, + ); }); }(); + super.initState(); } diff --git a/pubspec.lock b/pubspec.lock index 01d77cb..e3791c6 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,6 +1,13 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + _flutterfire_internals: + dependency: transitive + description: + name: _flutterfire_internals + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.5" args: dependency: transitive description: @@ -43,6 +50,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.0" + cloud_firestore_platform_interface: + dependency: transitive + description: + name: cloud_firestore_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "5.8.2" + cloud_firestore_web: + dependency: transitive + description: + name: cloud_firestore_web + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.2" collection: dependency: transitive description: @@ -84,42 +105,42 @@ packages: name: firebase_core url: "https://pub.dartlang.org" source: hosted - version: "1.11.0" + version: "2.1.0" firebase_core_platform_interface: dependency: transitive description: name: firebase_core_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "4.5.0" + version: "4.5.1" firebase_core_web: dependency: transitive description: name: firebase_core_web url: "https://pub.dartlang.org" source: hosted - version: "1.7.1" + version: "2.0.0" firebase_messaging: dependency: "direct main" description: name: firebase_messaging url: "https://pub.dartlang.org" source: hosted - version: "12.0.1" + version: "14.0.2" firebase_messaging_platform_interface: dependency: transitive description: name: firebase_messaging_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "4.1.0" + version: "4.2.3" firebase_messaging_web: dependency: transitive description: name: firebase_messaging_web url: "https://pub.dartlang.org" source: hosted - version: "3.1.0" + version: "3.2.3" flutter: dependency: "direct main" description: flutter @@ -138,23 +159,23 @@ packages: name: flutter_local_notifications url: "https://pub.dartlang.org" source: hosted - version: "9.7.0" + version: "12.0.2" flutter_local_notifications_linux: dependency: transitive description: name: flutter_local_notifications_linux url: "https://pub.dartlang.org" source: hosted - version: "0.5.0+1" + version: "1.0.0" flutter_local_notifications_platform_interface: dependency: transitive description: name: flutter_local_notifications_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "5.0.0" + version: "6.0.0" flutter_test: - dependency: "direct dev" + dependency: transitive description: flutter source: sdk version: "0.0.0" @@ -349,7 +370,7 @@ packages: name: timezone url: "https://pub.dartlang.org" source: hosted - version: "0.8.0" + version: "0.9.0" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index dd8e32c..4558380 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: firebase_notifications_handler description: Simple notifications handler which provides callbacks like onTap which really make it easy to handle notification taps and a lot more. -version: 1.0.9 +version: 1.1.0 homepage: https://github.com/rithik-dev/firebase_notifications_handler environment: @@ -11,19 +11,14 @@ dependencies: flutter: sdk: flutter - firebase_messaging: ^12.0.1 - flutter_local_notifications: ^9.7.0 http: ^0.13.5 path_provider: ^2.0.11 + firebase_messaging: ^14.0.2 + flutter_local_notifications: ^12.0.2 dev_dependencies: - flutter_test: - sdk: flutter - flutter_lints: ^2.0.1 false_secrets: - /example/android/app/google-services.json - - /example/lib/firebase_options.dart - -flutter: + - /example/lib/firebase_options.dart \ No newline at end of file