LoginSignup
1
0

More than 3 years have passed since last update.

houstonを使ってrailsサーバーからプッシュ通知を送る時に設定できる値のメモ、あと多言語対応

Last updated at Posted at 2020-01-22

1. 概要

"houston"というgemを使って、ruby on railsのサーバーからiOSアプリにプッシュ通知を送るようにしました。

今回は本文・SE・バッジ数だけの一番シンプルな通知を実装しましたが、GitHubのUsageを参照すると、通知データに対して他にも設定できる項目があるようです

将来細かい設定が必要になった時に忘れないように、調べたことを書いておきます。

2. メモ

Usage.rb

require 'houston'

# 定数の宣言はメソッド外でする
APN = Houston::Client.development
APN.certificate = File.read('/path/to/apple_push_notification.pem')

# 通知を送りたいデバイスのdevice tokenを代入
token = '<ce8be627 2e43e855 16033e24 b4c28922 0eeda487 9c477160 b2545e95 b68b5969>'

# houstonのインスタンスを生成
notification = Houston::Notification.new(device: token)
# 通知メッセージを設定
notification.alert = 'Hello, World!'

# アプリアイコンの右上に表示するバッジの数字を設定(この数字がそのまま表示されるので、加算するための計算はこの前に行っておく)
notification.badge = 57
# 通知を受信した時の音声ファイル(カスタムする場合は事前に用意)
notification.sound = 'sosumi.aiff'

# これをtrueにしないと通知を受信しない
notification.content_available = true

# リッチ通知を実装する際はtrueを設定
notification.mutable_content = true
# リッチ通知を実装する際は、XcodeのプロジェクトからUNNotificationContentExtensionのInfo.plistのcategoryを以下の値と同じにする
notification.category = 'INVITE_CATEGORY'

# 通知の設定は"AnyHashable("aps"): {...}"の値として送信されるが、それ以外にデータを付け足したい場合はここを設定する
notification.custom_data = { foo: 'bar' }

# 以下の値は"AnyHashable("aps"): {...}"の値に含むことができるが、使い道がわからない……
notification.url_args = %w[boarding A998]
notification.thread_id = 'notify-team-ios'

# 通知を送信する
APN.push(notification)

3. (追記) 通知の多言語対応

プッシュ通知の多言語対応について書いた記事があまり見当たらなかったので、ついでにメモしておきます。

以下の方法を用いると、アプリ内の表示と同じように端末の言語設定に応じた通知の多言語対応をすることができます。

Localizable.strings
"GAME_PLAY_REQUEST_FORMAT" = "%@ and %@ have invited you to play Monopoly";
Usage.rb
notification.alert = {
  # Localizable.stringsに記述したkeyを入れる
  "loc-key" : "GAME_PLAY_REQUEST_FORMAT",
  # 変数を設定した場合は配列で指定する
  "loc-args" : [ "Jenna", "Frank"]
}

引用元:Creating the Remote Notification Payload

1
0
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
0