2017-03-08 3 views
1

私はxamarin forms portableを使用してプロジェクトを開発中です。主な焦点は、iOSで動作する予定の通知を設定することです。私はxam.Plugins.Notifierや通知フレームワークのようないくつかのソリューションを試しましたが、まだシミュレータに表示されていないようです。それを要約すると、here's何I'veが行われ、xamarinチュートリアルに義務づける方針以下:iOS用xamarin forme pcl soluctionを使用して、iOSで通知を受け取るにはどうすればよいですか?

通知フレームワーク: https://developer.xamarin.com/guides/ios/platform_features/introduction-to-ios10/user-notifications/enhanced-user-notifications/

AppDelegate.cs UserNotificationsを使用して

//Request notification permissions from the user. 
     UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) => { 
      // Handle approval 
     }); 

ポータブル溶液 - MainPage.xaml.cs UserNotificationsを用い

 //iOS - Notification Framework (version 10 and above). 
     var content = new UNMutableNotificationContent(); 
     content.Title = "test"; 
     content.Subtitle = "Notification Subtitle"; 
     content.Body = "test 02"; 
     content.Badge = 1; 

     var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(5, false); 

     var requestID = "123"; 
     var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger); 

     UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => { 
      if (err != null) 
      { 
       // Do something with error... 
      } 
     }); 

はI'veはちょうど通知が火に得るためには、非常に簡単なテストを作成し、doesn't動作するようです。誰でも何が欠けているのか、あるいは他の解決策があるのか​​を知りましたか?

答えて

1

this plugin

に見てみるとそれはそれは、アクションを中断するユーザーであるため、iOS版で

は、あなたが最初にローカル通知を表示する許可を要求しなければならないと言います。

// Request Permissions 
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) 
{ 
    // Request Permissions 
    UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (granted, error) => 
    { 
     // Do something if needed 
    }); 
} 
else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) 
{ 
    var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
    UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null); 

    app.RegisterUserNotificationSettings(notificationSettings); 
} 

それは研究とプログラマーのフリーランスのヘルプのトンの多くの後

+0

これは私がappdelegate(最初のコード束)に書いたコードの最初の部分と似ていますが、それは正しいのですか?私はまだバージョンをチェックしていないと受け入れてください。とにかく、私はこれらの余分なパラメータを使って何か違いがあるかどうかを調べるつもりです。 –

2

あなたのために有用であるならば、私は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上のどこ見つけることができなかったとして、これは、誰かがお役に立てば幸いです。

関連する問題