Note that I decided to send push notifications using azure's Notification Hubs. Server-side development. (I don't know the smartphone app.)
From here Get the Java SDK It is an official website for the time being.
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.
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.
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");
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);
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);
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