Apple PNS(推送通知服务)示例代码 [英] Apple PNS (push notification services) sample code

查看:92
本文介绍了Apple PNS(推送通知服务)示例代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a sample project showing how to use APNS on the IPhone and how to set up things? I'm currently looking at the documentation but it would be nice to have some working code to pick apart and see how it all works together?

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

I can't seem to find anything using google or in the iphone dev center.

推荐答案

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

The worst part about setting up the push notification service is the provisioning. The major stumbling block that I came across was that there is a certificate and a key in the .cer file you download from Apple's site, I wrote a system service in C# that sent out notifications and the connections kept failing because I had exported the certificate and not the key.

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

I don't recall who originally wrote this, here is a little bit of code in python that helped me out when I was first testing the notification service. I like it because it is very simple and works well during testing.

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()

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

There's also a rails gem called apn_on_rails that seems to work pretty well if you're developing a rails application, I just saw it today and was able to send out notifications from the console.

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

On the iPhone side you'll just need to call the following to register for all types of notifications:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];

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

To receive the device token you'll need to implement the following delegate methods:

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

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

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

During testing you can just kick the deviceToken to the console with NSLog, then paste it into the python script above, in production you'll obviously need to set up some method to get the token to your servers.

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

Also, in production you'll need to query Apple's feedback service and remove device tokens from users who removed your app.

这篇关于Apple PNS(推送通知服务)示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆