コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
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) => {
・
・//省略
・
});
}
}
}
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();
}
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。