Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit ea3c05d

Browse files
Add code samples for Remote Config operations (#9)
1 parent 4d3866d commit ea3c05d

File tree

3 files changed

+200
-7
lines changed

3 files changed

+200
-7
lines changed

admin/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ repositories {
1515

1616
dependencies {
1717
// Firebase Admin Java SDK
18-
compile 'com.google.firebase:firebase-admin:6.13.0'
18+
compile 'com.google.firebase:firebase-admin:7.1.0'
1919
}

admin/src/main/java/com/google/firebase/example/FirebaseMessagingSnippets.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ public void sendToCondition() throws Exception {
7979

8080
// See documentation on defining a message payload.
8181
Message message = Message.builder()
82-
.setNotification(new Notification(
83-
"$GOOG up 1.43% on the day",
84-
"$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."))
82+
.setNotification(Notification.builder()
83+
.setTitle("$GOOG up 1.43% on the day")
84+
.setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
85+
.build())
8586
.setCondition(condition)
8687
.build();
8788

@@ -165,9 +166,10 @@ public Message webpushMessage() {
165166
public Message allPlatformsMessage() {
166167
// [START multi_platforms_message]
167168
Message message = Message.builder()
168-
.setNotification(new Notification(
169-
"$GOOG up 1.43% on the day",
170-
"$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."))
169+
.setNotification(Notification.builder()
170+
.setTitle("$GOOG up 1.43% on the day")
171+
.setTitle("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
172+
.build())
171173
.setAndroidConfig(AndroidConfig.builder()
172174
.setTtl(3600 * 1000)
173175
.setNotification(AndroidNotification.builder()
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Copyright 2020 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.firebase.example;
17+
18+
import com.google.auth.oauth2.GoogleCredentials;
19+
import com.google.firebase.FirebaseApp;
20+
import com.google.firebase.FirebaseOptions;
21+
import com.google.firebase.remoteconfig.Condition;
22+
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
23+
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException;
24+
import com.google.firebase.remoteconfig.ListVersionsPage;
25+
import com.google.firebase.remoteconfig.Parameter;
26+
import com.google.firebase.remoteconfig.ParameterGroup;
27+
import com.google.firebase.remoteconfig.ParameterValue;
28+
import com.google.firebase.remoteconfig.TagColor;
29+
import com.google.firebase.remoteconfig.Template;
30+
import com.google.firebase.remoteconfig.Version;
31+
32+
import java.io.FileInputStream;
33+
import java.io.IOException;
34+
import java.util.concurrent.ExecutionException;
35+
36+
/**
37+
* Remote Config snippets for documentation.
38+
*
39+
* See:
40+
* https://firebase.google.com/docs/remote-config/automate-rc
41+
*/
42+
public class FirebaseRemoteConfigSnippets {
43+
44+
//Get the current Remote Config Template
45+
public static Template getRemoteConfig() throws ExecutionException, InterruptedException {
46+
// [START get_rc_template]
47+
Template template = FirebaseRemoteConfig.getInstance().getTemplateAsync().get();
48+
// See the ETag of the fetched template.
49+
System.out.println("ETag from server: " + template.getETag());
50+
// [END get_rc_template]
51+
return template;
52+
}
53+
54+
// Modify Remote Config parameters
55+
public static void addParameterToGroup(Template template) {
56+
// [START add_rc_parameter]
57+
template.getParameterGroups().get("new_menu").getParameters()
58+
.put("spring_season", new Parameter()
59+
.setDefaultValue(ParameterValue.inAppDefault())
60+
.setDescription("spring season menu visibility.")
61+
);
62+
// [END add_rc_parameter]
63+
}
64+
65+
// Modify Remote Config conditions
66+
public static void addNewCondition(Template template) {
67+
// [START add_rc_condition]
68+
template.getConditions().add(new Condition("android_en",
69+
"device.os == 'android' && device.country in ['us', 'uk']", TagColor.BLUE));
70+
// [END add_rc_condition]
71+
}
72+
73+
// Validate the Remote Config template
74+
public static void validateTemplate(Template template) throws InterruptedException {
75+
// [START validate_rc_template]
76+
try {
77+
Template validatedTemplate = FirebaseRemoteConfig.getInstance()
78+
.validateTemplateAsync(template).get();
79+
System.out.println("Template was valid and safe to use");
80+
} catch (ExecutionException e) {
81+
if (e.getCause() instanceof FirebaseRemoteConfigException) {
82+
FirebaseRemoteConfigException rcError = (FirebaseRemoteConfigException) e.getCause();
83+
System.out.println("Template is invalid and cannot be published");
84+
System.out.println(rcError.getMessage());
85+
}
86+
}
87+
// [END validate_rc_template]
88+
}
89+
90+
// Publish the Remote Config template
91+
public static void publishTemplate(Template template) throws InterruptedException {
92+
// [START publish_rc_template]
93+
try {
94+
Template publishedTemplate = FirebaseRemoteConfig.getInstance()
95+
.publishTemplateAsync(template).get();
96+
System.out.println("Template has been published");
97+
// See the ETag of the published template.
98+
System.out.println("ETag from server: " + publishedTemplate.getETag());
99+
} catch (ExecutionException e) {
100+
if (e.getCause() instanceof FirebaseRemoteConfigException) {
101+
FirebaseRemoteConfigException rcError = (FirebaseRemoteConfigException) e.getCause();
102+
System.out.println("Unable to publish template.");
103+
System.out.println(rcError.getMessage());
104+
}
105+
}
106+
// [END publish_rc_template]
107+
}
108+
109+
/**
110+
* Remote Config snippets for Manage Remote Config template versions documentation.
111+
*
112+
* See:
113+
* https://firebase.google.com/docs/remote-config/templates
114+
*/
115+
// List all stored versions of the Remote Config template
116+
public static void listAllVersions() throws InterruptedException, ExecutionException {
117+
// [START list_all_versions]
118+
ListVersionsPage page = FirebaseRemoteConfig.getInstance().listVersionsAsync().get();
119+
while (page != null) {
120+
for (Version version : page.getValues()) {
121+
System.out.println("Version: " + version.getVersionNumber());
122+
}
123+
page = page.getNextPage();
124+
}
125+
126+
// Iterate through all versions. This will still retrieve versions in batches.
127+
page = FirebaseRemoteConfig.getInstance().listVersionsAsync().get();
128+
for (Version version : page.iterateAll()) {
129+
System.out.println("Version: " + version.getVersionNumber());
130+
}
131+
// [END list_all_versions]
132+
}
133+
134+
// Retrieve a specific version of the Remote Config template
135+
public static void getRemoteConfigAtVersion(long versionNumber) throws ExecutionException, InterruptedException {
136+
// [START get_rc_template_at_version]
137+
Template template = FirebaseRemoteConfig.getInstance().getTemplateAtVersionAsync(versionNumber).get();
138+
// See the ETag of the fetched template.
139+
System.out.println("Successfully fetched the template with ETag: " + template.getETag());
140+
// [END get_rc_template_at_version]
141+
}
142+
143+
// Roll back to a specific stored version of the Remote Config template
144+
public static void rollbackToVersion(long versionNumber) throws InterruptedException {
145+
// [START rollback_rc_template]
146+
try {
147+
Template template = FirebaseRemoteConfig.getInstance().rollbackAsync(versionNumber).get();
148+
System.out.println("Successfully rolled back to template version: " + versionNumber);
149+
System.out.println("New ETag: " + template.getETag());
150+
} catch (ExecutionException e) {
151+
if (e.getCause() instanceof FirebaseRemoteConfigException) {
152+
FirebaseRemoteConfigException rcError = (FirebaseRemoteConfigException) e.getCause();
153+
System.out.println("Error trying to rollback template.");
154+
System.out.println(rcError.getMessage());
155+
}
156+
}
157+
// [END rollback_rc_template]
158+
}
159+
160+
public static void main(String[] args) throws ExecutionException, InterruptedException {
161+
System.out.println("Hello, FirebaseRemoteConfigSnippets!");
162+
163+
// Initialize Firebase
164+
try {
165+
// [START initialize]
166+
FileInputStream serviceAccount = new FileInputStream("service-account.json");
167+
FirebaseOptions options = FirebaseOptions.builder()
168+
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
169+
.build();
170+
FirebaseApp.initializeApp(options);
171+
// [END initialize]
172+
} catch (IOException e) {
173+
System.out.println("ERROR: invalid service account credentials. See README.");
174+
System.out.println(e.getMessage());
175+
176+
System.exit(1);
177+
}
178+
179+
// Smoke test
180+
Template template = getRemoteConfig();
181+
template.getParameterGroups().put("new_menu", new ParameterGroup());
182+
addParameterToGroup(template);
183+
addNewCondition(template);
184+
validateTemplate(template);
185+
publishTemplate(template);
186+
listAllVersions();
187+
getRemoteConfigAtVersion(6);
188+
rollbackToVersion(6);
189+
System.out.println("Done!");
190+
}
191+
}

0 commit comments

Comments
 (0)