Send push notifications using Notification Hubs in Java

Note that I decided to send push notifications using azure's Notification Hubs. Server-side development. (I don't know the smartphone app.)

Get Java SDK

From here Get the Java SDK It is an official website for the time being.

Compile and build

Since maven command is used, if maven is not installed, see here Just run the maven command in the folder where pom.xml is

cd NotificationHubs
mvn package

Then Notification-Hubs-java-sdk-0.1.0.jar can be created under target. Place this in a folder that is in your path.

How to use

It is supposed to be sent by specifying a tag. It is assumed that the token and tag are registered in Notification Hubs in advance.

The token looks like an email address The tag is an image like an email address.

If you specify a tag and send a push notification, the push notification will be sent to all tokens associated with that tag.

Send push notification (immediate transmission)

String connectionString = "Endpoint=sb://xxxxx.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=~~~~~";
String hubPath = "Name of Notification hubs";

NotificationHub hub = new NotificationHub(connectionString, hubPath);
String message = "push notification";

String body = String.format("{\"data\":{\"message\":\"%s\"}}", message);

//For android terminals
Notification notifiation = Notification.createFcmNotifiation(body);

hub.sendNotification(notifiation, "Tag name");

Push notification transmission (date and time specified transmission)

String connectionString = "Endpoint=sb://xxxxx.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=~~~~~";
String hubPath = "Name of Notification hubs";

NotificationHub hub = new NotificationHub(connectionString, hubPath);
String message = "push notification";

String body = String.format("{\"data\":{\"message\":\"%s\"}}", message);

//For android terminals
Notification notifiation = Notification.createFcmNotifiation(body);

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse("2020-01-01 00:00:00");

hub.scheduleNotification(notifiation, "Tag name", date);

Token and tag registration

I mentioned that it is assumed that the token and tag are registered in Notification Hubs in advance, You can also register tokens and tags on the server side. (If you know the token and tag)

//For Android devices
Registration reg = new FcmRegistration("token");
reg.getTags().add("Tag name");

NotificationHub hub = new NotificationHub(connectionString, hubPath);
Registration registreg = hub.createRegistration(reg);

Delete tokens and tags

If you want to delete the token and tag, specify the tag or specify the RegistrationId.

-Please note that when deleting with a tag, unintended tokens will also be deleted. -RegistrationId is packed in the registration result (registreg in the above source) by registering tokens and tags. This value allows you to uniquely identify the token and tag.

NotificationHub hub = new NotificationHub(connectionString, hubPath);

//Delete by specifying a tag
CollectionResult collectionResult = hub.getRegistrationsByTag(tag);
for (Registration registration:collectionResult.getRegistrations()) {
    hub.deleteRegistration(registration.getRegistrationId());
}

//Delete by specifying RegistrationId
hub.deleteRegistration(registreg.getRegistrationId());

Recommended Posts

Send push notifications using Notification Hubs in Java
I tried to implement Firebase push notification in Java
Try using RocksDB in Java
HTTPS connection using tls1.2 in Java 6
I tried using JWT in Java
Try using the Stream API in Java
Call the Windows Notification API in Java
[Java] Send an email using Amazon SES
Using JavaScript from Java in Rhino 2021 version
ERRORCODE = -4471 occurs in Java application using Db2.
Try using JSON format API in Java
Read Felica using RC-S380 (PaSoRi) in Java
How to send push notifications on AWS
ChatWork4j for using the ChatWork API in Java
I want to send an email in Java.
[Java] API creation using Jerjey (Jax-rs) in eclipse
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Match IP addresses using regular expressions in Java
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Try using the COTOHA API parsing in Java
NLP4J [001b] Morphological analysis in Java (using kuromoji)
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
I tried using Google Cloud Vision API in Java
It's late! Try implementing Android Notification in Java (Beginner)
Make PUSH notifications with images in Firebase Cloud Messaging
How to convert A to a and a to A using AND and OR in Java
I tried using an extended for statement in Java
Try global hooking in Java using the JNativeHook library
Differences in code when using the length system in Java