2015-03-31 78 views
1

我似乎遇到了RPushHouston的问题。如何使用Rails gems RPush或Houston发送推送通知?

这里有或多或少的什么我的控制器看起来像......

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! 

看来我在Heroku服务器上app.save!线,ActiveRecord::RecordInvalid (Validation failed: Name has already been taken):得到一个错误。

我只是一个中间的铁轨,所以任何帮助,将不胜感激。我是否应该将'app'变量的代码段放到一个单独的类中,该类只是调用一次或某种东西,类似于objective-c中的单例。它显然不喜欢什么时候由不同的用户访问这个资源时,这是我得到这个错误。

我应该在Grocer刺戳一下,因为我似乎无法得到休斯敦或RPush的工作?

+1

我不熟悉的'RPush'宝石,但似乎你不能复制的了'name'应用程序。是否可能只需要创建一个'Rpush :: Apns :: App',即当'Rpush :: Apns :: App.find_by_name(“My App”)''返回'nil'? – AbM 2015-03-31 16:08:12

回答

0

您绝对不需要为每个应用程序进行一次以上的APN设置。这正是你遇到问题的原因。

-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