あなたのために有用であるならば、私はthrought upwork.comを見つけたこと、知りませんが、我々は解決策を見つけることができましたそれのための。私は正しい道のりでしたが、私がxamarinとc#を初めて使ったので、私が欠けていた部分がありました。
正しく動作するためには、依存関係を構築する必要がありました。私が掲示しているコードは、自分のスタイルの整理と統合されています。しかし、誰もが自分のスタイルにこのコードを適応させることができます。
AppDelegate.csその後
using Foundation;
using UIKit;
using UserNotifications;
namespace MyApp_v1.iOS
{
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
//Locator.CurrentMutable.RegisterConstant(new IOSCookieStore(), typeof(IPlatformCookieStore)); //IPlatformCookieStore
global::Xamarin.Forms.Forms.Init();
//Notification framework.
//----------------------
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (approved, err) => {
// Handle approval
});
//Get current notification settings.
UNUserNotificationCenter.Current.GetNotificationSettings((settings) => {
var alertsAllowed = (settings.AlertSetting == UNNotificationSetting.Enabled);
});
UNUserNotificationCenter.Current.Delegate = new AppDelegates.UserNotificationCenterDelegate();
//----------------------
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
}
}
、我々は(私がAppDelegatesというフォルダに入れて)iOSの溶液にデリゲートを作成しました。
UserNotificationCenterDelegate.cs
using System;
using System.Collections.Generic;
using System.Text;
using UserNotifications;
namespace MyApp _v1.iOS.AppDelegates
{
public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
#region Constructors
public UserNotificationCenterDelegate()
{
}
#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
}
}
次に、我々は(私がAppDependenciesというフォルダに入れて)iOSのソリューションへの依存を作成しました。
LocalNotification
ILocalNotification.cs最後
namespace MyApp _v1.AppInterfaces
{
public interface ILocalNotification
{
//void ShowNotification(string strTitle, string strDescription, string idNotification, string strURL);
void ShowNotification(string strNotificationTitle,
string strNotificationSubtitle,
string strNotificationDescription,
string strNotificationIdItem,
string strDateOrInterval,
int intervalType,
string extraParameters);
}
}
:CS
using System;
using UserNotifications;
using MyApp _v1.AppInterfaces;
using MyApp _v1.iOS.AppDependencies;
using Foundation;
using static CoreText.CTFontFeatureAllTypographicFeatures;
[assembly: Xamarin.Forms.Dependency(typeof(LocalNotification))]
namespace MyApp _v1.iOS.AppDependencies
{
public class LocalNotification : ILocalNotification
{
public void ShowNotification(string strNotificationTitle,
string strNotificationSubtitle,
string strNotificationDescription,
string strNotificationIdItem,
string strDateOrInterval,
int intervalType,
string extraParameters)
{
//intervalType: 1 - set to date | 2 - set to interval
//Object creation.
var notificationContent = new UNMutableNotificationContent();
//Set parameters.
notificationContent.Title = strNotificationTitle;
notificationContent.Subtitle = strNotificationSubtitle;
notificationContent.Body = strNotificationDescription;
//notificationContent.Badge = 1;
notificationContent.Badge = Int32.Parse(strNotificationIdItem);
notificationContent.Sound = UNNotificationSound.Default;
//Set date.
DateTime notificationContentDate = Convert.ToDateTime(strDateOrInterval);
NSDateComponents notificationContentNSCDate = new NSDateComponents();
notificationContentNSCDate.Year = notificationContentDate.Year;
notificationContentNSCDate.Month = notificationContentDate.Month;
notificationContentNSCDate.Day = notificationContentDate.Day;
notificationContentNSCDate.Hour = notificationContentDate.Hour;
notificationContentNSCDate.Minute = notificationContentDate.Minute;
notificationContentNSCDate.Second = notificationContentDate.Second;
notificationContentNSCDate.Nanosecond = (notificationContentDate.Millisecond * 1000000);
//Set trigger and request.
var notificationRequestID = strNotificationIdItem;
UNNotificationRequest notificationRequest = null;
if (intervalType == 1)
{
var notificationCalenderTrigger = UNCalendarNotificationTrigger.CreateTrigger(notificationContentNSCDate, false);
notificationRequest = UNNotificationRequest.FromIdentifier(notificationRequestID, notificationContent, notificationCalenderTrigger);
}
else
{
var notificationIntervalTrigger = UNTimeIntervalNotificationTrigger.CreateTrigger(Int32.Parse(strDateOrInterval), false);
notificationRequest = UNNotificationRequest.FromIdentifier(notificationRequestID, notificationContent, notificationIntervalTrigger);
}
//Add the notification request.
UNUserNotificationCenter.Current.AddNotificationRequest(notificationRequest, (err) =>
{
if (err != null)
{
System.Diagnostics.Debug.WriteLine("Error : " + err);
}
});
}
}
}
次に、我々は、ポータブルソリューション(私はAppInterfacesというフォルダに入れて)で、インタフェースを作成しました、ポータブルソリューションMainPageで、私は通知を作成するメソッドを呼び出します:
MainPage.xaml.cs
//var notificationData = (DateTime)strNotificationDate.to;
DateTime dateAlarmNotificationSchedule = Convert.ToDateTime(2017-28-02 08:30:00);
//Alarm set.
//iOS - Notification Framework (version 10 and above).
//DependencyService.Get<ILocalNotification>().ShowNotification(strNotificationTitle, strNotificationDescription, strNotificationIdItem, strNotificationURL);
DependencyService.Get<ILocalNotification>().ShowNotification("title example",
"subtitle example",
"description example",
"123",
strAlarmNotificationSchedule,
1,
"");
私は、Web上のどこ見つけることができなかったとして、これは、誰かがお役に立てば幸いです。
これは私がappdelegate(最初のコード束)に書いたコードの最初の部分と似ていますが、それは正しいのですか?私はまだバージョンをチェックしていないと受け入れてください。とにかく、私はこれらの余分なパラメータを使って何か違いがあるかどうかを調べるつもりです。 –