iOS端Blazor WASM Web Push通知无法生效求助
iOS端Web Push通知实现问题
我按照Blazor Pizza示例完成了Web Push通知的开发,桌面端可以正常接收通知。这里提个注意点:该教程只要求更新service-worker.js,但如果是发布应用,还需要同步更新service-worker.publish.js。
目前我能在iOS上将应用安装为PWA,系统会弹出通知授权提示,也成功获取到了订阅信息:
url - https://web.push.apple.com/QL6zHJz733--fiTROuD0EEGfsi0hhs7RUdZFXzx... p256dh - BCZ+cL6Fy1BFiBLEgJNs2dbA6YzkSYyS... auth - HJd1....
我的service-worker.js代码:
console.log('Installing service worker...'); self.skipWaiting(); }); self.addEventListener('fetch', event => { // You can add custom logic here for controlling whether to use cached data if offline, etc. // The following line opts out, so requests go directly to the network as usual. return null; }); self.addEventListener('push', event => { const payload = event.data.json(); event.waitUntil( self.registration.showNotification('MyApp', { body: payload.message, icon: 'icon-512.png', vibrate: [100, 50, 100], data: { url: payload.url } }) ); }); self.addEventListener('notificationclick', event => { event.notification.close(); event.waitUntil(clients.openWindow(event.notification.data.url)); });
我的service-worker.publish.js代码:
// offline support. See https://aka.ms/blazor-offline-considerations self.importScripts('./service-worker-assets.js'); self.addEventListener('install', event => event.waitUntil(onInstall(event))); self.addEventListener('activate', event => event.waitUntil(onActivate(event))); self.addEventListener('fetch', event => event.respondWith(onFetch(event))); const cacheNamePrefix = 'offline-cache-'; const cacheName = `${cacheNamePrefix}${self.assetsManifest.version}`; const offlineAssetsInclude = [ /\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/, /\.blat$/, /\.dat$/ ]; const offlineAssetsExclude = [ /^service-worker\.js$/ ]; // Replace with your base path if you are hosting on a subfolder. Ensure there is a trailing '/'. const base = "/"; const baseUrl = new URL(base, self.origin); const manifestUrlList = self.assetsManifest.assets.map(asset => new URL(asset.url, baseUrl).href); async function onInstall(event) { console.info('Service worker: Install'); // Fetch and cache all matching items from the assets manifest const assetsRequests = self.assetsManifest.assets .filter(asset => offlineAssetsInclude.some(pattern => pattern.test(asset.url))) .filter(asset => !offlineAssetsExclude.some(pattern => pattern.test(asset.url))) .map(asset => new Request(asset.url, { integrity: asset.hash, cache: 'no-cache' })); await caches.open(cacheName).then(cache => cache.addAll(assetsRequests)); } async function onActivate(event) { console.info('Service worker: Activate'); // Delete unused caches const cacheKeys = await caches.keys(); await Promise.all(cacheKeys .filter(key => key.startsWith(cacheNamePrefix) && key !== cacheName) .map(key => caches.delete(key))); } async function onFetch(event) { let cachedResponse = null; if (event.request.method === 'GET') { // For all navigation requests, try to serve index.html from cache, // unless that request is for an offline resource. // If you need some URLs to be server-rendered, edit the following check to exclude those URLs const shouldServeIndexHtml = event.request.mode === 'navigate' && !manifestUrlList.some(url => url === event.request.url); const request = shouldServeIndexHtml ? 'index.html' : event.request; const cache = await caches.open(cacheName); cachedResponse = await cache.match(request); } return cachedResponse || fetch(event.request); } self.addEventListener('push', event => { const payload = event.data.json(); event.waitUntil( self.registration.showNotification('MyApp', { body: payload.message, icon: 'icon-512.png', vibrate: [100, 50, 100], data: { url: payload.url } }) ); }); self.addEventListener('notificationclick', event => { event.notification.close(); event.waitUntil(clients.openWindow(event.notification.data.url)); });
服务端推送代码(使用web-push-csharp库):
var pushSubscription = new PushSubscription(webPushNotification.Url, webPushNotification.P256dh, webPushNotification.Auth); var vapidDetails = new VapidDetails("mailto:<info@example.com>", publicKey, privateKey); var webPushClient = new WebPushClient(); try { var payload = JsonConvert.SerializeObject(new { message = "MyApp Notification", url = $"/", }); await webPushClient.SendNotificationAsync(pushSubscription, payload, vapidDetails); } catch (Exception ex) { Console.Error.WriteLine("Error sending push notification: " + ex.Message); }
发送通知时没有报错,但针对https://web.push.apple.com/的订阅始终无法收到通知。有没有人成功实现向该地址推送Web Push通知?
内容的提问来源于stack exchange,提问作者seanmt13
相关产品推荐
相关产品推荐

