2017-02-17 4 views
0

Ich versuche, lokale Push-Benachrichtigung mit DependencyService für Xamarin Forms zu implementieren. Hier ist die Schnittstelle:Lokale Push-Benachrichtigung mit Xamarin Forms

public interface INotification 
    { 
     void SetNotification(DateTime notificationDate, TimeSpan notificationTime); 
    } 

Und hier ist die Umsetzung Andriod:

[assembly:Dependency(typeof(NotificationService))] 
namespace TestMVVM.Droid 
{ 
    public class NotificationService : INotification 
    { 
     public void SetNotification(DateTime notificationDate, TimeSpan notificationTime) 
     { 
      Notification.Builder builder = new Notification.Builder(this) 
       .SetContentTitle("Test Notification") 
       .SetContentText("Notification from Xamarin Forms"); 
     } 
    } 
} 

Es wird Fehler zeigt auf Notification.Builder builder = new Notification.Builder(this) als:

nicht von 'TestMVVM.Droid.NotificationService' umwandeln kann 'Andriod.Content.Context'

Ich verstehe das Problem. Context ermöglicht den Zugriff auf anwendungsspezifische Ressourcen und Klassen sowie Aufrufe für Vorgänge auf Anwendungsebene, z. B. Starten von Aktivitäten, Senden und Empfangen von Absichten usw. (siehe: https://developer.xamarin.com/api/type/Android.Content.Context/#Remarks). Da es sich um eine abstrakte Klasse handelt, kann ich kein Objekt erstellen und es an den Konstruktor Builder übergeben. Wie kann ich Kontext an den Builder-Konstruktor übergeben?

Edit: Danke für die Hilfe. Es funktioniert, aber die Benachrichtigung zeigt nicht an, dass nur der Ton funktioniert. hier der Code:

Notification.Builder builder = new Notification.Builder(Xamarin.Forms.Forms.Context) 
       .SetContentTitle("Test Notification") 
       .SetContentText("Notification from Xamarin Forms") 
       .SetDefaults(NotificationDefaults.Sound) 
       .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis()); 

      Notification notification = builder.Build(); 
      NotificationManager notificationManager = 
             Forms.Context.GetSystemService(Context.NotificationService) as NotificationManager; 
      const int notificationId = 0; 
      notificationManager.Notify(notificationId, notification); 

Antwort

3

Verwenden Xamarin.Forms.Forms.Context

[assembly:Dependency(typeof(NotificationService))] 
namespace TestMVVM.Droid 
{ 
    public class NotificationService : INotification 
    { 
     public void SetNotification(DateTime notificationDate, TimeSpan notificationTime) 
     { 
      Notification.Builder builder = new Notification.Builder (Xamarin.Forms.Forms.Context) 
       .SetContentTitle ("Test Notification") 
       .SetSmallIcon(Android.Resource.Drawable.AlertDarkFrame) 
       .SetWhen (Java.Lang.JavaSystem.CurrentTimeMillis()) 
       .SetDefaults (NotificationDefaults.Sound) 
       .SetContentText ("Notification from Xamarin Forms"); 
     } 
    } 
} 
+0

Danke für die Hilfe. Es klappt. Aber ich bin auf ein anderes Problem gestoßen. Die Benachrichtigung wird nicht angezeigt, nur der Ton funktioniert. Ich habe die Frage bearbeitet. Bitte schauen Sie – Vivek

+0

Nicht gute Idee Ändern Sie die ursprüngliche Frage, es war besser, Sie haben einen neuen Thread erstellt, aber wir sind bereits hier. Die Benachrichtigung wird nicht angezeigt, weil Sie kein Symbol festgelegt haben. Stellen Sie alles so ein, dass Sie es testen können. Verwenden Sie '.SetSmallIcon (Android.Resource.Drawable.AlertDarkFrame)' – apineda