百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

APNS

> 编程语言
开源

一个 Apple 推送通知服务 gem

538 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

一个 Apple 推送通知服务 gem

h1. APNS

a gem for the Apple Push Notification Service.

h2. Install

sudo gem install apns

h2. Setup:

Convert your certificate

In Keychain access export your certificate as a p12. Then run the following command to convert it to a .pem

openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts

After you have your .pem file. Set what host, port, certificate file location on the APNS class:

APNS.host = 'gateway.push.apple.com' 
# gateway.sandbox.push.apple.com is default

APNS.pem  = '/path/to/pem/file'
# this is the file you just created

APNS.port = 2195 
# this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.

h2. Example (Single notification):

Then to send a push notification you can either just send a string as the alert or give it a hash for the alert, badge and sound.

device_token = '123abc456def'

APNS.send_notification(device_token, 'Hello iPhone!' )

APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')

h2. Example (Multiple notifications):

You can also send multiple notifications using the same connection to Apple:

device_token = '123abc456def'

n1 = APNS::Notification.new(device_token, 'Hello iPhone!' )

n2 = APNS::Notification.new(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')

APNS.send_notifications([n1, n2])

h2. Send other info along with aps

You can send other application specific information as well.

APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default',
                                     :other => {:sent => 'with apns gem'})

This will add the other hash to the same level as the aps hash:

{"aps":{"alert":"Hello iPhone!","badge":1,"sound":"default"},"sent":"with apns gem"}

h2. Getting your iPhone's device token

After you setup push notification for your application with Apple. You need to ask Apple for you application specific device token.

h3. ApplicationAppDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{    
    // Register with apple that this app will use push notification
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | 
      UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];

    // Your app startup logic...
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{
    // Convert the binary data token into an NSString (see below for the implementation of this function)
    NSString *deviceTokenAsString = stringFromDeviceTokenData(deviceToken);

    // Show the device token obtained from apple to the log
    NSLog(@"deviceToken: %@", deviceTokenAsString);
}

h3. stringFromDeviceTokenData function

This snippet comes from "this stackoverflow post's anwser":http://stackoverflow.com/a/1990880/855846.

NSString* stringFromDeviceTokenData(NSData *deviceToken)
{
  const char *data = [deviceToken bytes];
  NSMutableString* token = [NSMutableString string];
  
  for (int i = 0; i < [deviceToken length]; i++) {
    [token appendFormat:@"%02.2hhX", data[i]];
  }
  
  return [[token copy] autorelease];
}

For more information on Apple Push Notifications you can see Apple Developer Documentation "here":http://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW2.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言