Skip to content

Instantly share code, notes, and snippets.

@mihanvr
Created December 12, 2016 13:27
Show Gist options
  • Save mihanvr/b87b0b9b8096a15e2923580369084852 to your computer and use it in GitHub Desktop.
Save mihanvr/b87b0b9b8096a15e2923580369084852 to your computer and use it in GitHub Desktop.
Firebase push notification sender
package sevenwinds.fool;
import lombok.Builder;
import lombok.Data;
import lombok.Singular;
import lombok.Value;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
/**
* Created by Miha on 12.12.2016.
*
* dep org.springframework:spring-web
* dep org.projectlombok:lombok
*/
@Data
public class FirebasePushNotificationSender {
String url = "https://fcm.googleapis.com/fcm/send";
String serverApiKey;
private final RestTemplate restTemplate;
private final HttpHeaders headers;
public FirebasePushNotificationSender() {
restTemplate = new RestTemplate();
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("answer", "json");
}
public void setServerApiKey(String serverApiKey) {
this.serverApiKey = serverApiKey;
headers.set("Authorization", "key=" + serverApiKey);
}
public PushResponse send(PushMessage message) {
HttpEntity<PushMessage> entity = new HttpEntity<>(message, headers);
ResponseEntity<PushResponse> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, PushResponse.class);
return exchange.getBody();
}
public static void main(String[] args) {
FirebasePushNotificationClient androidPush = new FirebasePushNotificationClient();
androidPush.setServerApiKey("AAAAs7MNMC***");
PushMessage message = PushMessage.builder()
.to("cWD8Y***")
.priority(Priority.normal)
.dataItem("message", "hello")
.dataItem("sender", "friend")
.notification(NotificationParam.builder().body("Hello").title("New message").build())
.build();
PushResponse send = androidPush.send(message);
System.out.println(send);
}
/**
* @see <a href="https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json">Firebase doc 1</a>
* @see <a href="https://firebase.google.com/docs/cloud-messaging/concept-options">Firebase doc 2</a>
* */
@Builder
@Value
public static class PushMessage {
String to;
Priority priority;
Integer time_to_live;
String collapse_key;
Boolean content_available;
@Singular("dataItem") Map<String, Object> data;
NotificationParam notification;
}
@Data
public static class PushResponse {
long multicast_id;
int success;
int failure;
int canonical_ids;
List<ResponseResults> results;
}
@Data
public static class ResponseResults {
String error;
String message_id;
}
@Builder
@Value
public static class NotificationParam {
//<common>
String title;
String body;
String sound;
String click_action;
String body_loc_key;
String body_loc_args;
String title_loc_key;
String title_loc_args;
//</common>
//<ios>
String badge;
//</ios>
//<adnroid>
String icon;
String tag;
String color;
//</android>
}
public enum Priority {
normal,
high,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment