Skip to content

Commit

Permalink
feat: Add flutterfire_messenger package
Browse files Browse the repository at this point in the history
  • Loading branch information
naipaka committed Nov 30, 2023
1 parent d23f430 commit 601e453
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/flutterfire_messenger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
10 changes: 10 additions & 0 deletions packages/flutterfire_messenger/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: "db7ef5bf9f59442b0e200a90587e8fa5e0c6336a"
channel: "stable"

project_type: package
3 changes: 3 additions & 0 deletions packages/flutterfire_messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0

* initial release.
39 changes: 39 additions & 0 deletions packages/flutterfire_messenger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
1 change: 1 addition & 0 deletions packages/flutterfire_messenger/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:altive_lints/altive_lints.yaml
5 changes: 5 additions & 0 deletions packages/flutterfire_messenger/lib/messenger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// flutterfire_messenger with FlutterFire Messaging, Local Notifications,
/// In-App Messaging, and Firebase Installation ID.
library flutterfire_messenger;

export 'src/messenger.dart';
33 changes: 33 additions & 0 deletions packages/flutterfire_messenger/lib/src/messenger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import 'package:firebase_messaging/firebase_messaging.dart';

/// A class that wraps package for sending messages.
/// Its role is to "send necessary messages."
///
/// It exposes methods for sending messages and for configuration.
class Messenger {
Messenger({
FirebaseMessaging? messaging,
}) : _messaging = messaging ?? FirebaseMessaging.instance;

Check warning on line 10 in packages/flutterfire_messenger/lib/src/messenger.dart

View check run for this annotation

Codecov / codecov/patch

packages/flutterfire_messenger/lib/src/messenger.dart#L10

Added line #L10 was not covered by tests

final FirebaseMessaging _messaging;

/// Requests messaging permissions.
///
/// On iOS, sets the foreground messaging presentation options using
/// [alert], [badge], and [sound], determining how messages appear
/// when the app is in the foreground.
Future<NotificationSettings> requestPermission({
bool alert = false,
bool badge = false,
bool sound = false,
}) async {
// Foreground messaging presentation options for iOS.
await _messaging.setForegroundNotificationPresentationOptions(
alert: alert,
badge: badge,
sound: sound,
);
final settings = await _messaging.requestPermission();
return settings;
}
}
17 changes: 17 additions & 0 deletions packages/flutterfire_messenger/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: flutterfire_messenger
description: flutterfire_messenger with FlutterFire Messaging, Local Notifications and In-App Messaging, and Firebase Instance ID.
version: 0.1.0

environment:
sdk: ^3.0.0

dependencies:
firebase_messaging: ^14.7.6
flutter:
sdk: flutter

dev_dependencies:
altive_lints: ^1.8.1
flutter_test:
sdk: flutter
mocktail: ^1.0.1
45 changes: 45 additions & 0 deletions packages/flutterfire_messenger/test/src/messenger_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutterfire_messenger/messenger.dart';
import 'package:mocktail/mocktail.dart';

class MockFirebaseMessaging extends Mock implements FirebaseMessaging {}

class MockNotificationSettings extends Mock implements NotificationSettings {}

void main() {
group('Messenger', () {
test(
'requestPermission should call '
'setForegroundNotificationPresentationOptions and '
'requestPermission on messaging', () async {
final messaging = MockFirebaseMessaging();
final messenger = Messenger(messaging: messaging);
final settings = MockNotificationSettings();

when(
() => messaging.setForegroundNotificationPresentationOptions(
alert: any(named: 'alert'),
badge: any(named: 'badge'),
sound: any(named: 'sound'),
),
).thenAnswer((_) async {});
when(messaging.requestPermission).thenAnswer((_) async => settings);

final got = await messenger.requestPermission();

expect(got, settings);

verify(
() => messaging.setForegroundNotificationPresentationOptions(
alert: any(named: 'alert'),
badge: any(named: 'badge'),
sound: any(named: 'sound'),
),
).called(1);
verify(messaging.requestPermission).called(1);

verifyNoMoreInteractions(messaging);
});
});
}

0 comments on commit 601e453

Please sign in to comment.