現代のアプリケーションには、変化に対して即座に反応することが求められています。新しいメールが届いた場合、カレンダーの予定が更新された場合、あるいは項目が削除された場合でも、ユーザーは手動で更新することなく、インターフェースが常に同期された状態を維持することを期待しています。
このプロセスを簡素化するため、4D 21 R4 では、Google Workspace と Microsoft 365 の両方を対象とした統一された通知システムが4D NetKitに導入されました。単一のプログラミングモデルにより、メールやカレンダーの変更を購読し、データが変更されるたびに自動的に反応させることができます。
4D NetKitの主な利点の一つは、プロバイダーに関係なく、通知APIがまったく同じように動作することです。
Googleを使用している場合でもMicrosoft 365 を使用している場合でも、通知 API の動作は同じです:
cs.NetKit.Google.new($myOAuth2Provider).mail.notifier($notification)
cs.NetKit.Office365.new($myOAuth2Provider).mail.notifier($notification)
この原則はカレンダー通知にも同様に適用されます。
何を監視できますか?
4D NetKitを使用すると、メールとカレンダーイベントの両方の変更を購読できます。
以下のイベントに対して反応できます:
- 作成
- 変更
- 削除
対象:
- メール
- カレンダーの予定
変更が検出されるとすぐに、アプリケーションは通知を受け取り、ユーザーインターフェースの更新、データの再読み込み、アラートの表示、またはカスタムビジネスロジックの実行を行うことができます。
リスニングモードの選択
通知機能を起動する前に、4D NetKit が変更をどのように検出するかを定義する必要があります。
2 つのモードが利用可能です。
リアルタイム Webhook 通知
最初のオプションは、変更が発生するたびに Google または Microsoft がアプリケーションを呼び出すようにすることです。
このモードを有効にするには、コールバックURLを含むendPointプロパティを指定します:
$parameter.endPoint:="https://mydomain.com/notifications"
エンドポイントは以下の条件を満たす必要があります:
- 一般に公開されていること
- 有効なドメイン名を使用していること
- 信頼できるSSL証明書で保護されていること
- ドメイン名と一致する証明書を持っていること
設定が完了すると、登録済みのリソースに変更があった際、Google または Microsoft からアプリケーションに直接通知されます。
このアプローチにより、可能な限り最速の反応時間が実現され、Web アプリケーションやクラウドでホストされるソリューションに最適です。
// Microsoft Graph webhook handler
[
{
"class": "NetKit.GraphNotificationHandler",
"method": "getResponse",
"regexPattern": "/4dnk-graph-notification",
"verbs": "post"
}
]
// Google webhook handler
[
{
"class": "NetKit.GraphNotificationHandler",
"method": "getResponse",
"regexPattern": "/4dnk-google-notification",
"verbs": "post"
}
]
Gmail では、追加のパラメータ「topicName」が必要となります。これは、Gmail API がメールボックスの変更通知を配信するために使用するGoogle Cloud Pub/Sub トピックを指定するものです。
タイマーベースの監視
エンドポイントを指定しない場合、4D NetKit は自動的にタイマーベースの監視に切り替わります。
このモードでは、4D NetKit はGoogle または Microsoft の API に定期的にクエリを送信して変更を検知します。
$parameter.timer:=60
この例では、4D NetKit は60 秒ごとに更新を確認します。このアプローチは、次のような場合に特に役立ちます。アプリケーションがローカルネットワーク上で実行されている場合 パブリック HTTPS エンドポイントが利用できない場合 よりシンプルなデプロイモデルを好む場合
エンドポイントが指定されていない場合は、タイマーモードが自動的に有効になります。
コールバック関数の定義
電子メールやカレンダーイベントの監視のいずれの場合でも、コールバックのモデルは同じです。
便利な方法として、すべての通知処理を一元化する専用のクラスを作成することができます。
たとえば、NetKitEventNotification クラスを作成することができます。
// This function is triggered when a mail or calendar event is created
Function onCreate($provider : Object; $event : Object)
var $resource:=$event.type="mailCreated" ? "Email" : "Calendar event"
ALERT("You have a new "+$resource+" event")
// This function is triggered when a mail or calendar event is deleted
Function onDelete($provider : Object; $event : Object)
var $resource:=$event.type="mailDeleted" ? "Email" : "Calendar event"
ALERT("A "+$resource+" has been deleted")
// This function is triggered when a mail or calendar event is modified
Function onModify($provider : Object; $event : Object)
var $resource:=$event.type="mailModified" ? "Email" : "Calendar event"
ALERT("A "+$resource+" event has been modified")
上記の例では、endPoint プロパティは宣言されていません。そのため、カスタムタイマー値を設定しない限り、通知はデフォルトの 30 秒間隔で実行されるタイマーベースの監視モードで動作します。 リアルタイム Webhook 通知を使用する場合は、endPoint を定義する必要があります:
// Set this URL to use Real-Time Webhook Notifications
property endPoint:="https://mydomain.com/googlenotifications"
// Gmail webhook mode only: Google Cloud Pub/Sub topic used for mailbox notifications
property topicName:="projects/my-project/topics/gmail-notifications"
// This function is triggered when a mail or calendar event is created
Function onCreate($provider : Object; $event : Object)
var $resource:=$event.type="mailCreated" ? "Email" : "Calendar event"
ALERT("You have a new "+$resource+" event")
// This function is triggered when a mail or calendar event is deleted
Function onDelete($provider : Object; $event : Object)
var $resource:=$event.type="mailDeleted" ? "Email" : "Calendar event"
ALERT("A "+$resource+" has been deleted")
// This function is triggered when a mail or calendar event is modified
Function onModify($provider : Object; $event : Object)
var $resource:=$event.type="mailModified" ? "Email" : "Calendar event"
ALERT("A "+$resource+" event has been modified")
通知サブスクリプションの開始
コールバックを定義したら、通知発行者の作成は簡単です:
// Create a notification handler instance.
var $notification:=cs.NetKitEventNotification.new()
// Create a Gmail notifier using the OAuth2 provider and the handler.
var $gmailNotification:=cs.NetKit.Google.new($myOAuth2Provider).mail.notifier($notification)
// Start listening for Gmail notifications.
$gmailNotification.start()
// Create a Google Calendar notifier using the same handler.
var $calendarNotification:=cs.NetKit.Google.new($myOAuth2Provider).calendar.notifier($notification)
// Start listening for Calendar notifications.
$calendarNotification.start()
まとめ
4D NetKitの通知機能を使えば、アプリケーションとGoogle WorkspaceおよびMicrosoft 365のデータを簡単に同期させることができます。メールとカレンダーの変更の両方をサポートしているため、ユーザーのアクティビティにリアルタイムで反応するレスポンシブなアプリケーションを迅速に構築できます。
何よりも、プログラミングモデルはプロバイダーを問わず同一であるため、通知ロジックを一度記述するだけで、あらゆる場所で利用できます。
現在、この投稿へのコメント機能は利用できません。