apn_sender 和 Rails 3 [英] apn_sender and Rails 3

查看:29
本文介绍了apn_sender 和 Rails 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目需要从服务器向设备发送通知.我不知道如何以及从哪里开始(首先创建表或其他),因为我是 Ruby on Rails 的新手.我已经按照 apn_sender 教程进行操作,但它不起作用,并且在开始时总是出错.是否有为 Rails 3 创建 apn_sender 的完整教程?

I have a project that needs to send notifications from server to device. I don't know how and where to start (create table first or other), because I'm new to Ruby on Rails. I've follow the apn_sender tutorial but it won't work and always errors out on start. Is there any full tutorial to create apn_sender for Rails 3?

谢谢

推荐答案

Rails 3 和 apn_sender

我也一直在研究 apn_sender 问题,因为我喜欢 Apple 推送通知支持似乎非常强大.但是,该项目已被放弃,现在落后于最新的 3 系列导轨.我已经让系统工作如下.

Rails 3 and apn_sender

I too have been working on the apn_sender problem, because I like that the Apple Push Notification support seems pretty robust. However, the project has been abandoned and now it is falling behind the latest 3 series rails. I have gotten the system working as follows.

gem 'apn_sender', :require => 'apn'
gem 'daemons'
gem 'resque', '1.20.0'
gem 'resque-access_worker_from_job'

Resque 中的信号处理在 1.20 之后发生了变化,因此您必须使用较旧的 1.20.0.守护进程应该一直是一个要求,但从未明确出现在 gem 规范中.

The signal handling changed in Resque after 1.20 so you must use the older 1.20.0. Daemons should have been a requirement all along but was never explicitly in the gem spec.

#!/usr/bin/env ruby

# Daemons sets pwd to /, so we have to explicitly set RAILS_ROOT
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

require 'rubygems'
require 'apn'
require 'apn/sender_daemon'

APN::SenderDaemon.new(ARGV).daemonize

此处检索此内容. 未生成 apn_sender 脚本的原因是生成器脚本需要更新 3 系列导轨.

Retrieved this from here. The reason the apn_sender script is not generated is because the generator script needs to be updated for 3 series rails.

./script/apn_sender --environment=production --verbose start

当启动环境时,生产值可能就是您想要的,因为 ad_hoc 和发布版本都使用生产证书.

When starting the environment the production value is probably what you want since both ad_hoc and release versions are using the production certificate.

github 上的这个 fork 已经修正了一些 rails 3 的问题,但是resque版本问题仍需明确解决.

This fork on github has corrected some of the rails 3 issues, but the resque version problem still needs to be resolved explicitly.

本网站 展示了如何绕过守护进程发送消息.他将令牌存储为 base64 编码的字符串,既美观又紧凑.apn_sender 框架中设备字符串的格式是一个十六进制字符串,格式如下:deadbeef 0a1b2cdf deadbeef 0a1b2cdf deadbeef 0a1b2cdf deadbeef 0a1b2cdf.空格可选.

This website shows how to send messages bypassing the daemon. He stores the tokens as base64 encoded strings which is nice and compact. The format of a device string in the apn_sender framework is a hex string formatted like this: deadbeef 0a1b2cdf deadbeef 0a1b2cdf deadbeef 0a1b2cdf deadbeef 0a1b2cdf. Spaces optional.

require 'base64'
ios_device_token = User.where(username: 'Cameron').first.ios_device_token
token = Base64.decode64(ios_device_token).unpack('H*').first.scan(/\w{8}/).join(' ')
notification = APN::Notification.new(token, { alert: 'Hello, World!', sound: true, badge: 99 })
sender = APN::Sender.new(verbose: true, environment: 'production')
sender.send_to_apple(notification)

确保feedback.rb 文件是最新的

# Unpacking code borrowed from http://github.com/jpoz/APNS/blob/master/lib/apns/core.rb
while bunch = socket.read(38)   # Read data from the socket
  f = bunch.strip.unpack('N1n1H140')
  feedback << APN::FeedbackItem.new(Time.at(f[0]), f[2])
end

该代码的一个版本缺少 38,导致反馈服务返回错误的令牌字符串

One version of the code lacked 38 and it resulted in bad token strings being returned from the feedback service

如果您打算将 Ruby 开发提升到一个新的水平,您还应该考虑使用 RubyMine 之类的 IDE因为它让一切变得如此简单.

If you plan on taking Ruby development to the next level you should also consider an IDE like RubyMine because it makes everything so much easier.

这篇关于apn_sender 和 Rails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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