Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add flutterfire_messenger package #27

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
34 changes: 34 additions & 0 deletions packages/flutterfire_messenger/lib/src/messenger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:firebase_messaging/firebase_messaging.dart';

/// A class that wraps package for sending notifications and messages.
/// Its role is to "send necessary notifications and messages to the user".
///
/// It exposes methods for sending and receiving notifications, displaying
/// in-app messages, and managing settings related to these functions.
class Messenger {
Messenger({
FirebaseMessaging? messaging,
}) : _messaging = messaging ?? FirebaseMessaging.instance;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L11 was not covered by tests

final FirebaseMessaging _messaging;

/// Requests notification permissions.
///
/// On iOS, sets the foreground notification presentation options using
/// [alert], [badge], and [sound], determining how notification appear
/// when the app is in the foreground.
Future<NotificationSettings> requestPermission({
bool alert = false,
bool badge = false,
bool sound = false,
}) async {
// Foreground notification presentation options for iOS.
await _messaging.setForegroundNotificationPresentationOptions(
alert: alert,
badge: badge,
sound: sound,
);
final settings = await _messaging.requestPermission();
return settings;
}
}
18 changes: 18 additions & 0 deletions packages/flutterfire_messenger/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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_in_app_messaging: ^0.7.4+6
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);
});
});
}