support for "read" pushes

This commit is contained in:
or-else
2022-03-03 17:47:32 -08:00
parent 470d38e583
commit c6a9df9fe9
6 changed files with 51 additions and 21 deletions
+19 -12
View File
@@ -44,7 +44,11 @@ const i18n = {
'new_chat': "新聊天", 'new_chat': "新聊天",
} }
}; };
self.i18nMessage = function(id) { self.i18nMessage = function(id) {
if (!id) {
return null;
}
// Choose translations: given something like 'de-CH', try 'de-CH' then 'de' then 'en'. // Choose translations: given something like 'de-CH', try 'de-CH' then 'de' then 'en'.
const lang = i18n[self.locale] || i18n[self.baseLocale] || i18n['en']; const lang = i18n[self.locale] || i18n[self.baseLocale] || i18n['en'];
// Try finding string by id in the specified language, if missing try English, otherwise use the id itself // Try finding string by id in the specified language, if missing try English, otherwise use the id itself
@@ -63,22 +67,25 @@ fbMessaging.onBackgroundMessage((payload) => {
} }
// Notify webapp that a message was received. // Notify webapp that a message was received.
if (webAppChannel) { if (webAppChannel || payload.data.what == 'read') {
webAppChannel.postMessage(payload.data); webAppChannel.postMessage(payload.data);
} }
const titles = {'msg': 'new_message', 'sub': 'new_chat'};
const pushType = payload.data.what || 'msg'; const pushType = payload.data.what || 'msg';
const title = payload.data.title || self.i18nMessage(pushType == 'msg' ? 'new_message' : 'new_chat'); const title = payload.data.title || self.i18nMessage(titles[pushType]);
const options = { if (title) {
body: payload.data.content || '', // TODO: content for 'sub' should be topic's or user's title. const options = {
icon: '/img/logo96.png', // TODO: use topic's or user's avatar (would have to fetch for 'sub', read from db for 'msg'). body: payload.data.content || '', // TODO: content for 'sub' should be topic's or user's title.
badge: '/img/badge96.png', icon: '/img/logo96.png', // TODO: use topic's or user's avatar (would have to fetch for 'sub', read from db for 'msg').
tag: payload.data.topic || undefined, badge: '/img/badge96.png',
data: { tag: payload.data.topic || undefined,
topic: payload.data.topic data: {
} topic: payload.data.topic
}; }
return self.registration.showNotification(title, options); };
return self.registration.showNotification(title, options);
}
}); });
// Update service worker immediately for both the current client // Update service worker immediately for both the current client
+14 -4
View File
@@ -315,11 +315,21 @@ class TinodeWeb extends React.Component {
return tinode; return tinode;
} }
// Notifiy Tinode that a push message was received from the server. // Tinode received a push notification from the server.
handlePushMessage(data) { handlePushMessage(data) {
if (data.what == 'msg' && Tinode.isChannelTopicName(data.topic)) { switch (data.what) {
// The last argument is a fake user Id: otherwise the update is seen as one from the current user. case 'msg':
this.tinode.oobNotification(data.topic, data.seq, 'fake-uid'); if (Tinode.isChannelTopicName(data.topic)) {
// The last argument is a fake user Id: otherwise the update is seen as one from the current user.
this.tinode.oobNotification('msg', data.topic, data.seq, 'fake-uid');
}
break;
case 'read':
console.log("Message read on another device", data.seq);
this.tinode.oobNotification('read', data.topic, data.seq);
break;
default:
console.warn("Unknown push type ignored", data.what, data);
} }
} }
+15 -2
View File
@@ -7643,8 +7643,21 @@ class TinodeWeb extends (react__WEBPACK_IMPORTED_MODULE_0___default().Component)
} }
handlePushMessage(data) { handlePushMessage(data) {
if (data.what == 'msg' && tinode_sdk__WEBPACK_IMPORTED_MODULE_4___default().isChannelTopicName(data.topic)) { switch (data.what) {
this.tinode.oobNotification(data.topic, data.seq, 'fake-uid'); case 'msg':
if (tinode_sdk__WEBPACK_IMPORTED_MODULE_4___default().isChannelTopicName(data.topic)) {
this.tinode.oobNotification('msg', data.topic, data.seq, 'fake-uid');
}
break;
case 'read':
console.log("Message read on another device", data.seq);
this.tinode.oobNotification('read', data.topic, data.seq);
break;
default:
console.warn("Unknown push type ignored", data.what, data);
} }
} }
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long