LoginSignup
2
3

More than 5 years have passed since last update.

Firebase(FCM)を使ったボタン付リッチプッシュ送信方法

Last updated at Posted at 2017-12-04

FCMを使ってボタン付リッチプッシュ通知送信(ツール:Postman)

  1. mutable_contentを「true」に設定
  2. dataにアプリに送りたいタグを追加(私の場合はdata-url, data-typeを追加)、こちらに追加したバグ名でアプリからデータを取得
  3. notificationのtitle,bodyには通知のタイトルと内容を記入
  4. click_actionはアプリにボタンを追加したい場合、アプリで設定したカテゴリ名を記入する。
  5. toには送りたい端末のトークンを記入

スクリーンショット 2017-12-04 10.52.02.png

アプリ側のカテゴリ実装方法(ボタン追加)

Notification Service Extensionをプロジェクトに追加

スクリーンショット 2017-12-04 10.59.33.png

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()

    }
}
2
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
2
3