I want to send mails with attachments using JavaMail, but I am encountering an Exception difficult to understand.
My code is divided in two parts. The first is the class EmailSender, deputied to manage the mail account used to send mail and the system configurations. The second is the class Mail, that manages the single email. (Code at the end of the post)
When creating an EmailSender, the constructor automatically searches the SMTP settings from a list of known settings with the method setServerHost().
When the EmailSender is asked to send a Email, the EmailSender transforms the information contained in the fields of the Email into a MimeMessage by calling the build() method of the Email; this is then sent using the Transport class of JavaMail.
The testing method I use is rather simple:
public static void sendMail(String subject, String body, String to, String urlAttachment) {
System.out.printf("Username:\t");
String username = readString();
System.out.printf("Password:\t");
String password = readString();
EmailSender account = new EmailSender(username, password);
Email mail = new Email(username, to, subject, body);
mail.addAttachment(urlAttachment);
account.sendMail(to, subject, body);
}
The error I get in turn is less so.
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.IOException: Exception writing Multipart
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1365)
at mail.EmailSender.sendMail(EmailSender.java:104)
at mail.EmailSender.sendMail(EmailSender.java:122)
at Test.TestLibraries.sendMail(TestLibraries.java:134)
at Test.TestLibraries.main(TestLibraries.java:51)
Caused by: java.io.IOException: Exception writing Multipart
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:86)
at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1694)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1913)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1315)
... 4 more
Caused by: javax.mail.MessagingException: Empty multipart: multipart/mixed;
boundary="----=_Part_0_2129789493.1581503162040"
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:556)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:84)
... 9 more
What is the problem (and how can I solve it)?
Here is the code:
public class EmailSender {
private String from;
private String password;
private String emailHost;
private Properties properties = System.getProperties();
private Session session;
public static final int serverName = 0;
public static final int serverPort = 1;
public static final int serverAutentication = 2;
public static final String[][] knownServerHostData = new String[][]
{
{ "smtp.mail.yahoo.com", "587", "SLL" } ,
{ "smtp.mail.com", "587", "StartTLS" } ,
{ "smtp.gmail.com", "587", "" } ,
{ "out.virgilio.it", "587", "" }
};
public EmailSender(String username, String password) {
this.from = username;
this.password = password;
this.session = Session.getDefaultInstance(properties);
this.setServerHost(password);
}
public boolean sendMail(String to, String subject, String body) {
return sendMail(new Email(from, to, subject, body));
}
public boolean sendMail(Email email) {
MimeMessage message = email.build(session);
Transport transport = null;
try {
transport = session.getTransport("smtp");
} catch (NoSuchProviderException e) { e.printStackTrace(); closeTransport(transport); }
try {
transport.connect(emailHost, from, password);
} catch (MessagingException e) { e.printStackTrace(); closeTransport(transport); }
try {
transport.sendMessage(message, message.getAllRecipients()); // <== THIS LINE RETURN EXCEPTION
} catch (MessagingException e) { e.printStackTrace(); closeTransport(transport); }
closeTransport(transport);
return true;
}
private void closeTransport(Transport transport) {
try { transport.close();
} catch (MessagingException e) { e.printStackTrace(); }
}
}
public class Email {
private String sender;
private Vector<String> recipients = new Vector<String>();
private Vector<String> cc = new Vector<String>();
private Vector<String> bcc = new Vector<String>();
private String subject;
private String body;
private Vector<String> attachments = new Vector<String>();
public Email(String from, String to, String subject, String body) {
this.sender = from;
this.recipients.add(to);
this.subject = subject;
this.body = body;
}
/** Returns a {#link MimeMessage} ready to be sent by an {#link EmailSender} with all the fields of {#code this} {#link Email}.
*
* #return
*/
public MimeMessage build(Session session) {
MimeMessage message = new MimeMessage(session);
// STEP 1 - Header
// Sets the sender
try { message.setFrom(new InternetAddress(sender));
} catch (AddressException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
// Sets the subject
try { message.setSubject(subject);
} catch (MessagingException e) { e.printStackTrace(); }
// Adds the recipients one by one
int i = 0;
try {
for(i=0 ; i<recipients.size() ; i++)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th recipient gave error."); }
try {
for(i=0 ; i<cc.size() ; i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th cc gave error."); }
try {
for(i=0 ; i<bcc.size() ; i++)
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th bcc gave error."); }
// STEP 2 - Body
// Adds the body
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
try {
messageBodyPart.setContent(message, "text/plain; charset=" +
MimeUtility.quote("us-ascii", HeaderTokenizer.MIME));
} catch (MessagingException e) { e.printStackTrace(); }
try { message.setText(body);
} catch (MessagingException e) { e.printStackTrace(); }
// Adds the attachments
try {
for(i=0 ; i<attachments.size() ; i++) // Preps the attachments
attachFileToMessageMultipart(multipart, attachments.get(i));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
catch (IOException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
// STEP 3 - Appends the MimeMessage's body
try {
message.setContent(multipart);
} catch (MessagingException e1) { e1.printStackTrace(); }
return message;
}
/** This method avoids compatibility problems between JavaMail 1.3 and JavaMail 1.4.
* #throws MessagingException
* #throws IOException
*
*/
private static void attachFileToMessageMultipart(Multipart multipart, String fileUrl) throws MessagingException, IOException {
File file = new File(fileUrl);
if( ! file.isFile() )
throw new IOException("The specified url does not identify a file.");
// JavaMail 1.3
MimeBodyPart attachPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileUrl);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(file.getName());
multipart.addBodyPart(attachPart);
}
Edit: After reading the answer of Bill Shannon, I edited my method build(). The current version is:
MimeMessage message = new MimeMessage(session);
System.out.println("\t Building mail.");
// STEP 1 - Header
// Sets the sender
try { message.setFrom(new InternetAddress(sender));
} catch (AddressException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
// Sets the subject
try { message.setSubject(subject);
} catch (MessagingException e) { e.printStackTrace(); }
System.out.println("\t\t Sender and subject set mail.");
// Adds the recipients one by one
int i = 0;
try {
for(i=0 ; i<recipients.size() ; i++)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th recipient gave error."); }
try {
for(i=0 ; i<cc.size() ; i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th cc gave error."); }
try {
for(i=0 ; i<bcc.size() ; i++)
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc.get(i)));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th bcc gave error."); }
System.out.println("\t\t TO, CC, BCC fields setted.");
// STEP 2 - Body
// Adds the body
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
try {
messageBodyPart.setText(body);
} catch (MessagingException e) { e.printStackTrace(); }
try {
multipart.addBodyPart(messageBodyPart);
} catch (IllegalWriteException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
// try {
// messageBodyPart.setContent(message, "text/plain; charset=" +
// MimeUtility.quote("us-ascii", HeaderTokenizer.MIME));
// } catch (MessagingException e) { e.printStackTrace(); }
System.out.println("\t\t Body attached.");
// Adds the attachments
for(i=0 ; i<attachments.size() ; i++)
{
// Creates a BodyPart representing the attachment
try {
messageBodyPart.attachFile(attachments.get(i));
} catch (MessagingException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
catch (IOException e) { e.printStackTrace(); System.err.println("The " + i + "-th attachment gave error."); }
// Appends the BodyPart to the MultiPart
try {
multipart.addBodyPart(messageBodyPart);
} catch (IllegalWriteException e) { e.printStackTrace(); }
catch (MessagingException e) { e.printStackTrace(); }
}
System.out.println("\t\t Files attached.");
// STEP 3 - Appends the MimeMessage's body
try {
message.setContent(multipart);
} catch (MessagingException e1) { e1.printStackTrace(); }
System.out.println("\t\t MimeMessage created.");
return message;
}
This version does not give Exceptions and the mail is sent... but without the attachment.
I don't understand what you're trying to do here:
messageBodyPart.setContent(message, "text/plain; charset=" +
MimeUtility.quote("us-ascii", HeaderTokenizer.MIME));
I'm sure you don't mean to add the MimeMessage object itself as
the content of a message body part. And you should never need to
use the MimeUtility.quote method in this way.
This statement:
message.setText(body);
sets the entire content of the MimeMessage object to be a plain
text message with the body string as its content. I don't think
that's what you want.
What you want is to use the setText method to set the content of
messageBodyPart object to the string body. Then add the messageBodyPart
object to the multipart object.
After that, you can add all the attachments to the multipart object. Note that you might want to use the MimeBodyPart.atttachFile
method to simplify your code.
Related
Messages published and response received are on two different subjects. Right now I have following set of functionality in my java class. Class is implementing TibrvMsgCallback interface.
How can i make sure that whatever the message is published i am receiving exactly its response?
public class TibcoRVUtility implements TibrvMsgCallback {
public void onMsg(TibrvListener listener, TibrvMsg msg) {
try {
_log.info("Request and Response found");
msgReceived = true;
} catch (final TibrvException ex) {
_log.error("Exception#" + this.getClass().getName() + ".onMsg", ex);
}
}
private void sendMessage(String messageString, final String soType,
final String responseSubject) {
try {
Tibrv.open(Tibrv.IMPL_NATIVE);
TibrvTransport transport = new TibrvRvdTransport(tibcoSetting.getService(), tibcoSetting.getNetwork(),
tibcoSetting.getDaemon());
String inboxName = transport.createInbox();
TibrvMsg msg = new TibrvMsg();
msg.setSendSubject("PUBLISH_SUBJECT");
msg.add("DATA", "DUMMY_MESSAGE");
TibrvListener listener = new TibrvListener(Tibrv.defaultQueue(), this, transport, responseSubject, null);
transport.send(msg);
_log.info("msg" + msg.toString());
_log.info("message successfully sent.");
while (!msgReceived) {
try {
Tibrv.defaultQueue().dispatch();
} catch (InterruptedException ex) {
_log.error("Exception#" + this.getClass().getName() + ".sendMessage", ex);
break;
} catch (TibrvException ex) {
_log.error("Exception#" + this.getClass().getName() + ".sendMessage", ex);
break;
}
}
listener.destroy();
transport.destroy();
} catch (TibrvException e) {
_log.error("Exception#" + this.getClass().getName() + ".sendMessage", e);
}
}
}
When you send a message, add another field
var correlation_id = Guid.NewGuid().ToString();
msg.add("CORRELATION_ID", correlation_id);
and then stash that correlation ID somewhere, in a hash set perhaps.
Have the publisher read the CORRELATION_ID off of the request and add it to the response.
When you receive a message, only process it if it has the ID that you are expecting in the CORRELATION_ID field.
I tried to connect with asynchronous socket and read new messages once per second.
I used sample client code (http://www.java2s.com/Tutorials/Java/Java_Network/0080__Java_Network_Asynchronous_Socket_Channels.htm) and in getTextFromUser method I added sleep method (with 1000 ms) and removed read command from user.
Additionally I added additional logic in ReadWriteHandler method. It started work great, but after about one hour program was suspended and has worked (execute my additional logic) not once per second but one per about 10 minutes.
Do you have any idea what might happen?
Part of code:
public void ConnectAsynchr() {
try {
this.channel = AsynchronousSocketChannel.open();
SocketAddress serverAddr = new InetSocketAddress("localhost", PortNumberAsynchr);
Future<Void> result = channel.connect(serverAddr);
try {
result.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.writeLog("ConnAsynch", "Asynchronous connection succesful established", true);
this.connectAsynch = true;
this.attach = new Attachment();
this.attach.channel = this.channel;
this.attach.buffer = ByteBuffer.allocate(16384);
this.attach.isRead = false;
this.attach.mainThread = Thread.currentThread();
ReadWriteHandler readWriteHandler = new ReadWriteHandler();
this.channel.write(this.attach.buffer, this.attach, readWriteHandler);
try {
this.attach.mainThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
this.writeLog("ERROR", e.toString(), false);
e.printStackTrace();
}
}
catch (IOException e) {
this.writeLog("ERROR", e.toString(), false);
System.out.println(e);
}
}
class Attachment {
AsynchronousSocketChannel channel;
ByteBuffer buffer;
Thread mainThread;
boolean isRead;
}
class ReadWriteHandler implements CompletionHandler<Integer, Attachment> {
#Override
public void completed(Integer result, Attachment attach) {
if (attach.isRead) {
attach.buffer.flip();
Charset cs = Charset.forName("UTF-8");
int limits = attach.buffer.limit();
byte bytes[] = new byte[limits];
attach.buffer.get(bytes, 0, limits);
String msg = new String(bytes, cs);
writeLog("Asynchr Msg rec", msg, false);
AsynchrMessLogic(msg);
try {
msg = this.getTextFromUser();
} catch (Exception e) {
e.printStackTrace();
}
if (msg.equalsIgnoreCase("bye")) {
attach.mainThread.interrupt();
return;
}
attach.buffer.clear();
byte[] data = msg.getBytes(cs);
attach.buffer.put(data);
attach.buffer.flip();
attach.isRead = false; // It is a write
attach.channel.write(attach.buffer, attach, this);
}else {
attach.isRead = true;
attach.buffer.clear();
attach.channel.read(attach.buffer, attach, this);
}
}
#Override
public void failed(Throwable e, Attachment attach) {
e.printStackTrace();
}
private String getTextFromUser() throws Exception{
/*System.out.print("\nPlease enter a message (Bye to quit):");
BufferedReader consoleReader = new BufferedReader(
new InputStreamReader(System.in));
String msg = consoleReader.readLine();
*/
Thread.sleep(threadSleep);
String msg="aaa";
return msg;
}
}
first of all, I'm rather new to socket programming to go easy on me ;).
I have a Java program that uses client-server programming to communicate between 1 or more clients and the server. So the clients can send any number of messages to the server where the messages are dealt with and all is fine so far. Now I want to notify the clients of e.g. database changes on the server side. So for example if one client changes for example table A, the other clients should also be notified about this change.
What I have so far is the following (server):
ExecutorService executor = null;
try (ServerSocket socket = new ServerSocket(port);)
{
executor = Executors.newFixedThreadPool(getThreadCount(5));
while(true)
{
Socket clientSocket = socket.accept();
Runnable worker = new PCRequestMapper(clientSocket);
executor.execute(worker);
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
finally
{
if(executor != null)
{
executor.shutdown();
}
}
The request mapper class then looks like this:
public class PCRequestMapper implements Runnable
{
private Socket client = null;
private static Map<Integer, PCRequestData> requestData = null;
public PCRequestMapper(Socket client)
{
this.client = client;
}
#Override
public void run()
{
try (ObjectInputStream in = new ObjectInputStream(
client.getInputStream());
ObjectOutputStream writer = new ObjectOutputStream(
client.getOutputStream());)
{
System.out.println("Thread started in PCRequestHandler with name: "
+ Thread.currentThread().getName());
Object recObj = in.readObject();
// ToDo Do something
PCBaseRequest req = (PCBaseRequest) recObj;
System.out.println("Req type: " + req.getRequestType() + " name: "
+ req.getName());
PCRequestData data = requestData.get(req.getRequestType());
if(data == null)
{
PCException ex = new PCException();
ex.setStackTrace(new Throwable().getStackTrace());
PCBaseReply reply = getErrorReply("No mapped request handler found in services.xml for request: "+req.getRequestType()+" - "+req.getName(),
PCException.NO_MAPPED_HANDLER, ex);
writer.writeObject(reply);
}
else
{
Class<?> c = Class.forName(data.getMappedClass());
Constructor<?> cons = c.getConstructor();
PCIRequestHandler object = (PCIRequestHandler)cons.newInstance();
PCBaseReply reply = object.heyHo(req);
System.out.println("Writing reply: "+reply.getClass());
writer.writeObject(reply);
}
} catch (IOException ioe)
{
ioe.printStackTrace();
} catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
} catch (NoSuchMethodException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It basically takes a message (request), maps it to a configured class and then that class performs whatever action needed.
On the client side, I have a class called RequestSender, which is used to send arbitrary requests to the server:
public class PCRequestSender
{
private static int getPort(int defaultPort)
{
final String port = PCConfigHandler.getStringProperty("serverPort");
if (null != port)
{
try
{
return Integer.parseInt(port);
} catch (NumberFormatException e)
{
System.out.println("Value of port property"
+ " is not a valid positive integer [" + port + "]."
+ " Reverting to default [" + defaultPort + "].");
}
}
return defaultPort;
}
public static PCBaseReply sendRequest(PCBaseRequest req)
{
PCBaseReply reply = null;
int port = getPort(8081);
String address = PCConfigHandler.getStringProperty("serverAddress");
try (Socket serverSocket = new Socket(address, port);
ObjectOutputStream out = new ObjectOutputStream(
serverSocket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(
serverSocket.getInputStream());)
{
out.writeObject(req);
Object recObj = in.readObject();
reply = (PCBaseReply) recObj;
System.out.println("Reply: "+reply);
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
return reply;
}
}
Now I'm a bit at a loss, because I would also like to constantly listen to a server socket to catch notifications. Do I need another socket on the server side? Is my setup not tooooo ideal?
I'm helpful for any hints...thanks!
Hey how can i display every response from the xmpp server?
I try to send messages but a lot of them get lost on the way so i want to check the response of the server. I am using smack 3.3.1 sending to the facebook xmpp port.
DeliveryReceiptManager and/or MessageEventManager wont show anything so i would like to see everything the server is responding!
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com",5222);
config.setSASLAuthenticationEnabled(true);
XMPPConnection connection = new XMPPConnection(config);
try {
//ESTA LINEA HACE QUE NO DE TIMEOUT
SmackConfiguration.setPacketReplyTimeout(15000);
XMPPConnection.DEBUG_ENABLED = true;
SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM", SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
connection.connect();
String apiKey = "1234567";
String accessToken = "";
connection.login(apiKey, accessToken);
}catch (XMPPException e){
e.printStackTrace();
}
try {
DeliveryReceiptManager deliveryReceiptManager = DeliveryReceiptManager.getInstanceFor(connection);
deliveryReceiptManager.addReceiptReceivedListener(new ReceiptReceivedListener() {
#Override
public void onReceiptReceived(String s, String s2, String s3) {
System.out.println("REVEIVED RESPONCE");
System.out.println(s);
System.out.println(s2);
System.out.println(s3);
}
});
Chat chat = connection.getChatManager().createChat("1234567890#chat.facebook.com", new MessageListener() {
#Override
public void processMessage(Chat chat, Message message) {
if(message.getType() == Message.Type.chat)
System.out.println(chat.getParticipant() + " says: " + message.getBody());
}
});
Message msg = new Message();
msg.setSubject("Invite");
msg.setBody("blablabla");
DeliveryReceiptManager.addDeliveryReceiptRequest(msg);
//MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
chat.sendMessage(msg);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}catch (XMPPException e){
e.printStackTrace();
}
try {
Thread.sleep(10000);
}catch (InterruptedException e) {
e.printStackTrace();
}
Connection.DEBUG_ENABLED = true;
I have used javapns for pushing notifications to ios and android.gcm.server to push notification for android devices. But when I sent one notifications to many devices each device get multiple number of copies of the notification sent. Sometimes this number is 2 and sometimes 3. Hardly it delivers only one which I expect always. Any Ideas ?
My code is as below
public void pushNotificationsToAndroid(String pushMessage,
String contentType, String content, String notification_id,
List<String> devices) {
try {
Sender sender = new Sender(
properties
.getProperty("notification.android.senderIdDemo"));
com.google.android.gcm.server.Message message = new com.google.android.gcm.server.Message.Builder()
.collapseKey("1").timeToLive(3).delayWhileIdle(true)
.addData("message", pushMessage)
.addData("content_type", contentType)
.addData("content", content)
.addData("notification_id", notification_id).build();
MulticastResult result = sender.send(message, devices, 1);
if (result.getResults() == null) {
System.out.println(result.getFailure());
logger.debug("getFailure() of sender.send() method :",
result.getFailure());
}
} catch (Exception exception) {
logger.error("erorr push notification ");
}
System.out.println("sent not at " + new Date());
logger.debug(
"exit pushNotificationsToAndroid() method : current time is ",
new Date());
}
public void pushNotificationsToIOS(String pushMessage, String contentType,
String content, String notification_id, List<String> devices)
{
boolean production = true;
String password = properties
.getProperty("notification.ios.password");
String keyStroke = properties
.getProperty("notification.ios.certFileName");
AppleNotificationServer jksServer = null;
try {
jksServer = new AppleNotificationServerBasicImpl(keyStroke,
password, ConnectionToAppleServer.KEYSTORE_TYPE_JKS,
production);
} catch (KeystoreException keystoreException) {
logger.error("erorr creating jksServer");
}
PushNotificationPayload payload = PushNotificationPayload.complex();
try {
payload.addAlert(pushMessage);
} catch (JSONException e2) {
logger.error("erorr creating payload alert");
}
try {
payload.addCustomDictionary("content_type", contentType);
} catch (JSONException e1) {
logger.error("erorr creating payload content_type");
}
try {
payload.addCustomDictionary("content", content);
} catch (JSONException e1) {
logger.error("erorr creating payload content");
}
try {
payload.addCustomDictionary("notification_id", notification_id);
} catch (JSONException e1) {
logger.error("erorr creating payload notification_id");
}
PushNotificationManager pushManager = new PushNotificationManager();
try {
pushManager.initializeConnection(jksServer);
} catch (CommunicationException | KeystoreException e) {
logger.error("erorr connecting Server");
}
try {
List<PushedNotification> notifications = pushManager
.sendNotifications(payload, Devices.asDevices(devices));
} catch (CommunicationException | KeystoreException e) {
logger.error("erorr push notifications");
}
}
In android official site says about some reasons for duplicate message conditions .