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_notification package #25

Closed
wants to merge 1 commit into from
Closed
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_notification/.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_notification/.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_notification/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_notification/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_notification/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_notification/lib/notification.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// flutterfire_notification with FlutterFire Messaging, Local Notifications,
/// In-App Messaging, and Firebase Installation ID.
library flutterfire_notification;

export 'src/notification.dart';
33 changes: 33 additions & 0 deletions packages/flutterfire_notification/lib/src/notification.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 notifications.
/// Its role is to "send necessary notifications."
///
/// It exposes methods for sending notifications and for configuration.
class Notification {
Notification({
FirebaseMessaging? messaging,
}) : _messaging = messaging ?? FirebaseMessaging.instance;

Check warning on line 10 in packages/flutterfire_notification/lib/src/notification.dart

View check run for this annotation

Codecov / codecov/patch

packages/flutterfire_notification/lib/src/notification.dart#L10

Added line #L10 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 notifications 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;
}
}
17 changes: 17 additions & 0 deletions packages/flutterfire_notification/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: flutterfire_notification
description: flutterfire_notification 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutterfire_notification/notification.dart';
import 'package:mocktail/mocktail.dart';

class MockFirebaseMessaging extends Mock implements FirebaseMessaging {}

class MockNotificationSettings extends Mock implements NotificationSettings {}

void main() {
group('Notification', () {
test(
'requestPermission should call '
'setForegroundNotificationPresentationOptions and '
'requestPermission on messaging',
() async {
final messaging = MockFirebaseMessaging();
final notification = Notification(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 notification.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);
},
);
});
}