Skip to content

Commit 5a61726

Browse files
committed
adds cloud functions and cloud messaging
1 parent c2c9183 commit 5a61726

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// ignore_for_file: non_constant_identifier_names, avoid_print
16+
import 'package:cloud_functions/cloud_functions.dart';
17+
import 'package:firestore_snippets/snippets/snippet.dart';
18+
19+
class CloudFunctionsSnippets implements DocSnippet {
20+
late final FirebaseFunctions functions;
21+
22+
CloudFunctionsSnippets() {
23+
functions = FirebaseFunctions.instance;
24+
}
25+
26+
@override
27+
void runAll() {
28+
// TODO: implement runAll
29+
}
30+
31+
// [START call_functions_from_your_app_call_the_function]
32+
Future<HttpsCallableResult> addMessage(String text) {
33+
final data = {
34+
"text": text,
35+
"push": true,
36+
};
37+
38+
final addMessage = functions.httpsCallable("addMessage");
39+
return addMessage(data);
40+
}
41+
// [END call_functions_from_your_app_call_the_function]
42+
43+
void callFunctionsFromYourApp_handleErrorsOnTheClient() {
44+
// [START call_functions_from_your_app_handle_errors_on_the_client]
45+
final addMessage = functions.httpsCallable("addMessage");
46+
addMessage({"text": "message text"}).then(
47+
// Read result of the Cloud Function.
48+
(result) => print(result.data['text']),
49+
onError: (e) => print("Error calling function: $e"),
50+
);
51+
// [END call_functions_from_your_app_handle_errors_on_the_client]
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// ignore_for_file: non_constant_identifier_names, avoid_print
16+
17+
import 'package:firebase_messaging/firebase_messaging.dart';
18+
import 'package:firestore_snippets/snippets/snippet.dart';
19+
20+
class CloudMessagingSnippets implements DocSnippet {
21+
@override
22+
void runAll() {
23+
setUp_retrieveTheCurrentRegistrationToken();
24+
sendATestMessage_monitorTokenRefresh();
25+
sendMessagesToMultipleDevices_subscribeClientAppToTopic();
26+
sendMessagesToMultipleDevices_onMessageReceived();
27+
sendMessagesToMultipleDevices_onBackgroundMessageReceived();
28+
}
29+
30+
void setUp_retrieveTheCurrentRegistrationToken() {
31+
// [START set_up_retrieve_the_current_registration_token]
32+
FirebaseMessaging.instance.getToken().then(
33+
(token) => print("Received token: $token"),
34+
onError: (e) => print("Error completing: $e"),
35+
);
36+
// [END set_up_retrieve_the_current_registration_token]
37+
}
38+
39+
void _sendRegistrationToServer(String t) => {};
40+
41+
void sendATestMessage_monitorTokenRefresh() {
42+
// [START send_a_test_message_monitor_token_refresh]
43+
FirebaseMessaging.instance.onTokenRefresh.listen((newToken) {
44+
print("Refreshed token: $newToken");
45+
_sendRegistrationToServer(newToken);
46+
});
47+
// [END send_a_test_message_monitor_token_refresh]
48+
}
49+
50+
void sendMessagesToMultipleDevices_subscribeClientAppToTopic() {
51+
// [START send_messages_to_multiple_devices_subscribe_client_app_to_topic]
52+
FirebaseMessaging.instance.subscribeToTopic("weather");
53+
// [END send_messages_to_multiple_devices_subscribe_client_app_to_topic]
54+
}
55+
56+
void sendMessagesToMultipleDevices_onMessageReceived() {
57+
// [START send_messages_to_multiple_devices_override_on_message_received]
58+
FirebaseMessaging.onMessage.listen((RemoteMessage event) {
59+
print("Received new message: ${event.data}");
60+
});
61+
// [END send_messages_to_multiple_devices_override_on_message_received]
62+
}
63+
64+
void sendMessagesToMultipleDevices_onBackgroundMessageReceived() {
65+
// [START send_messages_to_multiple_devices_override_on_background_message_received]
66+
FirebaseMessaging.onBackgroundMessage((RemoteMessage event) async {
67+
print("Received new message: ${event.data}");
68+
});
69+
// [END send_messages_to_multiple_devices_override_on_background_message_received]
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// ignore_for_file: non_constant_identifier_names, avoid_print
16+
17+
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
18+
import 'package:firestore_snippets/snippets/snippet.dart';
19+
20+
class CrashlyticsSnippets implements DocSnippet {
21+
@override
22+
void runAll() {}
23+
24+
void getStarted_forceATestCrash() {
25+
// [START get_started_force_a_test_crash]
26+
FirebaseCrashlytics.instance.crash();
27+
// [END get_started_force_a_test_crash]
28+
}
29+
}

apps/firestore_snippets/pubspec.lock

+21
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ packages:
5757
url: "https://pub.dartlang.org"
5858
source: hosted
5959
version: "2.6.8"
60+
cloud_functions:
61+
dependency: "direct main"
62+
description:
63+
name: cloud_functions
64+
url: "https://pub.dartlang.org"
65+
source: hosted
66+
version: "3.2.10"
67+
cloud_functions_platform_interface:
68+
dependency: transitive
69+
description:
70+
name: cloud_functions_platform_interface
71+
url: "https://pub.dartlang.org"
72+
source: hosted
73+
version: "5.1.1"
74+
cloud_functions_web:
75+
dependency: transitive
76+
description:
77+
name: cloud_functions_web
78+
url: "https://pub.dartlang.org"
79+
source: hosted
80+
version: "4.2.9"
6081
collection:
6182
dependency: transitive
6283
description:

apps/firestore_snippets/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies:
2828
firebase_remote_config: ^2.0.2
2929
firebase_crashlytics: ^2.5.3
3030
firebase_messaging: ^11.2.10
31+
cloud_functions: ^3.2.10
3132

3233
dev_dependencies:
3334
flutter_test:

0 commit comments

Comments
 (0)