Skip to content

Commit e349afa

Browse files
authored
Cloud functions (#3)
* adds cloud functions * adds cloud messaging snippets Co-authored-by: Eric Windmill <[email protected]>
1 parent 3f63796 commit e349afa

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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:cloud_functions/cloud_functions.dart';
18+
import 'package:firebase_snippets_app/snippets/snippet_base.dart';
19+
20+
class CloudFunctionsSnippets implements DocSnippet {
21+
late final FirebaseFunctions functions;
22+
23+
CloudFunctionsSnippets() {
24+
functions = FirebaseFunctions.instance;
25+
}
26+
27+
@override
28+
void runAll() {
29+
addMessage('text');
30+
callFunctionsFromYourApp_handleErrorsOnTheClient();
31+
}
32+
33+
// [START call_functions_from_your_app_call_the_function]
34+
Future<HttpsCallableResult> addMessage(String text) {
35+
final data = {
36+
"text": text,
37+
"push": true,
38+
};
39+
40+
final addMessage = functions.httpsCallable("addMessage");
41+
return addMessage(data);
42+
}
43+
// [END call_functions_from_your_app_call_the_function]
44+
45+
void callFunctionsFromYourApp_handleErrorsOnTheClient() {
46+
// [START call_functions_from_your_app_handle_errors_on_the_client]
47+
final addMessage = functions.httpsCallable("addMessage");
48+
addMessage({"text": "message text"}).then(
49+
// Read result of the Cloud Function.
50+
(result) => print(result.data['text']),
51+
onError: (e) => print("Error calling function: $e"),
52+
);
53+
// [END call_functions_from_your_app_handle_errors_on_the_client]
54+
}
55+
}
Lines changed: 71 additions & 0 deletions
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:firebase_snippets_app/snippets/snippet_base.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+
}

packages/firebase_snippets_app/pubspec.lock

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ packages:
6464
url: "https://pub.dartlang.org"
6565
source: hosted
6666
version: "2.6.8"
67+
cloud_functions:
68+
dependency: "direct main"
69+
description:
70+
name: cloud_functions
71+
url: "https://pub.dartlang.org"
72+
source: hosted
73+
version: "3.2.10"
74+
cloud_functions_platform_interface:
75+
dependency: transitive
76+
description:
77+
name: cloud_functions_platform_interface
78+
url: "https://pub.dartlang.org"
79+
source: hosted
80+
version: "5.1.1"
81+
cloud_functions_web:
82+
dependency: transitive
83+
description:
84+
name: cloud_functions_web
85+
url: "https://pub.dartlang.org"
86+
source: hosted
87+
version: "4.2.9"
6788
collection:
6889
dependency: transitive
6990
description:
@@ -113,6 +134,27 @@ packages:
113134
url: "https://pub.dartlang.org"
114135
source: hosted
115136
version: "1.6.1"
137+
firebase_messaging:
138+
dependency: "direct main"
139+
description:
140+
name: firebase_messaging
141+
url: "https://pub.dartlang.org"
142+
source: hosted
143+
version: "11.2.11"
144+
firebase_messaging_platform_interface:
145+
dependency: transitive
146+
description:
147+
name: firebase_messaging_platform_interface
148+
url: "https://pub.dartlang.org"
149+
source: hosted
150+
version: "3.2.1"
151+
firebase_messaging_web:
152+
dependency: transitive
153+
description:
154+
name: firebase_messaging_web
155+
url: "https://pub.dartlang.org"
156+
source: hosted
157+
version: "2.2.9"
116158
firebase_remote_config:
117159
dependency: "direct main"
118160
description:

packages/firebase_snippets_app/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ dependencies:
2626
firebase_core: ^1.12.0
2727
cloud_firestore: ^3.1.8
2828
firebase_remote_config: ^2.0.2
29+
firebase_messaging: ^11.2.11
30+
cloud_functions: ^3.2.10
2931

3032
dev_dependencies:
3133
flutter_test:

0 commit comments

Comments
 (0)