Bibliothèque gerant le service le service Apple Push Notification (APNs). Ce service, introduit par Apple permet d'envoyer des messages à des I-Phones (identifiés par des tokens générés par Apple).Le processus:
1- Connexion entre Provider(Utilisateur voulant envoyer des messages à des I-Phones) et Interface binaire d'apple (gateway.push.apple.com:2195) etablie de manière securisée (TLS ou SSL)
2- Communication suivant le modèle One Way Message (Du Provider à Application sur le I-Phone au travers de l'interface binaire).
des Infos importantes sur le sujet sont dispo sur :
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW9
cette petite bibliotheque, dans sa première gère les points d'en haut et les suivants:
3- Creation du Message (Format simple et evolué) à Envoyer selon le format donné par Apple.
4- Envoi de Message (simple) au destinataire.
une version ultèrieure permettra de gerer la gestion d'erreur renvoyés par l'interface Binaire du à par exemple à
un faux token, delai de delivrance expiré, etc.....
un Mock est livré ci-dessous.
Source / Exemple :
package she.apple.apns.test;
import java.io.UnsupportedEncodingException;
import net.sf.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import she.apple.apns.notification.APNEnhancedFormat;
import she.apple.apns.notification.APNException;
import she.apple.apns.notification.APNSimpleFormat;
import she.apple.apns.notification.payload.APNPayloadAlert;
import she.apple.apns.notification.payload.APNPayloadAlertSimple;
import she.apple.apns.notification.payload.APNPayloadBadge;
import she.apple.apns.notification.payload.APNPayloadExeption;
import she.apple.apns.notification.payload.APNPayloadMessage;
import she.apple.apns.notification.payload.APNPayloadPropertyEnum;
import she.apple.apns.notification.payload.APNPayloadSound;
/**
- Mocking class
-
- @author Arthur
-
- /
public class ApnsPayloadMock {
/**
- Mocking the enumerator of the paylaod's properties
- /
@Test
public void _enumPayloadPropertyMock() {
APNPayloadPropertyEnum appsPropertyEnum = APNPayloadPropertyEnum.ALERT;
// mocking the property enum (alert, badge, sound)
Assert.assertArrayEquals("alert".toCharArray(), appsPropertyEnum
.getDescription().toCharArray());
}
/**
- mocking the constructor, mocking the setter and getter and clearer
- /
@Test
public void _mockPayloadAlertMessageStructure() {
// 1- Alert simple class
// 1-a alert simple constructor
APNPayloadAlertSimple alertSimple2 = new APNPayloadAlertSimple(
"sie haben eine Nachricht");
// 1-a alert simple formatting message
Assert.assertArrayEquals(
"\"alert\":\"sie haben eine Nachricht\"".toCharArray(),
alertSimple2.formatToString().toCharArray());
APNPayloadAlert enhancedAlert = new APNPayloadAlert(
"You have a (1) new email", "Read", "READ_FORMAT",
new String[] { "Firstname", "Lastname" }, "Image011.jpg");
this.showIt(enhancedAlert.formatToString());
// 2 - enhanced alert'message
APNPayloadAlert alert = new APNPayloadAlert();
alert.setBody("sie haben eine Nachricht");
Assert.assertArrayEquals("\"alert\":\"sie haben eine Nachricht\""
.toCharArray(), alert.formatToString().toCharArray());
// asserting an alert message with more than one properties
// getter OK ?
alert.setActionLocKey("PLAY");
alert.setLocKey("GAME_PLAY_REQUEST_FORMAT");
alert.setLocArgs(new String[] { "Jenna", "Frank" });
Assert.assertTrue("PLAY".equals(alert.getActionLocKey()));
Assert.assertTrue("GAME_PLAY_REQUEST_FORMAT".equals(alert.getLocKey()));
Assert.assertTrue("Frank".equals(alert.getLocArgs()[1]));
this.showIt(alert.formatToString());
Assert.assertNotNull(alert.formatToString());
// remove alert body and asserting again
alert.clearBody();
Assert.assertNull(alert.formatToString()); // no body , no message !!?
alert.setBody("sie haben eine Nachricht");
// exporting the underlining type of the alert message
JSONObject exportObject = alert.getAlertDictionary();
Assert.assertTrue(exportObject.getString("action-loc-key")
.equalsIgnoreCase("PLAY"));
//
alert.setBody("Body Changed");
this.showIt(alert.formatToString());
alert.setBody("Body moved");
alert.clearActionLocKey();
alert.clearLocArgs();
alert.clearLocKey();
alert.clearLaunchImage();
alert.getLaunchImage();
this.showIt(alert.formatToString());
new APNSimpleFormat();
}
/**
- mocking the constructor, mocking the setter and getter and clearer
- /
@Test
public void _mockPayloadBadgeStructure() {
// asserting message construction in the constructor
APNPayloadBadge badge = new APNPayloadBadge(9);
Assert.assertTrue("\"badge\":9".equals(badge.formatToString()));
// getting, setting and clearing
Assert.assertTrue(9 == badge.getBadgeValue());
badge.clearBadgeValue();
Assert.assertNull(badge.formatToString());
}
@Test
public void _mockPayloadMessageStructure() {
APNPayloadAlert alert = new APNPayloadAlert("Message received from Bob");
alert.setBody("Message received from Bob (new)");
alert.setLocKey("SET_KEY");
alert.setLocArgs(new String[] { "Marina", "Krin" });
APNPayloadSound sound = new APNPayloadSound("bing.aiff");
APNPayloadMessage payload = new APNPayloadMessage();
// setter
try {
payload.setAlert(alert);
payload.setSound(sound);
payload.setBadge(new APNPayloadBadge(9));
} catch (APNPayloadExeption e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
payload.setCustomValue("key boolean", true);
payload.setCustomValue("key number", 10);
payload.setCustomValue("key String Array", new String[] { "Patrick",
"Eddy" });
JSONObject object = new JSONObject();
object.put("part1", 34);
object.put("part2", null);
object.put("part4", false);
object.put("part3", object);
payload.clearCustomValue("keyDD boolean");
payload.setCustomValue("custom Value", object);
// payload.clearCustomValue("key number");
// payload.clearCustomValue("key String Array");
// clearer
// payload.clearAlert();
// payload.clearBadge();
// payload.clearSound();
try {
payload.toString().getBytes("UTF-8");
System.out.println(payload.toString().getBytes("UTF-8").length);
System.out.println(payload.toString().getBytes("UTF-8").length);
System.out.println("length ->" + payload.toJsonString().length());
System.out.println("length ->" + payload.toString().length());
//
// this.showIt(payload.toJsonString() + " size: "
// + payload.toJsonString().length());
payload.setAlert(new APNPayloadAlert("hi buddy"));
this.showIt(payload.toJsonString() + " size: "
+ payload.toJsonString().length());
this.showIt(payload.toJsonString().getBytes() + " size: "
+ payload.toJsonString().length());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (APNPayloadExeption e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
- mocking the constructor, mocking the setter and getter and clearer
- /
@Test
public void _mockPayloadSoundStructure() {
// asserting message construction in the constructor
APNPayloadSound sound = new APNPayloadSound("bingbong.aiff");
Assert.assertTrue("\"sound\":\"bingbong.aiff\"".equals(sound
.formatToString()));
// asserting a sample sound message (message without)
Assert.assertArrayEquals("\"sound\":\"sweety.aiff\"".toCharArray(),
new APNPayloadSound("sweety.aiff").formatToString()
.toCharArray());
// getting, setting and clearing
Assert.assertTrue("bingbong.aiff".equals(sound.getSoundFileName()));
sound.clearSoundFileName();
Assert.assertNull(sound.formatToString());
sound.setSoundFileName("bingbong.aiff");
Assert.assertTrue("bingbong.aiff".equals(sound.getSoundFileName()));
}
@Test
public void mockingEnhancedNotificationFormat()
throws UnsupportedEncodingException, APNException {
APNEnhancedFormat enhancedFormat = new APNEnhancedFormat();
String json = null;
try {
json = (new APNPayloadMessage(new APNPayloadAlert("body"),
new APNPayloadSound("bing.aifff"), new APNPayloadBadge(10)))
.toJsonString();
this.showIt(json + " " + json.getBytes().length);
enhancedFormat.setPayload(json);
enhancedFormat.setDeviceToken("1345");
enhancedFormat.setIdentifer(65);
enhancedFormat.setExpiry(70);
} catch (APNException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
json = new APNPayloadMessage(new APNPayloadAlert("body new", "PLAY",
"KEY", new String[] { "MA", "BA" }, "heee"),
new APNPayloadSound("SoundFile"), new APNPayloadBadge(39))
.toJsonString();
this.showIt("\ncommand is: " + enhancedFormat.getCommand());
enhancedFormat.setPayload(json);
this.showIt("simple format: " + enhancedFormat.getTextContent());
this.showIt("payload format:->" + enhancedFormat.getPayload());
// Payload and device token
System.out.println("textcontent:" + enhancedFormat.getTextContent());
for (byte b : enhancedFormat.getBinaryContent())
System.out.print(b);
}
@Test
public void mockingSimpleNotificationFormat()
throws UnsupportedEncodingException, APNException {
APNSimpleFormat simpleFormat = new APNSimpleFormat();
String json = null;
try {
json = (new APNPayloadMessage(new APNPayloadAlert("body"),
new APNPayloadSound("bing.aifff"), new APNPayloadBadge(10)))
.toJsonString();
this.showIt(json + " " + json.getBytes().length);
simpleFormat.setPayload(json);
simpleFormat.setDeviceToken("1345");
this.showIt("device Token " + simpleFormat.getDeviceToken());
} catch (APNException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
json = new APNPayloadMessage(new APNPayloadAlert("body new", "PLAY",
"KEY", new String[] { "MA", "BA" }, "heee"),
new APNPayloadSound("SoundFile"), new APNPayloadBadge(39))
.toJsonString();
simpleFormat.setPayload(json);
this.showIt("simple format: " + simpleFormat.getTextContent());
this.showIt("payload format:->" + simpleFormat.getPayload());
// Payload and device token
System.out.println("textcontent: \n" + simpleFormat.getTextContent());
for (byte b : simpleFormat.getBinaryContent())
System.out.print(b);
this.showIt("\ncommand is: " + simpleFormat.getCommand());
}
private void showIt(String value) {
System.out.println(value);
}
}
Conclusion :
merci pour vos critiques!!
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.