Firebase云消息传递在三星互联网浏览器中无法工作

3

我正在设置Firebase Cloud Messaging来进行网络推送通知。目前,它只在Chrome(Windows和Android)以及Firefox(Android)中工作。但是,在预装于三星手机上的Samsung Internet Browser上却无法正常工作,而且我还没有机会在iOS上进行测试。

我已经尝试将gcm_sender_id作为发送方ID添加到我使用的Cloud Function以及manifest.json文件中,但没有用。以下是通知主体的设置方式。

// Create notification content
const notification = admin.messaging().Notification = {
    title : 'My test Title',
    body : `Lorem Ipsum Dolor`,

};

const payload = admin.messaging().Message = {
    notification,
    webpush:{
        notification : {
            vibrate: [200, 100, 200],
            icon: 'https://www.goodhousekeeping.com/life/pets/g4531/cutest-dog-breeds/', //A random dog photo
            fcm_options: {
                link: 'https://www.youtube.com',
                gcm_sender_id : '<SENDER_ID>',
            },
        },
    },
    topic: '<TOPIC>'
};
   //Send notification
   return admin.messaging().send(payload);

有什么办法可以让这个在三星浏览器上工作?服务工作者从v4开始就得到支持,设备已经更新到v9。值得注意的是,在即使在接收到它的设备上,当我点击它时,它也不会打开我在fcm_options中设置的网站,也不会遵循振动模式,但它确实加载了图标。

更新:截至2020年4月,FCM与iOS Chrome和Safari完全不兼容。

1个回答

1

我知道这可能没有什么帮助,但它今天“神奇地”开始工作了。浏览器版本是Samsung Internet v10。

firebase-messaging-sw.js

// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/7.13.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.13.2/firebase-messaging.js');


// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
    apiKey: '',
    authDomain: '',
    databaseURL: '',
    projectId: '',
    storageBucket: '',
    messagingSenderId: '',
    appId: '',
    measurementId: ''
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(payload => {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification
  const notificationTitle = payload.data.title;
  const notificationOptions = {
    body: payload.data.body,
    priority: payload.data.priority,
    vibrate: payload.data.vibrate,
    icon: payload.data.icon,
    click_action: payload.data.link,
    link: payload.data.link
  };


  return self.registration.showNotification(notificationTitle,
    notificationOptions);
});

//Open browser window or focus it if it is open on notification click
self.addEventListener('notificationclick', function(event) {
  event.notification.close();
  event.waitUntil(self.clients.openWindow('www.yourwebsitehere.com'));
});

云函数发送通知
//Sends notifications to users when the statistics document is updated
exports.sendNotifications = functions.firestore.document('restaurant/statistics').onUpdate(async (snapshot,context) =>{
    //Get updated document data
    const updatedData = snapshot.after.data();

    const payload = admin.messaging().Message = {
        data: {
            title: updatedData.title,
            body : updatedData.body,
            icon: updatedData.icon,
            link: updatedData.link,
            vibrate: "[300, 200, 300]",
            priority: "high"
         },
        topic: 'statistics'
    }

    return admin.messaging().send(payload);
});

这个三星浏览器甚至没有提供禁用推送通知以重置注册令牌的手段,有什么解决办法吗? - andreszs
@andreszs,尝试清除缓存并删除网站数据? - SRR
谢谢,不过应该还有其他的方法吧。。而且这个浏览器是预装在所有三星手机里的?我们完了。 - andreszs
我的应用程序没有生成令牌,而是Chrome在生成。我该怎么办才能解决这个问题? - user2828442
@user2828442,您应该提出一个单独的问题并在此处链接它。那时我很乐意去看一下。 - SRR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接