Friday, July 5, 2013

How to Push Notifications to IOS devices from java

For this we can use javapns jar.

First of all we need to have a certificate file type of .p12  . Follow the steps in https://code.google.com/p/javapns/wiki/PreparingCertificates  for creating this .p12 file.

But the sad point , It is a real headache to continue with this .p12 file with JDK 7 . Perhaps it must be a JDK 7 bug. Anyway still we can continue with a .jks file format of the above created certification file with comfortably in JDK7.

Below explains how to convert a .p12 to .jks

Create an empty JKS store
              keytool -genkey -alias alice -keystore alice.jks
Import alice.p12 into alice.jks
             keytool -v -importkeystore -srckeystore alice.p12 -srcstoretype PKCS12 -destkeystore    alice.jks -deststoretype JKS


Then you can use this .jks and send push notifications to ios .

boolean production = true;
  String password = "this is the password you provide when creating certificate file";
  String keyStroke ="alice.jks";
  AppleNotificationServer jksServer = null;
  try {
   jksServer = new AppleNotificationServerBasicImpl(keyStroke,
     password, ConnectionToAppleServer.KEYSTORE_TYPE_JKS,
     production);
  } catch (KeystoreException keystoreException) {
  
  }
  PushNotificationPayload payload = PushNotificationPayload.complex();
  try {
   payload.addAlert(pushMessage);
  } catch (JSONException e2) {
  
  }
 
  PushNotificationManager pushManager = new PushNotificationManager();
  try {
   pushManager.initializeConnection(jksServer);
  } catch (CommunicationException | KeystoreException e) {
  
  }
  try {
   List notifications = pushManager
     .sendNotifications(payload, "device id here to which the notifications will be sent");
  } catch (CommunicationException | KeystoreException e) {
  
  }


Tuesday, July 2, 2013

How to push notification to Android from java


 

For this we need the below jars basically.


gcm-server jar

json-simple jar


you can get the latest versions of the above.


And  lets get in to the business directly.


Sender sender = new Sender("senderId"); // follow this link to get your id http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
List devicesList = new ArrayList();

// deviceIds are the ids of the devices we send notifications. This is a 64 bit id. Note that this is not the imi nunber. Google search on how to find this.Device to device it varies.I guess.
devicesList.add("deviceId1");
devicesList.add("deviceId2");


com.google.android.gcm.server.Message message = new com.google.android.gcm.server.Message.Builder()
         .collapseKey("1").timeToLive(3).delayWhileIdle(true)
      .addData("message", "sending message")
      .build();


MulticastResult result = sender.send(message, devices, 1);



And that's it .Thank you.

 

 

 

Wednesday, April 24, 2013

How to define a property file in spring context and how to retrieve data from the property file

It is always a good practise to use property file to store configuration data kind of things.First we have to define it in the spring context  as below.lets take our property file as test.properties.
 
<bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath : test.properties"/>
</bean>
 
 As we are going to use java.util.Properties we have to define PropertiesFactoryBean as a bean in our context file as below.
 
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath : test.properties"/>
 </bean>
 
Also we will use @Autowired annotation when retrieving data.So lets define the below in our context file as well.

 
<context:annotation-config />  Now we have completed the spring context side changes.Lets see how to reach data.  public class Test{

@Autowired
private Properties properties;

private void test(){

String value=properties.getProperty("key");

}




Hope this helps .


 



 

 

 


Thursday, May 19, 2011

How to add a attribute of XML as a html element attribute using XSLT

Once you work with XSLT you may come across a situation where you need to directly apply a value of a XML attribute as the value of a HTML attribute.There are two options to figure the things out.

myXML.xml










Target html










In the above case the value of the attribute boxSize is directly applied as the value of the attribute size of input element in HTML format.This can be achieved as below two methods.

method 1











method 2




As you see method 2 is more easy and it is the simple way to get the job done.Thank you.Hope this helps.

Wednesday, May 11, 2011

How to choose a XML parser

There are many XML parsers out there.So here I am going to give some pros and cons of those.SAX,StAX,DOM,JAXB are the XML parsers that I took into consideration.

Parsing Mechanism

Features

JAXB

High memory usage

DOM

High memory usage

StAX

XML schema can not be validated.
Low memory usage

SAX

XML schema can be validated
Low memory usage

Hope this will give you some hints about what to choose.Thank you.

Wednesday, February 9, 2011

Contributing to a open source project with SVN

SVN

SVN is referred as subversion.Now I am going to tell you how you can use this SVN for your contributions to an open source project in simple ways.I assumed you use unix platform as your working environment.

Checking out a project.

For contributing an open source project you need to have the code base of the project which is available in the relevant organisation's repository and you can find out the url of that repository browsing through their official cite.

  1. Open an terminal
  2. cd Desktop
  3. type svn checkout url
ex: svn checkout https://docbook.svn.sf.net/svnroot/docbook/trunk for checking out the trunk of DocBook project


Then you will get the trunk of that source code in to your Desktop.

Creating a patch

Once you checked out the source you can do necessary editing of the source code and developing your own code for the project.Then you need to inform the community of that project regarding what changes you have made to the source code simply by using the following command in the terminal being in the necessary folder which is the folder where your code is in.

svn diff > a.patch

or

svn diff > a.diff

then the changes will be in your working folder as the file a.patch or a.diff.Then you can send this file to the community.

Come on visitors try SVN for your contribution.

Wednesday, December 29, 2010

Sax Parser...A simple way to access xml documents

Sax parser allow us to access a xml document and we can predefine how the program should behave once certain events are occurred in the xml document when the parser parse the xml document from top to bottom.

A simple java code that helps you to access a xml document through your implementation

package sax;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SaxHelperSanja {

public static void main(String args[]) {



try {

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {


public void startElement(String uri, String localName,
String qName, Attributes attributes)
throws SAXException {
/*this method is called once the parser meets a start element and you should implement here how your code should behave when a start element is found.The name of the start element is the parameter qName and the attributes of that element is under attributes parameter.*/
}

public void endElement(String uri, String localName,
String qName)
throws SAXException {
/*this method is called once the parser meets an end element and you should implement here how your code should behave when an end element is found.The name of the end element is the parameter qName.*/
}

public void characters(char ch[], int start, int length)
throws SAXException {
/* this method is called once the parser encounters text under an element.*/
}
};

// for specifying the xml document
saxParser.parse("/home/sanjaya/Desktop/a.xml", handler);

} catch (Exception e) {
e.printStackTrace();
}
}
}

there are startDocument() and endDocument() methods that are called when the parser begins the parsing of the xml document and ends the parsing of the document.In the above code I have not included them as I think they are not vital as the once I have mentioned above.Anyway feel free to use these two methods if you really need.

Hey that's the simple sax parsing.enjoy....