2017-04-23 23 views
-2

每次我嘗試啓動我的服務器我得到這個錯誤...web推送serviceworker未定義的方法錯誤?

undefined method `post' for main:Object (NoMethodError) 

application.rb中

post "/push" do 
    Webpush.payload_send(
     endpoint: "https://fcm.googleapis.com/gcm/send/eah7hak....", 
     message: "A message", 
     p256dh: "BO/aG9nYXNkZmFkc2ZmZHNmYWRzZmFl...", 
     auth: "aW1hcmthcmFpa3V6ZQ==", 
     vapid: { 
     subject: "mailto:[email protected]", 
     public_key: ENV['VAPID_PUBLIC_KEY'], 
     private_key: ENV['VAPID_PRIVATE_KEY'] 
     } 
    ) 
end 

上午我把代碼塊中錯誤的地方?我認爲app.rb是一回事application.rb

enter image description here

截圖是從git的教程:https://github.com/zaru/webpush

我試圖用VAPID和寶石實現瀏覽器通知webpush & serviceworker-rails

的application.js

// Register the serviceWorker script at /serviceworker.js from our server if supported 
if (navigator.serviceWorker) { 
    navigator.serviceWorker.register('/serviceworker.js') 
    .then(function(reg) { 
    console.log('Service worker change, registered the service worker'); 
    }); 
} 
// Otherwise, no push notifications :(
else { 
    console.error('Service worker is not supported in this browser'); 
} 

// When serviceWorker is supported, installed, and activated, 
// subscribe the pushManager property with the vapidPublicKey 
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => { 
    serviceWorkerRegistration.pushManager 
    .subscribe({ 
    userVisibleOnly: true, 
    applicationServerKey: window.vapidPublicKey 
    }); 
}); 

$('.webpush-button').on('click', (e) => { 
    navigator.serviceWorker.ready 
    .then((serviceWorkerRegistration) => { 
    serviceWorkerRegistration.pushManager.getSubscription() 
    .then((subscription) => { 
     $.post('/push', { 
     subscription: subscription.toJSON(), 
     message: 'You clicked a button!' 
     }); 
    }); 
    }); 
}); 

// Let's check if the browser supports notifications 
if (!("Notification" in window)) { 
    console.error("This browser does not support desktop notification"); 
} 

// Let's check whether notification permissions have already been granted 
else if (Notification.permission === "granted") { 
    console.log("Permission to receive notifications has been granted"); 
} 

// Otherwise, we need to ask the user for permission 
else if (Notification.permission !== 'denied') { 
    Notification.requestPermission(function (permission) { 
    // If the user accepts, let's create a notification 
    if (permission === "granted") { 
     console.log("Permission to receive notifications has been granted"); 
    } 
    }); 
} 
+2

是的,你把代碼放在錯誤的地方,'app.rb'不適用於Rails應用程序。嘗試在軌道控制器中使用該代碼。 – Gerry

+1

這一切都取決於你的導軌設計,如果你想成爲你的根頁面,然後創建根路線,否則任何路線將做到這一點。方法名稱(aka action)也是如此。 – Gerry

回答

1

簡單的例子,將其添加在控制器。

首先,創建與動作的名稱控制器(我將使用push,按照你的例子):

$ rails g controller Test push 

這將創建一個測試控制器,將更新您的路線:

# app/controllers/test_controller.rb 

class TestController < ApplicationController 
    def push 
    end 
end 

# config/routes.rb 

get 'test/push' 

與代碼下一步更新控制器要添加:

class TestController < ApplicationController 
    def push 
    Webpush.payload_send(
     message: params[:message], 
     endpoint: params[:subscription][:endpoint], 
     p256dh: params[:subscription][:keys][:p256dh], 
     auth: params[:subscription][:keys][:auth], 
     vapid: { 
     subject: "mailto:[email protected]", 
     public_key: ENV['VAPID_PUBLIC_KEY'], 
     private_key: ENV['VAPID_PRIVATE_KEY'] 
     } 
    ) 
    end 
end 

和最多日期您的航線使用post而不是get

post 'test/push' 

有了,你將有一個POST yoursite /測試/推可端點(就如同app.rb的例子)。

+0

是的,你沒有發送任何參數給你的動作,所以'params'哈希是空的,因此,當試圖獲得'params [:subscription] [:endpoint]'時,你會得到'NoMethodError',因爲'params [訂閱]'是'nil','nil'不響應'[]'方法(即'[:endpoint]')。而不是'link_to'使用'form_for'或者一個簡單的html表單和你需要的參數。 – Gerry

+0

我試過'form_for'和'<%= link_to'發送',push_path,{message:'YEAAA!',端點:'value2',p256dh:'value1',auth:'value2',訂閱:'value1',鍵:'value2',method::post}%>'但我仍然得到錯誤。無論如何,這些價值觀應該來自寶石,所以我對如何着手有點困惑。我在這裏問了一個新問題:http://stackoverflow.com/questions/43572177/subscription-params-nil-for-web-push-serviceworker ...如果你有時間繼續談話:)再次感謝你的幫幫我! –