void RegisterForegroundService()
        {
            NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);

            //Andorid8.0 Oreo 以降の通知で必要なChannel
            string channelId = "Covid19RadarNotification";

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                string channelNm            = "Covid19Radar";
                NotificationChannel channel = new NotificationChannel(channelId, channelNm, NotificationImportance.Default)
                {
                    Description = "任意の説明"
                };
                manager.CreateNotificationChannel(channel);
            }
            var notification = new Notification.Builder(this)
                               .SetContentTitle(Resources.GetString(Resource.String.app_name))
                               .SetContentText("Started ForegroundService.")
                               .SetSmallIcon(Resource.Drawable.notification_icon_background)                                                                            //Android 7
                               .SetColor(ActivityCompat.GetColor(Android.App.Application.Context, Resource.Color.notification_material_background_media_default_color)) //Android7.0対応
                               .SetOngoing(true)
                               .SetChannelId(channelId)                                                                                                                 // Android8
                               .Build();

            // Enlist this instance of the service as a foreground service
            this.StartForeground(2451, notification);
        }
Пример #2
0
        public override void OnReceive(Context context, Intent intent)
        {
            var message = intent.GetStringExtra("message");
            var title   = intent.GetStringExtra("title");

            var notIntent               = new Intent(context, typeof(MainActivity));
            var contentIntent           = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.CancelCurrent);
            NotificationManager manager = (NotificationManager)context.GetSystemService(Context.NotificationService);

            var style = new NotificationCompat.BigTextStyle();

            style.BigText(message);

            Android.Net.Uri uri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);

            var builder = new NotificationCompat.Builder(context, plan_main.CHANNEL_ID)
                          .SetContentIntent(contentIntent)
                          .SetSmallIcon(Resource.Drawable.Icon)
                          .SetContentTitle(title)
                          .SetContentText(message)
                          .SetStyle(style)
                          .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
                          .SetSound(uri)
                          .SetColor(ActivityCompat.GetColor(context, Resource.Color.colorPrimaryDark));

            var notification = builder.Build();

            manager.Notify(0, notification);
        }
Пример #3
0
        private Notification CreateNotification()
        {
            #region Create Channel
            string channelId          = "foreground";
            string channelName        = "foreground";
            string channelDescription = "The foreground channel for notifications";
            int    _pendingIntentId   = 1;

            NotificationManager _notificationManager;
            _notificationManager = (NotificationManager)Android.App.Application.Context.GetSystemService(Android.App.Application.NotificationService);
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                var channelNameJava = new Java.Lang.String(channelName);
                var channel         = new NotificationChannel(channelId, channelNameJava, NotificationImportance.Low)
                {
                    Description = channelDescription,
                };
                channel.EnableVibration(false);
                _notificationManager.CreateNotificationChannel(channel);
            }
            #endregion

            #region Create Notification
            Intent foregroundNotificationIntent = new Intent(Android.App.Application.Context, typeof(MainActivity));

            PendingIntent pendingIntent = PendingIntent.GetActivity(Android.App.Application.Context, _pendingIntentId, foregroundNotificationIntent, PendingIntentFlags.OneShot);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(Android.App.Application.Context, channelId)
                                                 .SetContentIntent(pendingIntent)
                                                 .SetContentTitle("ForegroundServiceApp")
                                                 .SetContentText("Foreground service started")
                                                 .SetOngoing(true)
                                                 .SetColor(ActivityCompat.GetColor(Android.App.Application.Context, Resource.Color.colorAccent))
                                                 .SetLargeIcon(BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, Resource.Drawable.xamagonBlue))
                                                 .SetSmallIcon(Resource.Drawable.xamagonBlue);

            var notification = builder.Build();
            #endregion

            return(notification);
        }