[JAVA] Push delivery from Spring application to Firebase Cloud Messaging

Firebase

A cross-platform messaging solution provided by Google to ensure the delivery of messages for free.

In Google I / O 2017 that was held the other day, it seems that the analysis system functions have been greatly improved in addition to the basic functions.

This time, there were some addictive points when calling such Firebase Cloud Messaging (hereinafter, FCM) from the server side, so I will summarize it as a memorandum.

Differences between FCM and Firebase Notifications

FCM

A mechanism that enables flexible push distribution to a wide range of targets such as mobile terminals and web browsers. Basically, it is necessary to implement the transmission process in each language.

Firebase Notifications

A mechanism that allows you to send push notifications from the Firebase dashboard. It's easy because it doesn't have to be implemented, but only simple push notifications are possible.

This time I will briefly summarize the former.

Request information

--FCM endpoint: https://fcm.googleapis.com/fcm/send --Method: POST --Request header: --Authorization: key = {server key}

Response information

When HTTP communication is performed in the above format, the following response information will be returned. Official: FCM Response

FCM response example


{
 "multicast_id":6782339717028231855,
 "success":0,
 "failure":1,
 "canonical_ids":0,
 "results":[
  {
   "error":"InvalidRegistration"
  }
 ]
}

So, make a model to receive the above.

FirebaseResponse.java


public class FirebaseResponse 
{
	public Long multicast_id;
	public Integer success;
	public Integer failure;
	public Integer canonical_ids;
	public List<Result> results;

	class Result 
	{
		private String message_id;
		private String registration_id;
		private String error;
	}
	//Accessor is omitted because lombok is used
}

Implementation

FirebaseClient.java



public class 
{
	private final String SAMPLE_MESSAGE= "{\"to\": \"xxx\",\"notification\": {\"title\": \"It's a headline\",\"body\": \"It's the text\"},\"data\": {\"greet\": \"Hello. World!!\"}}";
	private final String FCM_END_POINT = "https://fcm.googleapis.com/fcm/send";
	private final String SERVER_KEY = "Server key";

	RestTemplate template = new RestTemplate();

	HttpHeaders headers = new HttpHeaders();
	Charset utf8 = Charset.forName("UTF-8");
	MediaType mediaType = new MediaType(MediaType.APPLICATION_JSON, utf8);
	headers.setContentType(mediaType);
	headers.set( "Authorization", String.format("key=%s", SERVER_KEY) );

	HttpEntity< String > entity = new HttpEntity< String >( SAMPLE_MESSAGE, httpHeaders );

	FirebaseResponse response = template.postForObject(FCM_END_POINT , entity, FirebaseResponse.class );
	LOGGER.info(String.format("Firebase Push Result. header:[%s], message:[%s], response:[%s]", httpHeaders, SAMPLE_MESSAGE, response ));

}

Request processing and response acquisition are possible like this.

Garbled notifications received on the device

I was addicted to the part where the Japanese part of the push notification that arrived at the terminal was garbled like "?????????".

this is


Charset utf8 = Charset.forName("UTF-8");
MediaType mediaType = new MediaType(MediaType.APPLICATION_JSON, utf8);
headers.setContentType(mediaType);

It was because I didn't do the above. (It is also written firmly in Official. Orz)

Task

In the case of Firebase Notifications, the notification sent from the dashboard remains as a log, so data analysis can easily grasp the sending status, but in the case of FCM, only the sending result is returned, so the sending notification history management etc. is unique It needs to be taken into consideration as soon as it is developed in.

That said, it doesn't have that much information, so I think it's a small burden.

At the end

It's a service that Google is putting a lot of effort into, so I thought it was a technology that could become a weapon for living in the IT society in the future by wearing it.

However, knowledge of smartphone application development is essential to receive it on the terminal, so for the time being, I think I should learn from browser push notifications. .. ..

At that time, I will summarize it as an article again.

Well then.

Recommended Posts

Push delivery from Spring application to Firebase Cloud Messaging
Make PUSH notifications with images in Firebase Cloud Messaging
Try Spring Boot from 0 to 100.
Rewriting from applet to application
How to push from Tarminal to GitHub
Upgrade spring boot from 1.5 series to 2.0 series
Story when moving from Spring Boot 1.5 to 2.1
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Transition from Struts2 to Spring MVC (Controller)
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
From creating a Spring Boot project to running an application with VS Code
[Reverse lookup] Spring Security (updated from time to time)
Connect to Aurora (MySQL) from a Java application
Touch all Spring "Guides" (updated from time to time)
From building an AWS cloud environment to deploying a Spring Boot app (for beginners)