1, create the proper SSL Certificate and Keys. please refer to the link from Apple Provisioning and Development
after you get the p12 file from mac, you can get certificate and keys by the following steps
$ openssl pkcs12 -in CertificateName.p12 -out CertificateName.pem -nodes
and then test connection to apn test server
$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert CertificateName.pem
2, for the ios app, refer to Apple Provisioning and Development
In the Xcode Organizer window, go the Provisioning Profiles section and install the profile on your device.
and add the following code to your application code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UIApplicationsharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
return YES;
}
- (void)application:(UIApplication*)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString *tokenStr = [deviceToken description];
NSLog(@"tokenStr:");
NSLog(tokenStr);
}
run your app, your would see a token like
<fb012328 0e65563a e0c6e0bc e68405f4 40362a5e 46984a6a 1cc268ba e531cd59>
and the token would be remove space and < >
fb0123280e65563ae0c6e0bce68405aa40362a5e46984a6a1aa268bae531cd59this the token for you ios device. this would be used on server to send the client.
3, on server side
install node-apn
and create a file named push.js
var apn = require('apn');
var options = { };
var apnConnection = new apn.Connection(options);
var token='fb0123280e65563ae0c6e0bce68405aa40362a5e46984a6a1aa268bae531cd59';
var myDevice = new apn.Device(token);
var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600;// Expires 1 hour from now.
note.badge = 3;
note.alert ="You have a new message";
note.payload = {'messageFrom':'Wang'};
apnConnection.pushNotification(note, myDevice);
var options = {
"batchFeedback": true,
"interval": 300
};
var feedback = new apn.Feedback(options);
feedback.on("feedback", function(devices){
devices.forEach(function(item) {
// Do something with item.device and item.time;
console.log("hello, i got it");
});
console.log("what is it");
});
at the same folder where push.js is under, copy the file CertificateName.pem you created at step 1 to the folder, named it as cert.pem, and copy a same file named key.pem
then run the push.js using the following command
$ node push.js
then you would see the new message pushed to your ios device