作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
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/
我是一名优秀的程序员,十分优秀!