2013-03-12 141 views
2

我想在每次用户收到新消息时发送一个苹果推送通知。我发现这个教程http://www.waratuman.com/2011/01/13/apple-push-with-heroku/Apple推送通知Rails应用程序消息传送

,并使用此代码

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 

我知道这个代码应该建立通知和注册设备,但是当我真正想要发送通知新消息又该我做?

我现在有消息索引

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 

所以未读的路径会显示特定用户的所有未读邮件。我想发送这些推送通知。有什么建议?我仍然在学习如此模糊不清。

回答

1

this tutorial

可以使用创建通知:

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

这只会造成一个通知和储存,所以TOT实际发送的通知来看,这种rake任务

rake apn:notifications:deliver 

或致电此功能

APN::App.send_notifications