Firebase:サンプルから学ぶ (FriendlyChat:web)

# 本ブログの内容は、2019/03/30 時点の内容となります。

 

先日、Firebase のイベントに行く機会があり、事前学習のために Forebase の公式サンプル Firebase を試してみました。

今回は、その中から「FriendlyChat」を選んでみました。

 

15 のセクションに分かれていて、目安時間は 74 分となっています。 すべて英語なので、英語サイトと翻訳結果を見比べながら進めました。

 

エディタは、Visual Studio Code を利用します。 非常に使い勝手がよく、拡張機能も豊富です。 技術評論社Software Design でも 連載があり、2019年 4月号では特集も組まれています。 人気の高さを物語るかのように、出版社の在庫はない状態のようです。

 


解説は丁寧に書かれているので、概ねはハマるところはありませんでした。

唯一ハマったのは、端末への通知を許可するための cURL コマンドの発行部分でした。

 

ハマりポイント: Windows からの cURL 発行

コマンドプロンプトからの実行では、SERVER KEY や DEVICE TOKEN が長いためなのか、実行すると以下のようなエラーとなった。

 

curl: (6) Could not resolve host: notification
curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: title
curl: (6) Could not resolve host: New chat message!,
curl: (6) Could not resolve host: body
curl: (6) Could not resolve host: There is a new message in FriendlyChat,
curl: (6) Could not resolve host: icon
curl: (3) <url> malformed
curl: (6) Could not resolve host: click_action
<!doctype html>
<!--
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0

・・・・・

</div>

<!-- Import and configure the Firebase SDK -->
<!-- These scripts are made available when the app is served or deployed on Firebase Hosting -->
<!-- If you do not want to serve/host your project using Firebase Hosting see https://firebase.google.com/docs/web/setup -->
<script src="/__/firebase/5.7.3/firebase-app.js"></script>
<script src="/__/firebase/5.7.3/firebase-auth.js"></script>
<script src="/__/firebase/5.7.3/firebase-storage.js"></script>
<script src="/__/firebase/5.7.3/firebase-messaging.js"></script>
<script src="/__/firebase/5.7.3/firebase-firestore.js"></script>
<script src="/__/firebase/init.js"></script>

<script src="scripts/main.js"></script>
</body>
</html>
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (6) Could not resolve host: to
curl: (3) [globbing] unmatched close brace/bracket in column 153
JSON_PARSING_ERROR: Unexpected character (') at position 0.

 

色々調べてみると、Postman(https://www.getpostman.com/*1というツールがあるのを知り、さっそくインストール。


インストールして、以下のように入力を行い・・・

f:id:Omatchan:20190330141825p:plain

f:id:Omatchan:20190330141852p:plain


[Send]ボタンを押すと、正常に処理されました。

f:id:Omatchan:20190330142159p:plain

 

最終的に外部公開により、スマホとのやり取りができるようになりました。

f:id:Omatchan:20190330145209p:plain

 

まだまだ、写経レベルで、意味を把握しているわけではないですが、非常に便利なので、今後もいろいろ試していきます。

 

 

*1:Postman は Chrome のアプリも存在するようです。 別に機会にこちらも試してみたいと思います。