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

标签 iphone push-notification

是否有示例项目展示如何在 iPhone 上使用 APNS 以及如何进行设置?我目前正在查看文档,但如果能有一些工作代码可以分开并看看它们是如何协同工作的,那就太好了?

我似乎无法使用 Google 或 iPhone 开发中心找到任何内容。

最佳答案

设置推送通知服务最糟糕的部分是配置。我遇到的主要障碍是,您从 Apple 网站下载的 .cer 文件中有一个证书和一个 key ,我用 C# 编写了一个系统服务,该服务发送通知,但由于我导出了证书,连接一直失败而不是 key 。

我不记得最初是谁写的,这里有一些 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()

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

在 iPhone 端,您只需调用以下命令即可注册所有类型的通知:

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

要接收设备 token ,您需要实现以下委托(delegate)方法:

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

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

在测试期间,您可以使用 NSLog 将 deviceToken 踢到控制台,然后将其粘贴到上面的 python 脚本中,在生产中,您显然需要设置某种方法来将 token 获取到您的服务器。

此外,在生产中,您需要查询 Apple 的反馈服务,并从删除您的应用的用户中删除设备 token 。

关于iphone - Apple PNS(推送通知服务)示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1052645/

相关文章:

iphone - 在 UIToolBar 中垂直移动 UIBarButtonItems

ios - 使用 SFSpeechRecognizer 后 AVSpeechSynthesizer 不说话

android - 未收到 MixPanel 推送通知

java - 从 Java Web 应用程序发送推送通知

android - 明确的 com.google.android.c2dm.intent.REGISTER Intent

iphone - UITableView 数据源必须返回一个单元格

iphone - 如何返回从 objective-c (iPhone) 中的 Web 服务获取的数据?

iphone - AVFoundation - 从 Y 平面获取灰度图像 (​​kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)

android - meteor raix :push notification channelId for android 8. 0.0

iphone - 向 APNS 批量发送消息,如果其中一些出现错误怎么办?