diff --git a/Whisper-Push-Notifications.md b/Whisper-Push-Notifications.md index fffd1ea..3163a71 100644 --- a/Whisper-Push-Notifications.md +++ b/Whisper-Push-Notifications.md @@ -178,10 +178,69 @@ Now, why do we have distinct client and chat keys? The reason is simple: client Here is how you create chat session key might look like (remember we are still using `web3` object connected to `Device A` node): ```js +var newChatSessionTopic = '0x509579a2'; // NEW_CHAT_SESSION +var ackNewChatSessionTopic = '0xd012aae8'; // ACK_NEW_CHAT_SESSION +var createChatSession = function (subscriptionKey) { + var chatId = '0xdeadbeef'; + // subscriptionKey is key shared by server that allows us to communicate with server privately + var keyname = 'SUBSCRIPTION_KEY'; + web3.shh.deleteSymKey(keyname); + web3.shh.addSymKey(keyname, subscriptionKey); + + console.log("subscription key: ", subscriptionKey); + + // before sending new chat request, let's start waiting for response + var filter = web3.shh.filter({ + to: identity, + topics: [ackNewChatSessionTopic] + }); + filter.watch(function (error, result) { + if (!error) { + console.log("Chat Creation ACK received: ", result); + // response will be in JSON, e.g. {"server": "0xdeadbeef", "key": "0xdeadbeef"} + // which will give you serverID + var payload = JSON.parse(web3.toAscii(result.payload)); + + // no need to watch for the filter any more + filter.stopWatching(); + + // ok, at this point we have Chat Session SymKey, and we can: + // 1. register our device with that chat + // 2. share that key with others, so that they can register themselves + // 3. use chat key to trigger notifications + shareAndUseChatKey(payload.key) + } + }); + + var err = web3.shh.post({ + from: identity, + topics: [newChatSessionTopic], + payload: '{"chat": "' + chatId + '"}', // globally unique chat ID + ttl: 20, + keyname: keyname + }); + if (err !== null) { + console.log("message NOT sent") + } else { + console.log("message sent OK") + } +}; ``` +Now, we have Chat Session Key, which we can use to register our device id (to be used as target by FCM), and allow others to register themselves. ### 3. Sharing Chat session key, and allowing notifications +So, all we need to review is device registration and key sharing. Let's continue with `Device A` web3 object: + +```js +``` + +Now, on `Device B` side, we need to wait for shared key, and then once we register `Device B`, we will be able to send notifications: + +```js +``` + +### 4. Triggering Push Notifications # Implementation notes I: One-to-One Chats