def push_notification(users)
unless self.pushed_to_mobile? || users.empty?
# make sure all required users receive notifications on
# all registered devices.
@device_tokens = users.map { |u| u.device_tokens }.flatten.uniq
@device_tokens.each do |token|
notification = Grocer::Notification.new(
device_token: token.device_token,
alert: { "body" => "#{self.context_type}: #{self.name}", "action-loc-key" => "View" },
badge: 1,
sound: 'chord.aiff'
custom: { "content" => [ self.name, self.context_type, self.context_name, self.context_id, self.content_id ] },
)
feedback.each do |attempt|
# # TODO: if a device fails 3 times it should be removed from the token list.
# # Failing 3 times indicates that the user has turned notifications off or
# # removed the app from the phone. If the user logs in and their token does
# # not exist it will be readded.
puts "Device #{attempt.device_token} failed at #{attempt.timestamp}"
end
pusher.push( notification )
end
end
# update model to prevent notifying again.
self.pushed_to_mobile = true
self.save
end
def pusher
return @pusher if defined?( @pusher )
@pusher = Grocer.pusher(
certificate: Settings.apple_push_notification_options[:certificate],
passphrase: Settings.apple_push_notification_options[:pass],
gateway: Settings.apple_push_notification_options[:host],
retries: 3
)
end
def feedback
return @feedback if defined?( @feedback )
@feedback = Grocer.feedback(
certificate: Settings.apple_push_notification_options[:certificate],
passphrase: Settings.apple_push_notification_options[:pass],
gateway: Settings.apple_push_notification_options[:host],
retries: 3
)
end
如果我删除循环并强制代码发送到单个 token ,代码将运行并在设备上收到推送通知。但是,如果我尝试遍历 users.device_tokens 数组,代码会无误地执行,但设备上永远不会收到通知。
编辑:即使循环只运行一次。
最佳答案
问题与您访问 Grocer::Feedback 套接字的方式有关。尝试将您的 feedback 方法更改为以下内容。
def feedback
@feedback ||= Grocer.feedback(
certificate: Settings.apple_push_notification_options[:certificate],
passphrase: Settings.apple_push_notification_options[:pass],
gateway: Settings.apple_push_notification_options[:host],
retries: 3
)
@feedback.to_a
end
调用 to_a 将强制读取当前反馈套接字上的任何内容。
关于ios - Ruby on Rails,Grocer 无法向多个收件人发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26372805/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这