pslaboが試したことの記録

はてなダイヤリーからはてなブログに引っ越してきました

この日記は現在実行中の減量記録を含む個人的なメモとして始めましたが、最近はコンピュータやガジェット、ハック、セキュリティネタのほうがメインになっております。

はてなダイヤリー時代はカテゴリ分けが適当だったのですが、これはそのうち直します。


Firebase Cloud Messaging で node からメッセージを飛ばす

DelphiAndroid 向けにリモートプッシュ通知を送信しようと思ったら GCM はオワコンで FCM が必要なのに、Delphi は FCM に正式対応していない中、とりあえずサーバでの送信側実装とクライアントでの受信側実装のプロトタイプを作っている。

クライアント側は一応動くようになったので、サーバ側の試作のために一旦 node で書いてみることにします。

ここらへんを読みながら...

https://firebase.google.com/docs/admin/setup?hl=ja https://firebase.google.com/docs/cloud-messaging/admin/send-messages?hl=ja

なんとなくノリだけで書いたコードがこちら。

var admin = require('firebase-admin');

admin.initializeApp({
  credential: admin.credential.cert({
  "type": "service_account",
  "project_id": "fcmremotepush-xxxxxxx",
      .
      .
      .
  databaseURL: 'https://fcmremotepush-xxxxxxx.firebaseio.com'
});

// This registration token comes from the client FCM SDKs.
var registrationToken = 'メッセージ送信対象デバイスのトークン';

// See documentation on defining a message payload.
var message = {
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!",
    },
  token: registrationToken
};

// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });