简体   繁体   English

Apple Push Notifications Rails应用程序消息传递

[英]Apple Push Notifications Rails App Messaging

I want to send an apple push notification every time a user gets a new message. 我想在用户每次收到新消息时发送一个苹果推送通知。 I found this tutorial http://www.waratuman.com/2011/01/13/apple-push-with-heroku/ 我发现本教程http://www.waratuman.com/2011/01/13/apple-push-with-heroku/

and with this code 并用这段代码

 class Jobs::APN::DeliverNotifications
   @queue = "apn"

   def self.perform
      APN::Notification.send_notifications
   end

 end

 class Jobs::APN::Feedback < Job
   @queue = "#{RAILS_ENV}::apn"

   def self.perform
     APN::Feedback.process_devices
   end

 end

 class Api::ApnController < ApplicationController
   skip_before_filter :verify_authenticity_token

   def create
     APN::Device.create(:token => params[:token])
     render :text => "", :status => 200
   end

   def subscribe
     device = params['token'] ? APN::Device.find_or_create_by_token(:token =>  params['token']) : nil
     event = Event.first(:conditions => {:id => params['event_id']})
     subscription = Subscription.new :device => device, :event => event

     status = 200
     if device && event && subscription.valid?
       subscription.save
     else
       status = 422
     end
     render :text => "", :status => status
   end

   def unsubscribe
     device = APN::Device.find_or_create_by_token(:token => params['token'])
     event = Event.first(:conditions => {:id => params['event_id']})
     subscription = Subscription.first(:conditions => {:device_id => device.id, :event_id => event.id})
     subscription.delete if subscription
     render :text => "", :status => 200
   end

 end

I know this code is supposed to set up the notifications and register the device but when I actually want to send a notification for a new message what should I do? 我知道此代码应该用于设置通知并注册设备,但是当我实际上要发送新消息的通知时,我该怎么办?

I currently have a message index 我目前有一个消息索引

   def index
     if params[:mailbox] == "sent"
       @messages = @user.sent_messages
     elsif params[:mailbox] == "unread"
       @messages = @user.received_messages.unread
     else
       @messages = @user.received_messages
     end


     respond_to do |format|
       format.html
       format.json { render :json => @messages }
     end
   end

so the unread path will show all my unread messages for a specific user. 因此未读路径将显示特定用户的所有我的未读消息。 I want to send those as a push notification. 我想将其作为推送通知发送。 Any advice? 有什么建议吗? I'm still learning so excuse the vagueness. 我还在学习,所以请谅解。

From this tutorial 本教程

You can create a notification using: 您可以使用以下方法创建通知:

device = #load your device
notification = APN::Notification.new   
notification.device = device   
notification.badge = 5   
notification.sound = true   
notification.alert = "My first push"   
notification.save 

this will only create a notification and store it, so tot actually send the notification run this rake task 这只会创建一个通知并将其存储,因此要实际发送通知,请运行此rake任务

rake apn:notifications:deliver  

or call this function 或调用此函数

APN::App.send_notifications

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM