2016-05-05 4 views
1

私は単純な通知を送信しようとしています。私はそれの依存関係にするために、このラインを持っている私の実装の名前空間定義の前にXamarin NotificationManager.notify(..)メソッドでローカル通知を送信できないのはなぜですか

class PlatformNotifier :INotifier 
{ 

    public void LocalNotify(string title, string message) 
    { 
     NotificationManager notificationManager = Android.App.Application.Context.GetSystemService(Context.NotificationService) as NotificationManager; 
     Notification.Builder builder = new Notification.Builder(Android.App.Application.Context) 
      .SetContentTitle(title) 
      .SetContentText(message) 
      .SetAutoCancel(true); 

     Notification notification = builder.Build(); 
     notificationManager.Notify(1, notification); 
    } 
} 

:このような.droidプロジェクトのインターフェイスを実装する次に

public interface INotifier 
{ 
    void LocalNotify(string title, string message); 

} 

:私はこのシンプルなインターフェイスで出始めますサービス

[assembly: Dependency(typeof(PlatformNotifier))] 

は最後に、私は次のコード

でサービスを呼び出します

デバッグを介して私はプラットフォームノーティファイアのコードに到達し、Notify(...)が呼び出されることを確認しましたが、通知は表示されません。

+0

どのAndroidバージョンをターゲットにしていますか? –

+0

私はターゲットを絞っています5.1私は信じる – scuz888

+0

それは 'NotificationCompat.Builder'を必要としないようにしてください。あなたのアプローチが[Xamarinの推薦方法](https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/local_notifications_in_android/)のように思われるので、代わりに現在のアクティビティを試すことをお勧めしますコンテキストのためのアプリケーションの。 –

答えて

0

Notification.Builderに提供している文脈の可能性があります。アプリケーションの代わりに現在のアクティビティを試してください。

James Montemagno's CurrentActivity pluginを使用して、現在のアクティビティを取得できます。

Nugetを経由してあなたのXamarin.AndroidプロジェクトにPlugin.CurrentActivityをインストールし、その後、これを試してみてください。

... 

    Notification.Builder builder = new Notification.Builder(CrossCurrentActivity.Current.Activity)... 

... 
0

は動作しますが、そのコードを使用してください。)

をそのPlatform NotifierクラスのメソッドLocalNotifyで:

Notification.Builder builder = new Notification.Builder(Android.App.Application.Context) 
    .SetContentTitle(Title) 
    .SetContentText(ContentMessage) 
    .SetSmallIcon(Resource.Drawable.icon); 

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

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

    // Publish the notification: 
    const int notificationId = 0; 
    notificationManager.Notify(notificationId, notification); 
関連する問題