Xamarin.Androidで通知を表示する

提供: MonoBook
2019年1月25日 (金) 07:25時点における180.15.192.154 (トーク)による版 (ページの作成:「== 準備 == NuGetから以下のパッケージを入れる * Xamarin.Android.Support.v4 == 実装 == 以下の名前空間をいれる。<syntaxhighlight lang="csharp…」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

準備

NuGetから以下のパッケージを入れる

  • Xamarin.Android.Support.v4

実装

以下の名前空間をいれる。

using Android.Content;
using Android.Support.V4.App;

必要最小限でかけばこんな感じ。

            // 通知を生成
            var message = "Hello, World!";
            
            var notification = new NotificationCompat.Builder(context)
                .SetSmallIcon(Android.Resource.Drawable.StarBigOn)
                .SetContentTitle("Boop!")
                .SetStyle(new NotificationCompat.BigTextStyle().BigText(message))
                .SetContentText(message)
                
                // 通知タップ時にインテントを発動させる場合
                //.SetContentIntent(pendingIntent)
                
                .builder.Build();
                
            // 通知マネージャーを取得
            var manager = (NotificationManager)this.GetSystemService(Context.NotificationService);

            // 通知を発射
            manager.Notify(1, notification);

関連項目