Я использую Visual Studio 2019 16.6.0 Preview 2.1 и пытаюсь получить вибрацию и свет, чтобы работать на Galaxy S10 (Android 10). Я тестирую свое приложение. Я получаю уведомление, но телефон не вибрирует и свет не включается. Я также хотел бы иметь звук, но ничего из того, что я пробовал, не соответствует Visual Studio, поскольку все примеры / примеры, которые я могу найти, находятся в java. Мой код соответствует и работает без ошибок.
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
string NOTIFICATION_CHANNEL_ID = "com.OML.Android";
string channelName = "Notifications Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, Android.App.NotificationImportance.None);
chan.EnableLights(true);
chan.EnableVibration(true);
chan.LightColor = Color.Red;
//chan.SetSound() <<-- simply can't figure out how to construct a Uri to the default sound
chan.LockscreenVisibility = Android.App.NotificationVisibility.Private;
NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService);
manager.CreateNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.SetOngoing(true)
.SetSmallIcon(Resource.Drawable.alert_box)
.SetContentTitle("Alert System")
.SetPriority(1)
.SetAutoCancel(true)
.Build();
StartForeground(2, notification);
} else
{
Notification notify = new Notification();
notify.Defaults = NotificationDefaults.All;
StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notify);
}
Так чего мне не хватает?
* ОБНОВЛЕНИЕ *
За @Cherry Bu - MSFT's ответ Я изменил свой код на:
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
string NOTIFICATION_CHANNEL_ID = "com.OML.Android";
string channelName = "Notifications Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, Android.App.NotificationImportance.None);
chan.EnableLights(true);
chan.EnableVibration(true);
chan.LightColor = Color.Red;
chan.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification), new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build());
chan.LockscreenVisibility = Android.App.NotificationVisibility.Private;
NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService);
manager.CreateNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.SetOngoing(true)
.SetSmallIcon(Resource.Drawable.alert_box)
.SetContentTitle("Alert System")
.SetPriority(1)
//.SetVibrate(new long[] { 1000, 1000 })
//,SetLights(Color.AliceBlue, 3000, 3000)
.SetAutoCancel(true)
.Build();
StartForeground(2, notification);
} else
{
Notification notify = new Notification();
notify.Defaults = NotificationDefaults.All;
StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notify);
}
SingletonServicemanager.isMyServiceRunning = true;
return StartCommandResult.Sticky;
}
Я также добавил следующие разрешения в манифест:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
* Обновление *
Взглянул на проект github @Cherry Bu - MSFT опубликовал, и я считаю, что я делаю то же самое, что и он, но мой код не работает, поэтому в интересах решения этой проблемы я публикую код для моего сервиса переднего плана , Во-первых, код, который я использую для запуска службы, которая является частью моего Utils
кода:
public static void StartForegroundServiceComapt<SignalRSrv>(Context context, Bundle args = null) where SignalRSrv : Service
{
var intent = new Intent(context, typeof(SignalRSrv));
if (args != null)
{
intent.PutExtra("TheBundle", args);
}
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
context.StartForegroundService(intent);
}
else
{
context.StartService(intent);
}
}
Как видно из кода выше, я запускаю службу SignalR, этот код:
public class SignalRSrv : Service
{
private bool InstanceFieldsInitialized = false;
private string username = "";
private string firstname = "";
private string lastname = "";
private string company = "";
private string department = "";
private string section = "";
private int notifyid = 0;
public SignalRSingleton mInstance;
private void InitializeInstanceFields()
{
mBinder = new LocalBinder(this);
}
private Handler mHandler; // to display any received messages
private IBinder mBinder; // Binder given to clients
private Notification notification = null;
public SignalRSrv()
{
if (!InstanceFieldsInitialized)
{
InitializeInstanceFields();
InstanceFieldsInitialized = true;
}
}
public SignalRSingleton GetInstance()
{
return mInstance;
}
public override void OnCreate()
{
base.OnCreate();
mInstance = SignalRSingleton.getInstance();
mHandler = new Handler(Looper.MainLooper);
}
public override void OnDestroy()
{
try
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
{
StopForeground(StopForegroundFlags.Remove);
}
else
{
StopForeground(true);
}
StopSelf();
}
catch (System.Exception e)
{
var m = e.Message;
}
}
public override IBinder OnBind(Intent intent)
{
return null;
}
private void startSignalR(Bundle bundle)
{
User MyUser = bundle.GetParcelable("MyUser") as User;
if (MyUser.Department == null || MyUser.Department == "")
{
MyUser.Department = "none";
}
if (MyUser.Section == null || MyUser.Section == "")
{
MyUser.Section = "none";
}
mInstance.setmHubConnection(MyUser.Username, MyUser.Firstname ,MyUser.Lastname,MyUser.Company ,MyUser.Department,MyUser.Section, "FOREGROUND");
mInstance.setHubProxy();
try
{
// Connect the client to the hup
mInstance.mHubConnection.Start().Wait(-1);
mInstance.mHubProxy.On("broadcastMessage", (string platform, string message) =>
{
try
{
if (!isApplicationInTheBackground())
{
showMessage(message);
} else
{
showNotification(message, bundle, notification);
}
}
catch (System.Exception e)
{
var error = e.Message;
}
});
}
catch (System.Exception e) when (e is InterruptedException || e is ExecutionException)
{
//opps
//var x = 1;
return;
}
}
public async void showMessage(string message)
{
try
{
var result = await UserDialogs.Instance.ConfirmAsync(new ConfirmConfig
{
Message = "Alert - " + message,
OkText = "Ok",
});
if (result)
{
// do something
var x = message;
}
}
catch (System.Exception e)
{
var s = e.Message;
}
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
Bundle bundlee = intent.GetBundleExtra("TheBundle");
MyUser = bundlee.GetParcelable("MyUser") as User;
username = MyUser.Username;
firstname = MyUser.Firstname;
lastname = MyUser.Lastname;
company = intent.GetStringExtra("theSelectedCompany");
department = intent.GetStringExtra("theSelectedDepartment");
section = intent.GetStringExtra("theSelectedSection");
startSignalR(bundlee);
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
string NOTIFICATION_CHANNEL_ID = "com.OML.Android";
string channelName = "Notifications Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, Android.App.NotificationImportance.None);
chan.EnableLights(true);
chan.EnableVibration(true);
chan.LightColor = Color.Red;
chan.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification), new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build());
chan.LockscreenVisibility = Android.App.NotificationVisibility.Private;
NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService);
manager.CreateNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.SetOngoing(true)
.SetSmallIcon(Resource.Drawable.alert_box)
.SetContentTitle("Alert System")
.SetPriority(1)
//.SetVibrate(new long[] { 1000, 1000 })
//,SetLights(Color.AliceBlue, 3000, 3000)
.SetAutoCancel(true)
.Build();
StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
} else
{
Notification notify = new Notification();
notify.Defaults = NotificationDefaults.All;
StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notify);
}
SingletonServicemanager.isMyServiceRunning = true;
return StartCommandResult.Sticky;
}
private void cancelnotification()
{
NotificationManager notificationManager = (NotificationManager)Android.App.Application.Context.GetSystemService(Context.NotificationService);
notificationManager.Cancel(notifyid);
}
public void showNotification( string message, Bundle bundle, Notification notification)
{
try
{
Intent intent = new Intent();
User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
MyUser = bundle.GetParcelable("MyUser") as User;
if (MyUser.Usertype != "")
{
if (MyUser.Usertype == "COMPADMIN")
{
intent = new Intent(this, typeof(Drawer)); //Activity you want to open
} else if (MyUser.Usertype == "DEPTADMIN") {
intent = new Intent(this, typeof(Drawer)); //Activity you want to open
} else
{
intent = new Intent(this, typeof(regUser)); //Activity you want to open
}
}
else
{
intent = new Intent(this, typeof(regUser)); //Activity you want to open
}
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("TheBundle", bundle);
intent.PutExtra("MessageReceived", message);
var pendingIntent = PendingIntent.GetActivity(this, RandomGenerator(), intent, PendingIntentFlags.OneShot);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"com.OML.Android")
.SetSmallIcon(Resource.Drawable.alert_box)
.SetContentTitle("Message Received")
.SetContentText(message)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)Android.App.Application.Context.GetSystemService(Context.NotificationService);
notifyid = RandomGenerator();
notificationManager.Notify(notifyid, notificationBuilder.Build());
}
catch (System.Exception e)
{
Utils.LogError("An error occured in SignalRSrv.cs ShowNotification, the error is: " + e.Message);
}
}
private int RandomGenerator()
{
return new Random().Next(int.MinValue, int.MaxValue);
}
private bool isApplicationInTheBackground()
{
bool isInBackground;
RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
ActivityManager.GetMyMemoryState(myProcess);
isInBackground = myProcess.Importance != Android.App.Importance.Foreground;
return isInBackground;
}
}
public class LocalBinder : Binder
{
private readonly SignalRSrv outerInstance;
public LocalBinder(SignalRSrv outerInstance)
{
this.outerInstance = outerInstance;
}
public virtual SignalRSrv Service
{
get
{
// Return this instance of SignalRSrv so clients can call public methods
return outerInstance;
}
}
}
Это в коде SignalRSrv, где я создаю канал, это неправильно?