ios push notifications using nodejs

4 篇文章 0 订阅
2 篇文章 0 订阅

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值