Wovnプッシュ通知翻訳機能(自動翻訳)
お手元のアプリが既にApple Push Notification service(APNs )またはFirebase を通じてプッシュ通知を受信できる状態であることを前提としています。
デバッグをよりスムーズに行うために、以下の手順を推奨します:
- まず、Wovn SDKを使わずに、通知内容を表示前に変更できるようアプリを設定してください。
- 設定方法は Apple の公式ドキュメント や他のオンラインリソースをご参照ください。
- 上記設定が完了したら、Wovn SDKを組み込んで通知内容の翻訳を行います。
多くのトラブルは、手順1の不備に起因しています。
1. Wovn SDK経由で通知を翻訳する方法
- まず、テストデバイスにテストプッシュ通知を送信します。これにより、Wovnダッシュボードに通知データを報告して翻訳できるようになります。
- Wovnダッシュボードが通知データを翻訳した後、同じ通知を再度テストデバイスに送信して確認します。
- 通知が翻訳されたことを確認した後、すべてのユーザーに送信できます。
2. Wovnがサーバーから通知データにアクセスして、エンドユーザーに表示する前に翻訳する方法
-
サーバー側で送信するすべてのプッシュ通知に
"mutable_content": true
を含めてください。これが指定されていない場合、アプリは通知が編集不要と判断し、そのまま表示されます。 -
通知の内容をカスタマイズするロジックを追加していない場合は、
UNNotificationServiceExtension
を拡張してください。手順は Apple の公式ドキュメントを参照してください: https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension -
UNNotificationServiceExtension
はメインターゲットとは別のサンドボックス環境で実行されるため、両者間でデータを共有するブリッジの構築が必要です。これには App Groups を使用します。- メインターゲットの App Groups に
group.io.wovn.workbox
を追加します(例:wovn-ios-demoapp)。 - Notification Service Extension 側の App Groups にも同様に
group.io.wovn.workbox
を追加します。
- メインターゲットの App Groups に
-
メインターゲットおよび
UNNotificationServiceExtension
の両方で、Wovn SDK に App Group(group.io.wovn.workbox
)を設定してください。これにより、通知内容が翻訳されるようになります。
AppDelegate.swift設定
- v3.5.0以上
- v3.4.1以下
AppDelegate.swiftで、Wovn.start
のコードパラメータappGroupIdentifier
を"group.io.wovn.workbox"
に、isNotificationServiceEnv
をfalse
に変更します。例:Wovn.start(appGroupIdentifier: "group.io.wovn.workbox", isNotificationServiceEnv: false)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// アプリケーションのコード
// [変更前]
// Wovn.start(isDebugMode: _isDebugAssertConfiguration(), autoTranslateUIKit: true)
// [変更後]
Wovn.start(
appGroupIdentifier: "group.io.wovn.workbox",
isDebugMode: _isDebugAssertConfiguration(),
autoTranslateUIKit: true,
isNotificationServiceEnv: false
)
// wovnのコード
// その他のアプリケーションコード
}
AppDelegate.swiftで、Wovn.eventTypeReport
を呼び出す前に、Wovn.setAppGroupIdentifier("group.io.wovn.workbox")
を追加します
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// アプリケーションのコード
// [変更前]
// Wovn.eventTypeReport(_isDebugAssertConfiguration())
// [変更後]
Wovn.setAppGroupIdentifier("group.io.wovn.workbox")
Wovn.eventTypeReport(_isDebugAssertConfiguration())
// wovnのコード
// その他のアプリケーションコード
}
NotificationService.swift設定
- v3.5.0以上
- v3.4.1以下
UNNotificationServiceExtensionフォルダ内のNotificationService.swiftに以下のコードを追加します。
import UserNotifications
import WOVNswift
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
Wovn.start(appGroupIdentifier: "group.io.wovn.workbox", isNotificationServiceEnv: true)
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
let translatedContent = Wovn.translateNotificationContent(notiContent: bestAttemptContent)
contentHandler(translatedContent)
}
}
override func serviceExtensionTimeWillExpire() {
// システムによって拡張が終了される直前に呼び出されます。
// これを機会に変更されたコンテンツの「最善の試み」を配信してください。そうでなければ元のプッシュペイロードが使用されます。
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
UNNotificationServiceExtensionフォルダ内のNotificationService.swiftに以下のコードを追加します。
import UserNotifications
import WOVNswift
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
Wovn.setAppGroupIdentifier("group.io.wovn.workbox", isMainApp: false)
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
let translatedContent = Wovn.translateNotificationContent(notiContent: bestAttemptContent)
contentHandler(translatedContent)
}
}
override func serviceExtensionTimeWillExpire() {
// システムによって拡張が終了される直前に呼び出されます 。
// これを機会に変更されたコンテンツの「最善の試み」を配信してください。そうでなければ元のプッシュペイロードが使用されます。
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
- 手順4の後、プッシュ通知の内容が翻訳され、translatedContentに保存されます。上記のコードでは、そのまま表示しています。しかし、エンドユーザーに表示する前にさらにカスタマイズすることができます。
機能のテスト
-
以下のデータでFirebase Cloud Messagingを介してデバイスにプッシュ通知を送信できます
{
"to": "{YOUR TESTING DEVICE FCM TOKEN}",
"notification": {
"mutable_content": true, // この行を忘れないでください
"title": "こんにちは",
"body": "素晴らしい日ですね!"
}
} -
初回は、テストデバイスは日本語で通知を受信するはずです。これは、Wovnがまだその翻訳を持っていないためです。Wovnにデータを報告することを許可している場合、Wovnダッシュボードの PushNotification 画面で通知のタイトルと本文を見つけることができるはずです
-
-
2つの値を翻訳した後、
保存して公開
をクリックすると、次回同じ通知を送信するときから、エンドユーザーの言語に翻訳されるはずです。 -
3. 機密ユーザー情報を含む通知データを翻訳するためにWovnを使用する方法
-
ユーザーデータを通知の
title
やbody
に直接追加する代わりに、通知のdataセクション内にそのデータを含めて%{variable_name}%
を使用できます。{
"to": "{YOUR TESTING DEVICE FCM TOKEN}",
"notification": {
"mutable_content": true, // この行を忘れないでください
"title": "今月の請求書の準備ができました, %name%さん!",
"body": "今月は%amount%円です。 %date%までにお支払いください"
},
"data": {
"name": "John Doe",
"amount": "10000",
"date": "2023/12/12"
}
} -
通常のプッシュ通知をテストするときと同じプロセスに従って、ユーザーの機密データを含む通知も、情報を漏洩させることなく翻訳できます。
-
4. ローカルプッシュ通知を翻訳するためにWovnを使用する方法
Wovn.translateNotificationData
を使用して、表示前にローカルプッシュ通知データを翻訳できます。
var content = UNMutableNotificationContent()
content.title = "こんにちは%name%さん"
content.subtitle = "お金を払ってください.%amount%ドルです"
content.body = "期限内にお支払いをお願いいたします。%amount%ドルです"
content.userInfo.updateValue("Jone Doe", forKey: "name")
content.userInfo.updateValue("150", forKey: "amount")
content.userInfo.updateValue("2099/12/12", forKey: "date")
content.sound = UNNotificationSound.default
content = Wovn.translateNotificationContent(notiContent: content)
// ローカルプッシュ通知表示のコード