From c2c91833a958efdf2fce30441cc05e857d30d1c1 Mon Sep 17 00:00:00 2001 From: Eric Windmill Date: Tue, 15 Mar 2022 10:47:16 -0400 Subject: [PATCH 1/2] adds cloud messaging snippets --- .gitignore | 1 + apps/firestore_snippets/.gitignore | 2 ++ apps/firestore_snippets/database.rules.json | 4 +-- apps/firestore_snippets/lib/app.dart | 5 +++ apps/firestore_snippets/pubspec.lock | 35 +++++++++++++++++++++ apps/firestore_snippets/pubspec.yaml | 2 ++ apps/firestore_snippets/storage.rules | 2 +- 7 files changed, 48 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index a0454fe..554c04e 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ # Web related lib/generated_plugin_registrant.dart +apps # Symbolication related app.*.symbols diff --git a/apps/firestore_snippets/.gitignore b/apps/firestore_snippets/.gitignore index a0454fe..09eb2a5 100644 --- a/apps/firestore_snippets/.gitignore +++ b/apps/firestore_snippets/.gitignore @@ -8,6 +8,8 @@ .buildlog/ .history .svn/ +ios/ +windows/ # IntelliJ related *.iml diff --git a/apps/firestore_snippets/database.rules.json b/apps/firestore_snippets/database.rules.json index c0aa595..45b94c1 100644 --- a/apps/firestore_snippets/database.rules.json +++ b/apps/firestore_snippets/database.rules.json @@ -1,6 +1,6 @@ { "rules": { - ".read": "auth != null", - ".write": "auth != null" + ".read": "true", + ".write": "true" } } \ No newline at end of file diff --git a/apps/firestore_snippets/lib/app.dart b/apps/firestore_snippets/lib/app.dart index 62e8220..f511d42 100644 --- a/apps/firestore_snippets/lib/app.dart +++ b/apps/firestore_snippets/lib/app.dart @@ -14,6 +14,7 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_remote_config/firebase_remote_config.dart'; +import 'package:firestore_snippets/snippets/cloud_messaging.dart'; import 'package:firestore_snippets/snippets/firestore.dart'; import 'package:firestore_snippets/snippets/remote_config.dart'; import 'package:flutter/material.dart'; @@ -35,13 +36,17 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { late final FirestoreSnippets _firestoreSnippets; late final RemoteConfigSnippets _remoteConfigSnippets; + late final CloudMessagingSnippets _cloudMessagingSnippets; @override void initState() { _firestoreSnippets = FirestoreSnippets(widget.firestore); _remoteConfigSnippets = RemoteConfigSnippets(widget.firebaseRemoteConfig); + _cloudMessagingSnippets = CloudMessagingSnippets(); _firestoreSnippets.runAll(); + _remoteConfigSnippets.runAll(); + _cloudMessagingSnippets.runAll(); super.initState(); } diff --git a/apps/firestore_snippets/pubspec.lock b/apps/firestore_snippets/pubspec.lock index 5fd3efe..1a4711d 100644 --- a/apps/firestore_snippets/pubspec.lock +++ b/apps/firestore_snippets/pubspec.lock @@ -92,6 +92,41 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.6.1" + firebase_crashlytics: + dependency: "direct main" + description: + name: firebase_crashlytics + url: "https://pub.dartlang.org" + source: hosted + version: "2.5.3" + firebase_crashlytics_platform_interface: + dependency: transitive + description: + name: firebase_crashlytics_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.1" + firebase_messaging: + dependency: "direct main" + description: + name: firebase_messaging + url: "https://pub.dartlang.org" + source: hosted + version: "11.2.10" + firebase_messaging_platform_interface: + dependency: transitive + description: + name: firebase_messaging_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.1" + firebase_messaging_web: + dependency: transitive + description: + name: firebase_messaging_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.9" firebase_remote_config: dependency: "direct main" description: diff --git a/apps/firestore_snippets/pubspec.yaml b/apps/firestore_snippets/pubspec.yaml index 70de676..e82b011 100644 --- a/apps/firestore_snippets/pubspec.yaml +++ b/apps/firestore_snippets/pubspec.yaml @@ -26,6 +26,8 @@ dependencies: firebase_core: ^1.12.0 cloud_firestore: ^3.1.8 firebase_remote_config: ^2.0.2 + firebase_crashlytics: ^2.5.3 + firebase_messaging: ^11.2.10 dev_dependencies: flutter_test: diff --git a/apps/firestore_snippets/storage.rules b/apps/firestore_snippets/storage.rules index 4eda34f..a7db696 100644 --- a/apps/firestore_snippets/storage.rules +++ b/apps/firestore_snippets/storage.rules @@ -2,7 +2,7 @@ rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { - allow read, write: if request.auth!=null; + allow read, write: if true; } } } From 5a6172664b71e2f24d17bb4c34b2da1b42ff5268 Mon Sep 17 00:00:00 2001 From: Eric Windmill Date: Wed, 16 Mar 2022 08:07:21 -0700 Subject: [PATCH 2/2] adds cloud functions and cloud messaging --- .../lib/snippets/cloud_functions.dart | 53 ++++++++++++++ .../lib/snippets/cloud_messaging.dart | 71 +++++++++++++++++++ .../lib/snippets/crashlytics.dart | 29 ++++++++ apps/firestore_snippets/pubspec.lock | 21 ++++++ apps/firestore_snippets/pubspec.yaml | 1 + 5 files changed, 175 insertions(+) create mode 100644 apps/firestore_snippets/lib/snippets/cloud_functions.dart create mode 100644 apps/firestore_snippets/lib/snippets/cloud_messaging.dart create mode 100644 apps/firestore_snippets/lib/snippets/crashlytics.dart diff --git a/apps/firestore_snippets/lib/snippets/cloud_functions.dart b/apps/firestore_snippets/lib/snippets/cloud_functions.dart new file mode 100644 index 0000000..3d6db4f --- /dev/null +++ b/apps/firestore_snippets/lib/snippets/cloud_functions.dart @@ -0,0 +1,53 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ignore_for_file: non_constant_identifier_names, avoid_print +import 'package:cloud_functions/cloud_functions.dart'; +import 'package:firestore_snippets/snippets/snippet.dart'; + +class CloudFunctionsSnippets implements DocSnippet { + late final FirebaseFunctions functions; + + CloudFunctionsSnippets() { + functions = FirebaseFunctions.instance; + } + + @override + void runAll() { + // TODO: implement runAll + } + + // [START call_functions_from_your_app_call_the_function] + Future addMessage(String text) { + final data = { + "text": text, + "push": true, + }; + + final addMessage = functions.httpsCallable("addMessage"); + return addMessage(data); + } + // [END call_functions_from_your_app_call_the_function] + + void callFunctionsFromYourApp_handleErrorsOnTheClient() { + // [START call_functions_from_your_app_handle_errors_on_the_client] + final addMessage = functions.httpsCallable("addMessage"); + addMessage({"text": "message text"}).then( + // Read result of the Cloud Function. + (result) => print(result.data['text']), + onError: (e) => print("Error calling function: $e"), + ); + // [END call_functions_from_your_app_handle_errors_on_the_client] + } +} diff --git a/apps/firestore_snippets/lib/snippets/cloud_messaging.dart b/apps/firestore_snippets/lib/snippets/cloud_messaging.dart new file mode 100644 index 0000000..f6ae613 --- /dev/null +++ b/apps/firestore_snippets/lib/snippets/cloud_messaging.dart @@ -0,0 +1,71 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ignore_for_file: non_constant_identifier_names, avoid_print + +import 'package:firebase_messaging/firebase_messaging.dart'; +import 'package:firestore_snippets/snippets/snippet.dart'; + +class CloudMessagingSnippets implements DocSnippet { + @override + void runAll() { + setUp_retrieveTheCurrentRegistrationToken(); + sendATestMessage_monitorTokenRefresh(); + sendMessagesToMultipleDevices_subscribeClientAppToTopic(); + sendMessagesToMultipleDevices_onMessageReceived(); + sendMessagesToMultipleDevices_onBackgroundMessageReceived(); + } + + void setUp_retrieveTheCurrentRegistrationToken() { + // [START set_up_retrieve_the_current_registration_token] + FirebaseMessaging.instance.getToken().then( + (token) => print("Received token: $token"), + onError: (e) => print("Error completing: $e"), + ); + // [END set_up_retrieve_the_current_registration_token] + } + + void _sendRegistrationToServer(String t) => {}; + + void sendATestMessage_monitorTokenRefresh() { + // [START send_a_test_message_monitor_token_refresh] + FirebaseMessaging.instance.onTokenRefresh.listen((newToken) { + print("Refreshed token: $newToken"); + _sendRegistrationToServer(newToken); + }); + // [END send_a_test_message_monitor_token_refresh] + } + + void sendMessagesToMultipleDevices_subscribeClientAppToTopic() { + // [START send_messages_to_multiple_devices_subscribe_client_app_to_topic] + FirebaseMessaging.instance.subscribeToTopic("weather"); + // [END send_messages_to_multiple_devices_subscribe_client_app_to_topic] + } + + void sendMessagesToMultipleDevices_onMessageReceived() { + // [START send_messages_to_multiple_devices_override_on_message_received] + FirebaseMessaging.onMessage.listen((RemoteMessage event) { + print("Received new message: ${event.data}"); + }); + // [END send_messages_to_multiple_devices_override_on_message_received] + } + + void sendMessagesToMultipleDevices_onBackgroundMessageReceived() { + // [START send_messages_to_multiple_devices_override_on_background_message_received] + FirebaseMessaging.onBackgroundMessage((RemoteMessage event) async { + print("Received new message: ${event.data}"); + }); + // [END send_messages_to_multiple_devices_override_on_background_message_received] + } +} diff --git a/apps/firestore_snippets/lib/snippets/crashlytics.dart b/apps/firestore_snippets/lib/snippets/crashlytics.dart new file mode 100644 index 0000000..ab89f44 --- /dev/null +++ b/apps/firestore_snippets/lib/snippets/crashlytics.dart @@ -0,0 +1,29 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ignore_for_file: non_constant_identifier_names, avoid_print + +import 'package:firebase_crashlytics/firebase_crashlytics.dart'; +import 'package:firestore_snippets/snippets/snippet.dart'; + +class CrashlyticsSnippets implements DocSnippet { + @override + void runAll() {} + + void getStarted_forceATestCrash() { + // [START get_started_force_a_test_crash] + FirebaseCrashlytics.instance.crash(); + // [END get_started_force_a_test_crash] + } +} diff --git a/apps/firestore_snippets/pubspec.lock b/apps/firestore_snippets/pubspec.lock index 1a4711d..5586efb 100644 --- a/apps/firestore_snippets/pubspec.lock +++ b/apps/firestore_snippets/pubspec.lock @@ -57,6 +57,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.6.8" + cloud_functions: + dependency: "direct main" + description: + name: cloud_functions + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.10" + cloud_functions_platform_interface: + dependency: transitive + description: + name: cloud_functions_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "5.1.1" + cloud_functions_web: + dependency: transitive + description: + name: cloud_functions_web + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.9" collection: dependency: transitive description: diff --git a/apps/firestore_snippets/pubspec.yaml b/apps/firestore_snippets/pubspec.yaml index e82b011..e40b5fc 100644 --- a/apps/firestore_snippets/pubspec.yaml +++ b/apps/firestore_snippets/pubspec.yaml @@ -28,6 +28,7 @@ dependencies: firebase_remote_config: ^2.0.2 firebase_crashlytics: ^2.5.3 firebase_messaging: ^11.2.10 + cloud_functions: ^3.2.10 dev_dependencies: flutter_test: