FCMを使ってボタン付リッチプッシュ通知送信(ツール:Postman)
- mutable_contentを「true」に設定
- dataにアプリに送りたいタグを追加(私の場合はdata-url, data-typeを追加)、こちらに追加したバグ名でアプリからデータを取得
- notificationのtitle,bodyには通知のタイトルと内容を記入
- click_actionはアプリにボタンを追加したい場合、アプリで設定したカテゴリ名を記入する。
- toには送りたい端末のトークンを記入
アプリ側のカテゴリ実装方法(ボタン追加)
Notification Service Extensionをプロジェクトに追加
NotificationService.swiftのdidReceive関数に以下のコード追加
NotificationService.swift
//プッシュ通知にボタンを追加
let yesAction = UNNotificationAction(identifier: "yes", title: "はい", options: [])
let noAction = UNNotificationAction(identifier: "no", title: "いいえ", options: [])
//以下のカテゴリidentifier名はプッシュ通知送信側のclick_actionに追加
let category = UNNotificationCategory(identifier: "btnCategory", actions: [yesAction, noAction], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
AppDeleagate.swiftにボタンイベント処理を追加
AppDeleagate.swift
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
if response.actionIdentifier == "yes" {
let alert = UIAlertController(
title: "YES",
message: "YESをクリック",
preferredStyle: .alert)
let defaultAction: UIAlertAction = UIAlertAction(title: "OK", style: .default, handler:{
(action: UIAlertAction!) -> Void in
})
alert.addAction(defaultAction)
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
if response.actionIdentifier == "no" {
let alert = UIAlertController(
title: "NO",
message: "NOをクリック",
preferredStyle: .alert)
let defaultAction: UIAlertAction = UIAlertAction(title: "OK", style: .default, handler:{
(action: UIAlertAction!) -> Void in
})
alert.addAction(defaultAction)
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
completionHandler()
}
}