Beispiel #1
0
        public void On(string title, string subTitle, string body)
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                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; // アイコン上に表示するバッジの数値
                });
            }
            else    //iOS9まで向け
            {
                UIApplication.SharedApplication.InvokeOnMainThread(delegate {
                    _notification = new UILocalNotification();
                    _notification.Init();
                    _notification.FireDate = NSDate.FromTimeIntervalSinceNow(10); //メッセージを通知する日時
                    _notification.TimeZone = NSTimeZone.DefaultTimeZone;
                    //_notification.RepeatInterval = NSCalendarUnit.Day; // 日々繰り返しする場合
                    _notification.AlertTitle  = title;
                    _notification.AlertBody   = body;
                    _notification.AlertAction = @"Open"; //ダイアログで表示されたときのボタンの文言
                    _notification.UserInfo    = NSDictionary.FromObjectAndKey(new NSString("NotificationValue"), new NSString("NotificationKey"));
                    _notification.SoundName   = UILocalNotification.DefaultSoundName;
                    // アイコン上に表示するバッジの数値
                    UIApplication.SharedApplication.ApplicationIconBadgeNumber += 1;
                    //通知を登録
                    UIApplication.SharedApplication.ScheduleLocalNotification(_notification);
                });
            }
        }