Apple PNS(推送通知服务)示例代码

fro*_*h42 51 iphone push-notification

是否有一个示例项目展示了如何在iPhone上使用APNS以及如何设置内容?我目前正在查看文档,但是有一些工作代码可以分开并看看它们如何一起工作会很好吗?

我似乎无法使用谷歌或在iPhone开发中心找到任何东西.

jes*_*rry 59

设置推送通知服务最糟糕的部分是配置.我遇到的主要绊脚石是你从Apple网站下载的.cer文件中有一个证书和一个密钥,我用C#写了一个发送通知的系统服务,并且因为我导出了证书而导致连接失败而不是关键.

我不记得是谁最初写的这个,这里有一些python中的代码,当我第一次测试通知服务时帮助了我.我喜欢它,因为它非常简单并且在测试期间运行良好.

import socket, ssl, json, struct

# device token returned when the iPhone application
# registers to receive alerts

deviceToken = 'XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX' 

thePayLoad = {
     'aps': {
          'alert':'Oh no! Server\'s Down!',
          'sound':'k1DiveAlarm.caf',
          'badge':42,
          },
     'test_data': { 'foo': 'bar' },
     }

# Certificate issued by apple and converted to .pem format with openSSL
# Per Apple's Push Notification Guide (end of chapter 3), first export the cert in p12 format
# openssl pkcs12 -in cert.p12 -out cert.pem -nodes 
#   when prompted "Enter Import Password:" hit return
#
theCertfile = 'cert.pem'
# 
theHost = ( 'gateway.sandbox.push.apple.com', 2195 )

# 
data = json.dumps( thePayLoad )

# Clear out spaces in the device token and convert to hex
deviceToken = deviceToken.replace(' ','')
byteToken = bytes.fromhex( deviceToken ) # Python 3
# byteToken = deviceToken.decode('hex') # Python 2

theFormat = '!BH32sH%ds' % len(data)
theNotification = struct.pack( theFormat, 0, 32, byteToken, len(data), data )

# Create our connection using the certfile saved locally
ssl_sock = ssl.wrap_socket( socket.socket( socket.AF_INET, socket.SOCK_STREAM ), certfile = theCertfile )
ssl_sock.connect( theHost )

# Write out our data
ssl_sock.write( theNotification )

# Close the connection -- apple would prefer that we keep
# a connection open and push data as needed.
ssl_sock.close()
Run Code Online (Sandbox Code Playgroud)

还有一个名为apn_on_rails的rails gem,如果你正在开发一个rails应用程序,它似乎运行得很好,我今天刚看到它并且能够从控制台发送通知.

在iPhone方面,您只需要调用以下内容即可注册所有类型的通知:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
Run Code Online (Sandbox Code Playgroud)

要接收设备令牌,您需要实现以下委托方法:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud)

在测试期间,你可以使用NSLog将deviceToken踢到控制台,然后将其粘贴到上面的python脚本中,在生产中你显然需要设置一些方法来将令牌送到你的服务器.

此外,在制作中,您需要查询Apple的反馈服务,并从删除了您的应用的用户中删除设备令牌.


Sco*_* K. 15

一个好的起点是Urban Airship.您可以设置一个免费的基本帐户,该帐户将执行向Apple服务器发送推送通知的所有服务器端工作.他们还可以帮助您完成使应用程序使用其服务所需的所有步骤,并提供优秀的示例代码,说明如何注册应用程序以进行通知.

除了成为他们服务的快乐用户之外,我与他们没有任何其他联系.


Sim*_*ker 14

如果它有帮助,我编写了一个Python库PyAPNs,用于与服务器端的推送通知服务进行交互:

http://github.com/simonwhitaker/PyAPNs


Mla*_*den 12

http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-notification-provider-server-tutorial/

这个帮助我很多在使用PHP在linux服务器上提供提供者端代码.


Ben*_*tow 5

在iPhone方面写的代码真的不多.您需要获取iPhone或iPod Touch的唯一令牌,然后将该令牌转发到您的服务器.获取令牌需要调用UIApplication,但没有预定义的方法将其传递给您的服务器.我的一个应用程序对PHP脚本执行HTTP POST,将令牌放入数据库.

如果您对配置和设置证书等感到好奇,可以查看Apple推送通知服务编程指南.