Last active
August 29, 2015 14:04
-
-
Save omniproc/73e4d9fb91b2fcb4b790 to your computer and use it in GitHub Desktop.
iOS push notification test-service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here comes our service implementation | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.notnoop.apns.*; | |
// This is a basic implementation of notnoop/apns for enhanced push notifications | |
// It's purpose is to let you know if your push certificates are working correctly | |
// We're using enhanced push notifications here - this means you'll get a detailed explanation | |
// you your push failed if it fails. | |
// Get notnoop/apns from here https://github.com/notnoop/java-apns for the push service to work | |
// This class was tested using notnoop/apns v. 1.0.0 Beta 3, but it should work with higher versions also | |
public class Push | |
{ | |
static Logger LOGGER = LoggerFactory.getLogger(Push.class); | |
// enter the path to your certificate which you place within your project root here, e.g.: "mycertificate.p12" | |
private static String certpath = "certificate.p12"; | |
// enter the password you used to protect your certificate here | |
private static String certpass = "PASSW0RD"; | |
// enter the token of the iOS device you want to sent the push message too | |
// remember: since we're using withSandboxDesitination, this has to be a dev. certificate and your device has to be registred within Apple's member center | |
// using this deivce uuid | |
public static String token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; | |
public static void main(String[] args) | |
{ | |
List<String> apnReceivers = new ArrayList<String>(); | |
apnReceivers.add(token); | |
pushMessage(apnReceivers, certpath); | |
} | |
public static void pushMessage(List<String> receivers, String certificatePath) | |
{ | |
// create a new delegate | |
ApnsDelegate delegate = new CustomApnsDelegate(); | |
// build a new apns service and submit the created delagate to it | |
ApnsService service = APNS.newService().withCert(certificatePath, certpass).withSandboxDestination().withDelegate(delegate).build(); | |
// generate you notification payload | |
String message = "My push notification!"; | |
// compose your push notification | |
String payload = APNS.newPayload().alertBody(message).badge(1).noActionButton().build(); | |
// push the notification | |
try | |
{ | |
System.out.println("Pushing notification."); | |
service.push(receivers, payload); | |
} | |
catch(Exception e) | |
{ | |
//TODO error handling | |
System.out.println("Push failed."); | |
} | |
} | |
} | |
// Here comes our delegate implementation | |
import com.notnoop.apns.ApnsDelegate; | |
import com.notnoop.apns.ApnsNotification; | |
import com.notnoop.apns.DeliveryError; | |
public class CustomApnsDelegate implements ApnsDelegate | |
{ | |
public CustomApnsDelegate(){} | |
@Override | |
public void messageSent(ApnsNotification message, boolean resent) | |
{ | |
System.out.println("MessageSent: " + message); | |
} | |
@Override | |
public void messageSendFailed(ApnsNotification message, Throwable e) | |
{ | |
System.err.println("MessageSendFailed: " + e.getMessage()); | |
} | |
@Override | |
public void connectionClosed(DeliveryError e, int messageIdentifier) | |
{ | |
System.out.println("ConnectionClosed with code: " + e.toString()); | |
} | |
@Override | |
public void cacheLengthExceeded(int newCacheLength) | |
{ | |
System.out.println("CacheLengthExceeded. New cache length: " + newCacheLength); | |
} | |
@Override | |
public void notificationsResent(int resendCount) | |
{ | |
System.out.println("NotificationsResent count: " + resendCount); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment