ios push notifications using nodejs
2014-05-09 11:02
232 查看
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 < >
fb0123280e65563ae0c6e0bce68405aa40362a5e46984a6a1aa268bae531cd59
this 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
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 < >
fb0123280e65563ae0c6e0bce68405aa40362a5e46984a6a1aa268bae531cd59
this 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
相关文章推荐
- Send Push Notifications to iOS Devices using Xcode 8 and Swift 3, APNs Auth Key
- IOS PUSH NOTIFICATIONS USING JABBER AND EJABBERD
- Open Source C# (Mono Compatible) Library for Sending Push Notifications To iOS (iPhone/iPad APNS), Android (C2DM), Windows Phone Apps
- iOS Push Notifications | Parse
- iOS + Nodejs SSL/Https双向认证
- 为一个 iOS 应用编写一个简单的 Node.js/MongoDB Web 服务
- Node.js : Building RESTful APIs using Loopback and MySQL
- 为一个 iOS 应用编写一个简单的 Node.js/MongoDB Web 服务
- Build a JavaScript Compressor tool using NodeJS, ExpressJS, Jade, UglifyJS tutorial Read more: http
- [Node.js] Take Screenshots of Multiple Dimensions for Responsive Sites using Nightmare
- 为一个 iOS 应用编写一个简单的 Node.js/MongoDB Web 服务
- Node.js 基于 ursa 模块的 RSA 加密解密(已与IOS,Android实现加密通信)
- [Whole Web] [Node.js] Using npm run to launch local scripts
- 编写一个使用 Node.js/MongoDB Web 服务的 iOS 应用
- Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL
- Sending iOS Push Notifications via APNS in JavaScript
- 使用Node.js和Redis实现push服务
- 用Node.js做了一个iOS icon自动化生成工具
- (转)pem, cer, p12 and the pains of iOS Push Notifications encryption
- iOS + Nodejs SSL/Https双向认证