This repository was archived by the owner on Dec 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Java FCM API Snippets #2
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc83c56
Adding FCM sample snippets
hiranya911 844404d
More FCM snippets
hiranya911 210c052
Added sub/unsub snippets
hiranya911 f7704cf
Added dry run mode sample
hiranya911 8c29322
All platforms snippet
hiranya911 700af66
Upgraded admin dependency to latest
hiranya911 4d00a3b
Minor updates based on code review feedback
hiranya911 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
admin/src/main/java/com/google/firebase/example/FirebaseMessagingSnippets.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
/* | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.google.firebase.example; | ||
|
||
import com.google.firebase.messaging.AndroidConfig; | ||
import com.google.firebase.messaging.AndroidNotification; | ||
import com.google.firebase.messaging.ApnsConfig; | ||
import com.google.firebase.messaging.Aps; | ||
import com.google.firebase.messaging.ApsAlert; | ||
import com.google.firebase.messaging.FirebaseMessaging; | ||
import com.google.firebase.messaging.Message; | ||
import com.google.firebase.messaging.Notification; | ||
import com.google.firebase.messaging.TopicManagementResponse; | ||
import com.google.firebase.messaging.WebpushConfig; | ||
import com.google.firebase.messaging.WebpushNotification; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class FirebaseMessagingSnippets { | ||
|
||
public void sendToToken() throws Exception { | ||
// [START send_to_token] | ||
// This registration token comes from the client FCM SDKs. | ||
String registrationToken = "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."; | ||
|
||
// See the "Defining the message" section below for details | ||
// on how to define a message payload. | ||
Message message = Message.builder() | ||
.putData("score", "850") | ||
.putData("time", "2:45") | ||
.setToken(registrationToken) | ||
.build(); | ||
|
||
// Send a message to the device corresponding to the provided | ||
// registration token. | ||
String response = FirebaseMessaging.getInstance().sendAsync(message).get(); | ||
// Response is a message ID string. | ||
System.out.println("Successfully sent message: " + response); | ||
// [END send_to_token] | ||
} | ||
|
||
public void sendToTopic() throws Exception { | ||
// [START send_to_topic] | ||
// The topic name can be optionally prefixed with "/topics/". | ||
String topic = "highScores"; | ||
|
||
// See the "Defining the message" section below for details | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of referencing specific docs headers (which can change) maybe "See docs on defining a message" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
// on how to define a message payload. | ||
Message message = Message.builder() | ||
.putData("score", "850") | ||
.putData("time", "2:45") | ||
.setTopic(topic) | ||
.build(); | ||
|
||
// Send a message to the devices subscribed to the provided topic. | ||
String response = FirebaseMessaging.getInstance().sendAsync(message).get(); | ||
// Response is a message ID string. | ||
System.out.println("Successfully sent message: " + response); | ||
// [END send_to_topic] | ||
} | ||
|
||
public void sendToCondition() throws Exception { | ||
// [START send_to_condition] | ||
// Define a condition which will send to devices which are subscribed | ||
// to either the Google stock or the tech industry topics. | ||
String condition = "'stock-GOOG' in topics || 'industry-tech' in topics"; | ||
|
||
// See the "Defining the message" section below for details | ||
// on how to define a message payload. | ||
Message message = Message.builder() | ||
.setNotification(new Notification( | ||
"$GOOG up 1.43% on the day", | ||
"$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")) | ||
.setCondition(condition) | ||
.build(); | ||
|
||
// Send a message to devices subscribed to the combination of topics | ||
// specified by the provided condition. | ||
String response = FirebaseMessaging.getInstance().sendAsync(message).get(); | ||
// Response is a message ID string. | ||
System.out.println("Successfully sent message: " + response); | ||
// [END send_to_condition] | ||
} | ||
|
||
public void sendDryRun() throws Exception { | ||
Message message = Message.builder() | ||
.putData("score", "850") | ||
.putData("time", "2:45") | ||
.setToken("token") | ||
.build(); | ||
|
||
// [START send_dry_run] | ||
// Send a message in the dry run mode. | ||
boolean dryRun = true; | ||
String response = FirebaseMessaging.getInstance().sendAsync(message, dryRun).get(); | ||
// Response is a message ID string. | ||
System.out.println("Dry run successful: " + response); | ||
// [END send_dry_run] | ||
} | ||
|
||
public Message androidMessage() { | ||
// [START android_message] | ||
Message message = Message.builder() | ||
.setAndroidConfig(AndroidConfig.builder() | ||
.setTtl(3600 * 1000) // 1 hour in milliseconds | ||
.setPriority(AndroidConfig.Priority.NORMAL) | ||
.setNotification(AndroidNotification.builder() | ||
.setTitle("$GOOG up 1.43% on the day") | ||
.setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.") | ||
.setIcon("stock_ticker_update") | ||
.setColor("#f45342") | ||
.build()) | ||
.build()) | ||
.setTopic("industry-tech") | ||
.build(); | ||
// [END android_message] | ||
return message; | ||
} | ||
|
||
public Message apnsMessage() { | ||
// [START apns_message] | ||
Message message = Message.builder() | ||
.setApnsConfig(ApnsConfig.builder() | ||
.putHeader("apns-priority", "10") | ||
.setAps(Aps.builder() | ||
.setAlert(ApsAlert.builder() | ||
.setTitle("$GOOG up 1.43% on the day") | ||
.setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.") | ||
.build()) | ||
.setBadge(42) | ||
.build()) | ||
.build()) | ||
.setTopic("industry-tech") | ||
.build(); | ||
// [END apns_message] | ||
return message; | ||
} | ||
|
||
public Message webpushMessage() { | ||
// [START webpush_message] | ||
Message message = Message.builder() | ||
.setWebpushConfig(WebpushConfig.builder() | ||
.setNotification(new WebpushNotification( | ||
"$GOOG up 1.43% on the day", | ||
"$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.", | ||
"https://my-server/icon.png")) | ||
.build()) | ||
.setTopic("industry-tech") | ||
.build(); | ||
// [END webpush_message] | ||
return message; | ||
} | ||
|
||
public Message allPlatformsMessage() { | ||
// [START multi_platforms_message] | ||
Message message = Message.builder() | ||
.setNotification(new Notification( | ||
"$GOOG up 1.43% on the day", | ||
"$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")) | ||
.setAndroidConfig(AndroidConfig.builder() | ||
.setTtl(3600 * 1000) | ||
.setNotification(AndroidNotification.builder() | ||
.setIcon("stock_ticker_update") | ||
.setColor("#f45342") | ||
.build()) | ||
.build()) | ||
.setApnsConfig(ApnsConfig.builder() | ||
.setAps(Aps.builder() | ||
.setBadge(42) | ||
.build()) | ||
.build()) | ||
.setTopic("industry-tech") | ||
.build(); | ||
// [END multi_platforms_message] | ||
return message; | ||
} | ||
|
||
public void subscribeToTopic() throws Exception { | ||
String topic = "highScores"; | ||
// [START subscribe] | ||
// These registration tokens come from the client FCM SDKs. | ||
List<String> registrationTokens = Arrays.asList( | ||
"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about fake tokens. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
// ... | ||
"ecupwIfBy1w:APA91bFtuMY7MktgxA3Au_Qx7cKqnf..." | ||
); | ||
|
||
// Subscribe the devices corresponding to the registration tokens to the | ||
// topic. | ||
TopicManagementResponse response = FirebaseMessaging.getInstance().subscribeToTopicAsync( | ||
registrationTokens, topic).get(); | ||
// See the TopicManagementResponse reference documentation | ||
// for the contents of response. | ||
System.out.println(response.getSuccessCount() + " tokens were subscribed successfully"); | ||
// [END subscribe] | ||
} | ||
|
||
public void unsubscribeFromTopic() throws Exception { | ||
String topic = "highScores"; | ||
// [START unsubscribe] | ||
// These registration tokens come from the client FCM SDKs. | ||
List<String> registrationTokens = Arrays.asList( | ||
"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", | ||
// ... | ||
"ecupwIfBy1w:APA91bFtuMY7MktgxA3Au_Qx7cKqnf..." | ||
); | ||
|
||
// Unsubscribe the devices corresponding to the registration tokens from | ||
// the topic. | ||
TopicManagementResponse response = FirebaseMessaging.getInstance().unsubscribeFromTopicAsync( | ||
registrationTokens, topic).get(); | ||
// See the TopicManagementResponse reference documentation | ||
// for the contents of response. | ||
System.out.println(response.getSuccessCount() + " tokens were unsubscribed successfully"); | ||
// [END unsubscribe] | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change the constant to
"YOUR_REGISTRATION_TOKEN"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done