rails6和actioncable |推送通知在桌面chrome上工作,而不是在移动设备上

pkln4tw6  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(241)

我正在向Rails6应用程序添加推送通知功能。我在开发中使用actioncable和async adapter,在生产中使用redis,以便在用户将任务分配给另一个用户时成功地发送桌面通知。当我用手机做同样的事情时,我不会收到任何通知(在android上使用chrome,无论是在普通版本还是安装的pwa版本上)。如果我从手机向桌面上的用户分配任务,他将收到通知。除了在手机上向用户显示通知外,似乎一切都很正常。
连接.rb:

identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    def session
      cookies.encrypted[Rails.application.config.session_options[:key]]
    end

    protected

    def find_verified_user
      User.find_by(id: session["warden.user.user.key"][0][0])
    end

通知通道.rb:

class NotificationChannel < ApplicationCable::Channel
  def subscribed
    stream_for current_user
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

通知\u channel.js:

import consumer from "./consumer"

consumer.subscriptions.create("NotificationChannel", {
  connected() {
    // Called when the subscription is ready for use on the server
  },

  disconnected() {
    // Called when the subscription has been terminated by the server
  },

  received(data) {
    if (Notification.permission === 'granted') {
      var title = 'Push Notification'
      var body = data
      var options = { body: body }
      new Notification(title, options)
    }
  }
});

任务控制器.rb:

@house.users.each do |user|
  NotificationChannel.broadcast_to(user, "#{current_user.name} challenged you with #{@task.name}")
end

服务人员:

var CACHE_VERSION = 'v1';
var CACHE_NAME = CACHE_VERSION + ':sw-cache-';

function onInstall(event) {
  console.log('[Serviceworker]', "Installing!", event);
  event.waitUntil(
    caches.open(CACHE_NAME).then(function prefill(cache) {
      return cache.addAll([
        '<%= asset_pack_path 'application.js' %>',
        '<%= asset_pack_path 'application.css' %>',
      ]);
    })
  );
}

function onActivate(event) {
  console.log('[Serviceworker]', "Activating!", event);
  event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.filter(function(cacheName) {
          // Return true if you want to remove this cache,
          // but remember that caches are shared across
          // the whole origin
          return cacheName.indexOf(CACHE_VERSION) !== 0;
        }).map(function(cacheName) {
          return caches.delete(cacheName);
        })
      );
    })
  );
}

// Borrowed from https://github.com/TalAter/UpUp
function onFetch(event) {
  event.respondWith(
    // try to return untouched request from network first
    fetch(event.request).catch(function() {
      // if it fails, try to return request from the cache
      return caches.match(event.request).then(function(response) {
        if (response) {
          return response;
        }
        // if not found in cache, return default offline content for navigate requests
        if (event.request.mode === 'navigate' ||
          (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) {
          console.log('[Serviceworker]', "Fetching offline content", event);
          return caches.match('/offline.html');
        }
      })
    })
  );
}

self.addEventListener('install', onInstall);
self.addEventListener('activate', onActivate);
self.addEventListener('fetch', onFetch);

控制台日志:

2020-04-13T14:14:00.223160+00:00 app[web.1]: I, [2020-04-13T14:14:00.223074 #4]  INFO -- : [b4ebe1e2-f3df-485b-8c24-dc91656bb6db] Started GET "/cable" for 79.178.15.179 at 2020-04-13 14:14:00 +0000
2020-04-13T14:14:00.223561+00:00 app[web.1]: I, [2020-04-13T14:14:00.223509 #4]  INFO -- : [b4ebe1e2-f3df-485b-8c24-dc91656bb6db] Started GET "/cable/" [WebSocket] for 79.178.15.179 at 2020-04-13 14:14:00 +0000
2020-04-13T14:14:00.223641+00:00 app[web.1]: I, [2020-04-13T14:14:00.223594 #4]  INFO -- : [b4ebe1e2-f3df-485b-8c24-dc91656bb6db] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
2020-04-13T14:14:00.231064+00:00 app[web.1]: D, [2020-04-13T14:14:00.230963 #4] DEBUG -- :   User Load (4.8ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 80], ["LIMIT", 1]]
2020-04-13T14:14:00.231809+00:00 app[web.1]: I, [2020-04-13T14:14:00.231729 #4]  INFO -- : Registered connection (Z2lkOi8vcGlua2FzL1VzZXIvODA)
2020-04-13T14:14:00.381473+00:00 app[web.1]: I, [2020-04-13T14:14:00.381364 #4]  INFO -- : Finished "/cable/" [WebSocket] for 79.178.15.179 at 2020-04-13 14:14:00 +0000

2020-04-13T14:13:38.061012+00:00 app[web.1]: D, [2020-04-13T14:13:38.060929 #4] DEBUG -- : [b289eb0a-5982-406c-905a-9dc1ed6dafe8] [ActionCable] Broadcasting to notification:Z2lkOi8vcGlua2FzL1VzZXIvMjY: "notification body"

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题