草庐IT

ios - 推送通知在杂货店不起作用

coder 2024-01-30 原文

我将通过 Rails 后端向 iOS 设备发送通知。
我已将 grocer gem 添加到 Gemfile,然后将其安装到项目中。

gem 'grocer'

我计划在后台模式下发送通知。所以我创建了 resque 作业,并像这样在 app/jobs/notificationsender.rb 中添加了 grocer 逻辑。

def self.perform(language)
    @lang = language

    # Thread.new do
      while true

        begin
            pusher = Grocer.pusher(certificate:  "#{Rails.root}/lib/notification/cer.pem", # required
                       passphrase:  "llvc",                         # optional
                       gateway:     "gateway.sandbox.push.apple.com", # optional
                       port:        2195,                             #optional
                       retries:     3)

        feedback = Grocer.feedback( certificate:  "#{Rails.root}/lib/notification/cer.pem", # required
                                  passphrase:  "llvc",                        
                                  gateway:     "feedback.sandbox.push.apple.com", 
                                  port:        2196,                     
                                  retries:     3)

        note = Grocer::Notification.new(
          device_token: device_token,
          alert: message,
          sound: 'default',
          badge: 0,
          expiry: Time.now + 60*60,
          content_available: true,
          custom: {
            "linkname": linkname,
            "linkurl": linkurl
          }
        )

        feedback.each do |attempt|
          puts "Device #{attempt.device_token} failed at #{attempt.timestamp}"
        end

        pusher.push(note)
        rescue Exception => e
            puts e.class
            puts e
        end

        sleep(5)
      end #while
  end

我从 iphone 获得了一个设备 token 并将其发送到后端。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    _deviceToken = deviceToken;

    // Store the deviceToken in the current installation and save it to Parse.
    if (currentInstallation == nil) {
        currentInstallation = [[BSGInstallation alloc] init];
    }

    currentInstallation.delegate = self;
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];
}

我下载了一个 apple pushnotification 开发证书并从中生成了 p12,然后从命令行创建了 cer.pem 文件。
我用应用程序开发 cer 签署了 ios 项目,用于调试和分发,用于发布和分配的开发配置。当然,我在配置中正确添加了设备标识符。
在构建 ios、后端、resque 作业后,我在 iPhone 中每 5 秒收到一次通知。我在 iPad 上安装了该应用程序。但通知不会出现在 iPad 上。当然iphone和ipad的id都注册正确了。并且我确认了 iphone 和 ipad 的设备 token 已通过 Grocer::notification.new()
但该通知在 iPad 中不起作用。所以我重置了服务器并重新安装在每台设备上。起初我测试了 iPad。结果是一样的。然后我转向 iPhone。该系统也无法在 iphone 上运行。在它起作用之前。很奇怪。
所以我想知道以下内容。
1)iphone或ipad收到通知的原因。我想知道尽可能多的分支,以便我可以按照步骤快速检测原因。 2) 我可以在 APNS 之前检查通知到达吗?
感谢您阅读我的详细说明。

最佳答案

IOS和rails代码都没有错误。
我已经在 ios 中进行了代码签名,并通过 url 创建了一个 pem 文件。
https://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
遵循 github 教程时出现错误。 谢谢。

关于ios - 推送通知在杂货店不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35725681/

有关ios - 推送通知在杂货店不起作用的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. ruby-on-rails - 如何在发布新的 Ruby 或 Rails 版本时收到通知? - 2

    有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  8. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  9. arrays - Ruby 数组 += vs 推送 - 2

    我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“

  10. ruby - 字符串文字前面的 * 在 ruby​​ 中有什么作用? - 2

    这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw

随机推荐