LoginSignup
1
3

More than 5 years have passed since last update.

Nifty Cloud mobile backendの配信端末登録APIを作った話

Last updated at Posted at 2017-11-22

◆SDKに配信端末情報の登録機能が無かった

PUSH通知をアプリに実装しようと、Nifty Cloud mobile backend(以下、ncmb)を選択。

JSで開発していたので、Javascript SDKで配信端末情報の登録を行おうとしたら、SDKで出来るのは、配信端末情報の更新と削除のみ。

NCMB.Installation Class
http://mb.cloud.nifty.com/assets/sdk_doc/javascript/jsdoc/classes/NCMB.Installation.html#method_update

◆REST API用のシグネチャ生成でエラー連発

そこで、ncmbで用意されている配信端末登録APIを利用する事にした。

配信端末登録
http://mb.cloud.nifty.com/doc/current/rest/push/installationRegistration.html

しかし、ncmbで用意されているREST APIを利用するにはシグネチャ情報がいるのだが、これの生成がやっかいで、エラーを連発。

シグネチャの生成方法
http://mb.cloud.nifty.com/doc/current/rest/common/signature.html

◆ユーザーコミュニティに相談

そこで、ncmbのユーザーコミュニティで相談。

ユーザーコミュニティ
https://github.com/NIFTYCloud-mbaas/UserCommunity/issues/675

すると、すぐに回答がありました。

署名を作る処理は意外と面倒なので、JavaScript SDKを使っているのであればRequestを用いると楽です。以下は私のコードですが参考にしてください。

https://github.com/moongift/ncmb-cli/blob/aa0a25b634332bbe83b0468dac2aed75ed70ce3e/bin/ncmb-csv-import#L71

◆APIを自作して解決

という訳で、ncmbのSDKのソースを流用して、配信端末情報登録APIを作りました。

const ncmb = new NCMB(config.appKey, config.clKey);

router.post('/api', (req, res, next) => {

  let body = {
    "applicationName" : "testApp",
    "deviceToken" : "token1234567890abc",
    "deviceType" : "ios",
    "sdkVersion" : "2.3.4",
    "timeZone" : "Asia/Tokyo",
    "appVersion" : "1.0"
  }

  ncmb.request({
    path : `/${ncmb.version}/installations`,
    method : "POST",
    data : body
  })
  .then((obj) => {
    res.header('Content-Type', 'application/json; charset=utf-8');
    res.send(obj);
    return;
  })
  .catch((err) => {
    res.header('Content-Type', 'application/json; charset=utf-8');
    res.send({"err" : err});
    return;
  })
});
`
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3