Skip to content

Commit

Permalink
* Updated example app
Browse files Browse the repository at this point in the history
* Updated documentation
* Updated some dependencies to latest release
  • Loading branch information
rithik-dev committed Oct 24, 2022
1 parent b11a9c3 commit 7b0a911
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 59 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions example/lib/screens/splash_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SplashScreen extends StatefulWidget {
}) : super(key: key);

@override
// ignore: library_private_types_in_public_api
_SplashScreenState createState() => _SplashScreenState();
}

Expand All @@ -18,6 +19,9 @@ class _SplashScreenState extends State<SplashScreen> {
void initState() {
(() async {
await Future.delayed(Duration.zero);

if (!mounted) return;

Navigator.pushReplacementNamed(
context,
HomeScreen.id,
Expand Down
51 changes: 36 additions & 15 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
6 changes: 3 additions & 3 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ 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: "../"

dev_dependencies:
flutter_test:
sdk: flutter

flutter_lints: ^1.0.4
flutter_lints: ^2.0.1

flutter:
uses-material-design: true
23 changes: 11 additions & 12 deletions lib/src/service.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import 'dart:convert';
import 'dart:io';

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_notifications_handler/src/app_state.dart';
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
Expand Down Expand Up @@ -58,7 +56,7 @@ class PushNotificationService {
/// {@macro openedAppFromNotification}
static bool _openedAppFromNotification = false;

/// [_openedAppFromNotification] getter.
/// {@macro openedAppFromNotification}
static bool get openedAppFromNotification => _openedAppFromNotification;

/// {@macro notificationIdCallback}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -179,13 +176,14 @@ class PushNotificationService {
}
},
);

return flutterLocalNotificationsPlugin;
}

/// [_notificationHandler] implementation
static Future<void> _notificationHandler(
RemoteMessage message, {
AppState? appState,
required AppState appState,
}) async {
_enableLogs ??= Constants.enableLogs;
if (_enableLogs!) {
Expand Down Expand Up @@ -244,7 +242,7 @@ class PushNotificationService {
enableVibration: true,
);

final iOsSpecifics = IOSNotificationDetails(sound: _customSound);
final iOsSpecifics = DarwinNotificationDetails(sound: _customSound);

final notificationPlatformSpecifics = NotificationDetails(
android: androidSpecifics,
Expand All @@ -263,6 +261,7 @@ class PushNotificationService {
notificationPlatformSpecifics,
payload: jsonEncode(message.data),
);

if (_onOpenNotificationArrive != null) {
_onOpenNotificationArrive!(_navigatorKey, message.data);
}
Expand All @@ -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);
}
}
}
26 changes: 19 additions & 7 deletions lib/src/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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___
///
/// .
///
Expand All @@ -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;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}

Expand Down
Loading

0 comments on commit 7b0a911

Please sign in to comment.