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

Commit cc83c56

Browse files
committed
Adding FCM sample snippets
1 parent d880303 commit cc83c56

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2018 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+
17+
package com.google.firebase.example;
18+
19+
import com.google.firebase.messaging.FirebaseMessaging;
20+
import com.google.firebase.messaging.Message;
21+
import com.google.firebase.messaging.Notification;
22+
23+
public class FirebaseMessagingSnippets {
24+
25+
public void sendToToken() throws Exception {
26+
// [START send_to_token]
27+
// This registration token comes from the client FCM SDKs.
28+
String registrationToken = "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...";
29+
30+
// See the "Defining the message" section below for details
31+
// on how to define a message payload.
32+
Message message = Message.builder()
33+
.putData("score", "850")
34+
.putData("time", "2:45")
35+
.setToken(registrationToken)
36+
.build();
37+
38+
// Send a message to the device corresponding to the provided
39+
// registration token.
40+
String response = FirebaseMessaging.getInstance().sendAsync(message).get();
41+
// Response is a message ID string.
42+
System.out.println("Successfully sent message: " + response);
43+
// [END send_to_token]
44+
}
45+
46+
public void sendToTopic() throws Exception {
47+
// [START send_to_topic]
48+
// The topic name can be optionally prefixed with "/topics/".
49+
String topic = "highScores";
50+
51+
// See the "Defining the message" section below for details
52+
// on how to define a message payload.
53+
Message message = Message.builder()
54+
.putData("score", "850")
55+
.putData("time", "2:45")
56+
.setTopic(topic)
57+
.build();
58+
59+
// Send a message to the devices subscribed to the provided topic.
60+
String response = FirebaseMessaging.getInstance().sendAsync(message).get();
61+
// Response is a message ID string.
62+
System.out.println("Successfully sent message: " + response);
63+
// [END send_to_topic]
64+
}
65+
66+
public void sendToCondition() throws Exception {
67+
// [START send_to_condition]
68+
// Define a condition which will send to devices which are subscribed
69+
// to either the Google stock or the tech industry topics.
70+
String condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
71+
72+
// See the "Defining the message" section below for details
73+
// on how to define a message payload.
74+
Message message = Message.builder()
75+
.setNotification(new Notification(
76+
"$GOOG up 1.43% on the day",
77+
"$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."))
78+
.setCondition(condition)
79+
.build();
80+
81+
// Send a message to devices subscribed to the combination of topics
82+
// specified by the provided condition.
83+
String response = FirebaseMessaging.getInstance().sendAsync(message).get();
84+
// Response is a message ID string.
85+
System.out.println("Successfully sent message: " + response);
86+
// [END send_to_condition]
87+
}
88+
89+
}

0 commit comments

Comments
 (0)