这或多或少是我的控制器的样子......
def create
if authenticate_user
post = Post.find_by_id(params[:post_id])
if post
comment = post.comments.new(comment_params.merge(user_id: params[:user_id]))
Comment.transaction do
if (comment.save)
apns_file = File.join(Rails.root, "APNSCert.pem")
app = Rpush::Apns::App.new
app.name = "My App"
app.certificate = File.read(apns_file)
app.environment = "sandbox" # APNs environment.
app.password = ENV["apns_certificate_password"] #figaro
app.connections = 1
app.save!
#SEND Push notification to the user who made the original post
post_user = User.find(post.user_id)
comment_user = User.find(comment.user_id)
if post_user && comment_user
rememeber_tokens_for_user = RememberToken.where("user_id = ?", post_user.id)
if rememeber_tokens_for_user.size > 0
rememeber_tokens_for_user.each do |remember_token|
# Create a notification that alerts a message to the user, plays a sound, and sets the badge on the app
alert = comment_user.name + ": " + comment.comment_text
if alert.length > 50
alert = alert[0..46] + "..."
end
n = Rpush::Apns::Notification.new
n.app = Rpush::Apns::App.find_by_name("My App")
n.device_token = remember_token.device_token
n.alert = alert
n.data = { foo: "bar" }
n.save!
看来myoku上的服务器在app.save!
行ActiveRecord::RecordInvalid (Validation failed: Name has already been taken):
上收到错误。
我只不过是铁轨的中间人,所以任何帮助都会受到赞赏。我想把代码的'app'变量部分放到一个单独的类中,这个类只调用一次或类似东西,类似于objective-c中的单例?它显然不喜欢当它被一个不同的用户POST到这个资源时访问,这是我收到这个错误的时候。
我应该去刺激Grocer,因为我似乎无法让休斯顿或RPush工作?
答案 0 :(得分:1)
您绝对不需要为每个应用多次执行APN设置。这正是你遇到问题的原因。
答案 1 :(得分:1)
只有在没有创建应用时才插入支票来创建应用:
if !Rpush::Gcm::App.find_by_name("My App")
app.name = "My App"
app.certificate = File.read(apns_file)
app.environment = "sandbox" # APNs environment.
app.password = ENV["apns_certificate_password"] #figaro
app.connections = 1
app.save!
end