コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
//DependencyServiceから利用する
public interface INotificationService
{
//iOS用の登録
void Regist();
//通知する
void On(string title, string subTitle, string body);
//通知を解除する
void Off();
}
using System;
using Foundation;
using UIKit;
using Xamarin.Forms;
[assembly: Dependency(typeof(NotificationService))]
public class NotificationService : INotificationService
{
public void Regist()
{
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// 許可をもらう通知タイプの種類を定義
UNAuthorizationOptions types = UNAuthorizationOptions.Badge | // アイコンバッチ
UNAuthorizationOptions.Sound | // サウンド
UNAuthorizationOptions.Alert; // テキスト
UNUserNotificationCenter.Current.RequestAuthorization(types, (granted, err) => {
// Handle approval
if (err != null)
{
System.Diagnostics.Debug.WriteLine(err.LocalizedFailureReason + System.Environment.NewLine + err.LocalizedDescription);
}
if (granted)
{
}
});
}
}
public void On(string title, string subTitle, string body)
{
UIApplication.SharedApplication.InvokeOnMainThread(delegate
{
var content = new UNMutableNotificationContent();
content.Title = title;
content.Subtitle = title;
content.Body = body;
content.Sound = UNNotificationSound.Default;
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(5, false); //5秒後に通知、リピートしない
//日時を指定する場合は以下の方法
//NSDateComponents components = new NSDateComponents();
//components.TimeZone = NSTimeZone.DefaultTimeZone;
//components.Year = _notifyDate.LocalDateTime.Year;
//components.Month = _notifyDate.LocalDateTime.Month;
//components.Day = _notifyDate.LocalDateTime.Day;
//components.Hour = _notifyDate.LocalDateTime.Hour;
//components.Minute = _notifyDate.LocalDateTime.Minute;
//components.Second = _notifyDate.LocalDateTime.Second;
//var calendarTrigger = UNCalendarNotificationTrigger.CreateTrigger(components, false);
var requestID = "notifyKey";
content.UserInfo = NSDictionary.FromObjectAndKey(new NSString("notifyValue"), new NSString("notifyKey"));
var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
UNUserNotificationCenter.Current.Delegate = new LocalNotificationCenterDelegate();
// ローカル通知を予約する
UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
if (err != null)
{
// Do something with error...
LogUtility.OutPutError(err.LocalizedFailureReason + System.Environment.NewLine + err.LocalizedDescription);
}
});
UIApplication.SharedApplication.ApplicationIconBadgeNumber += 1; // アイコン上に表示するバッジの数値
});
}
public void Off()
{
UIApplication.SharedApplication.InvokeOnMainThread(delegate
{
//全ての送信待ちの通知を削除する場合
//UNUserNotificationCenter.Current.RemoveAllDeliveredNotifications();
//通知時に設定したキーを元に通知情報をキャンセル
UNUserNotificationCenter.Current.RemovePendingNotificationRequests(new string[] { "notifyKey" });
});
}
}
//PCLのXaml.csファイルのロード時等に記述します。
public ContentPage()
{
//通知設定をiOSに登録
DependencyService.Get<INotificationService>().Regist();
}
//PCLのXaml.csファイルの任意の場所で記述します。
void OnNotificationClick(object sender, EventArgs e)
{
//通知を実行
DependencyService.Get<INotificationService>().On("タイトルテスト", "サブタイトル", "本文テスト");
}
public class LocalNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
#region Constructors
public LocalNotificationCenterDelegate()
{
}
#endregion
#region Override Methods
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
// Do something with the notification
Console.WriteLine("Active Notification: {0}", notification);
// Tell system to display the notification anyway or use
// `None` to say we have handled the display locally.
completionHandler(UNNotificationPresentationOptions.Alert);
}
#endregion
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。