fc2ブログ

記事一覧

iOS10の通知にアクションボタンを追加する方法 | Xamarin.Forms


前回の記事では通知に画像を表示する方法についてご紹介いたしました。引き続き
ローカル通知のカスタマイズについてご紹介いたします。今回はアクションボタンを追加する方法です。ユーザーに通知が届いた際に、ユーザーに動作を選択させる手法ですね。例えば、通知した内容に関連する「URLを開く」かどうかなど、選択肢を与えることはユーザービリティの向上につながりますね。


xamarin_ios_notification_reply_01.png xamarin_ios_notification_reply_03.png


前提条件
・Windows10 Pro 64Bit
・Visual Studio 2015 Community Update3
・Xamarin 4.3.0.795 (NuGet Xamarin.Forms 2.3.4.247)
・macOS Sierra 10.12.4 / Xcode8.3.1 / Xamarin.iOS 10.6.0.10



1.iOSでの実装方法

以前の記事でiOS10の通知の実装方法をご紹介しており、その続きになりますので、通知自体の実装方法は割愛します。ソースも一部省略していますが、以前の記事のソースと見比べながら補完してください。

NotificationService.cs
using UserNotifications;
[assembly: Dependency(typeof(NotificationService))]
namespace AppName.iOS.Services
{
    public class NotificationService : INotificationService
    {
        //アクションを取得する
        private UNNotificationCategory[] GetActionCategories()
        {
            // アクションボタン1を作成する
            var actionID1 = "reply";
            var actionTitle1 = "Reply";
            var actionOptions1 = UNNotificationActionOptions.Foreground;
            var action1 = UNNotificationAction.FromIdentifier(actionID1, actionTitle1, actionOptions1);

            // アクションボタン2を作成する
            var actionID2 = "noaction";
            var actionTitle2 = "No Action";
            var actionOptions2 = UNNotificationActionOptions.Destructive; //赤文字で表示する
            var action2 = UNNotificationAction.FromIdentifier(actionID2, actionTitle2, actionOptions2);

            // カテゴリを作成する
            var categoryID = "message";
            var actions = new UNNotificationAction[] { action1, action2 };
            var intentIDs = new string[] { };
            var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);

            // category配列を返す
            var categories = new UNNotificationCategory[] { category };
            return categories;
        }

        public void On(string title, string subTitle, string body)
        {
            UIApplication.SharedApplication.InvokeOnMainThread(delegate
            {
                if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
                {
                    //アクションを取得する
                    var categories = this.GetActionCategories();

                    //通知情報を設定する
                    var content = new UNMutableNotificationContent();
                    ・
                    ・//省略
                    ・
                    //アクションのカテゴリを紐づけ
                    content.CategoryIdentifier = "message";

                    //アクションの設定
                    UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
                    ・
                    ・//省略
                    ・
                    UNUserNotificationCenter.Current.Delegate = new LocalNotificationCenterDelegate();
                    // ローカル通知を予約する
                    UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
                    ・
                    ・//省略
                    ・
            });
        }
    }
}

これでボタンを表示する通知を送ることができます。ではボタンを押下した場合の動作について実装する必要がありますので次にご説明していきます。



2.アクションに対する動作を実装する

UNUserNotificationCenterDelegateを継承し、DidReceiveNotificationResponseをオーバーライドすると、通知時に設定したアクションのIDごとに分岐して動作を記述することができます。
あとはこのクラスをUNUserNotificationCenter.Current.Delegateに紐づけをします。(手順1にてソース記載済み)

namespace AppName.iOS.Services
{
    public class LocalNotificationCenterDelegate : UNUserNotificationCenterDelegate
    {
        //アクション
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {
            // Take action based on Action ID
            switch (response.ActionIdentifier)
            {
                case "reply":
                    //Replyボタンを押下した場合の動作を記述する
                    break;
                default:
                    // Take action based on identifier
                    if (response.IsDefaultAction)
                    {
                        // デフォルトアクションを記述する
                    }
                    else if (response.IsDismissAction)
                    {
                        // キャンセルした場合
                    }
                    break;
            }

            // Inform caller it has been handled
            completionHandler();
        }
    }
}

尚、シミュレータでは正しく動作しませんでした。実機のみアクションボタンが表示されました。




最後までお読みいただきありがとうございます。
当ブログの内容をまとめた Xamarin逆引きメニュー は以下のURLからご覧になれます。
https://itblog.dynaspo.com/blog-entry-81.html


関連記事

コメント

コメントの投稿

※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。

 入力されていないコメントには返信しませんのであらかじめご了承くださいませ。

※ニックネームでも良いので必ずご入力ください。

    

※必ずご入力ください。

    
    

※必ずご入力ください。

※技術的な質問には環境やエラーについて正確かつ詳細にお教えください。

・正確なエラーの内容

・Windowsのバージョン番号

・Visual Studioのバージョン

・機器の型番

・アプリやソフトのバージョン

    

カテゴリ別記事一覧

広告

プロフィール

石河 純


著者名 :石河 純
自己紹介:素人上がりのIT技術者。趣味は卓球・車・ボウリング

IT関連の知識はざっくりとこんな感じです。
【OS関連】
WindowsServer: 2012/2008R2/2003/2000/NT4
Windows: 10/8/7/XP/2000/me/NT4/98
Linux: CentOS RedHatLinux9
Mac: macOS Catalina 10.15 / Mojave 10.14 / High Sierra 10.13 / Sierra 10.12 / OSX Lion 10.7.5 / OSX Snow Leopard 10.6.8
【言語】
VB.net ASP.NET C#.net Java VBA
Xamarin.Forms
【データベース】
Oracle 10g/9i
SQLServer 2016/2008R2/2005/2000
SQLAnywhere 16/11/8
【BI/レポートツール】
Cognos ReportNet (IBM)
Microsoft PowerBI
ActiveReport (GrapeCity)
CrystalReport
【OCX関連】
GrapeCity InputMan SPREAD MultiRow GridView
【ネットワーク関連】
CCNP シスコ技術者認定
Cisco Catalyst シリーズ
Yamaha RTXシリーズ
FireWall関連
【WEB関連】
SEO SEM CSS jQuery IIS6/7 apache2

休みの日は卓球をやっています。
現在、卓球用品通販ショップは休業中です。