2017-07-24 12 views
1

Ich versuche, lokale Benachrichtigung in meiner App zu implementieren, aber in einem Fall kann ich keine App einen zweiten Fall erstellen, ich habe einen Fehler.Xamarin lokale Benachrichtigung funktioniert nicht

Mein Code. Schnittstelle:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace GNote 
{ 
    public interface INotification 
    { 
     void LocalNotification(); 
    } 
} 

Dependecy Service:

DependencyService.Get<INotification>().LocalNotification(); 

Klasse NotificationService:

using Android.App; 
using Android.Content; 

namespace GNote.Droid 
{ 
    class NotificationService : INotification 
    { 
     public void LocalNotification() 
     { 
      // Instantiate the builder and set notification elements: 
      Notification.Builder builder = new Notification.Builder(this) 
       .SetContentTitle("Sample Notification") 
       .SetContentText("Hello World! This is my first notification!") 
       .SetSmallIcon(Resource.Drawable.ic_audiotrack_dark); 

      // Build the notification: 
      Notification notification = builder.Build(); 

      // Get the notification manager: 
      NotificationManager notificationManager = 
      Application.Context.GetSystemService(Context.NotificationService) as NotificationManager; 

      // Publish the notification: 
      const int notificationId = 0; 
      notificationManager.Notify(notificationId, notification); 

     } 
    } 
} 

I Xamarin Dokumentation Leitfaden Documentation und in Konstruktor verwendet, gefolgt dieses Notification.Bui lder builder = new Notification.Builder (this)

Aber in diesem Fall habe ich Fehler:

Argument 1: cannot convert from 'GNote.Droid.NotificationService' to 'Android.Content.Context'

ich ein weiteres Beispiel für die Umsetzung example und statt diese Verwendung Application.Context gefunden . Aber dann habe ich

An unhandled exception occured. occurred

wenn versuchen Benachrichtigung erstellen.

Können Sie mir sagen, wie es zu beheben?

Vielen Dank.

Antwort

0

Ich würde versuchen, den Kontext Parameterwert in

Notification.Builder builder = new Notification.Builder(this) 

zu

Notification.Builder builder = new Notification.Builder(Android.App.Application.Context) 
zu ändern
Verwandte Themen