У меня работает служба signalR, и теперь мне нужно отправить уведомление пользователю при получении сообщения.Я написал код уведомления, который выполняется, но уведомление не отображается.Мой сервисный код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Acr.UserDialogs;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;
using Java.Lang;
using Java.Util.Concurrent;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Transports;
namespace MyAndroid
{
[Service]
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 void InitializeInstanceFields()
{
mBinder = new LocalBinder(this);
}
private Handler mHandler; // to display any received messages
private IBinder mBinder; // Binder given to clients
private SignalRSingleton mInstance;
private Notification notification = null;
public SignalRSrv()
{
if (!InstanceFieldsInitialized)
{
InitializeInstanceFields();
InstanceFieldsInitialized = true;
}
}
public override void OnCreate()
{
base.OnCreate();
mInstance = SignalRSingleton.getInstance();
mHandler = new Handler(Looper.MainLooper);
notification = RegisterForegroundService(); // here we set up the notification and start in foreground service
}
public override void OnDestroy()
{
base.OnDestroy();
}
public override IBinder OnBind(Intent intent)
{
//binder = new LocalBinder(this);
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);
return mBinder;
}
private void startSignalR(Bundle bundle)
{
mInstance.setmHubConnection(username, firstname,lastname,company,department,section);
mInstance.setHubProxy();
try
{
// Connect the client to the hup
mInstance.mHubConnection.Start();
mInstance.mHubProxy.On("broadcastMessage", (string platform, string message) =>
{
try
{
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 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);
return StartCommandResult.Sticky;
}
Notification RegisterForegroundService()
{
var notification = new NotificationCompat.Builder(this)
.SetContentTitle("League Alert")
.SetContentText(Resources.GetString(Resource.String.notification_text))
.SetSmallIcon(Resource.Drawable.alert_box)
// Enlist this instance of the service as a foreground service
StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
return notification;
}
public void showNotification(string message, Bundle bundle, Notification notification)
{
int count = 1;
try
{
Intent resultIntent = new Intent(this, typeof(Drawer));
// Pass some values to SecondActivity:
resultIntent.PutExtra("TheBundle", bundle);
// Construct a back stack for cross-task navigation:
Android.App.TaskStackBuilder stackBuilder = Android.App.TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(Drawer)));
stackBuilder.AddNextIntent(resultIntent);
// Create the PendingIntent with the back stack:
PendingIntent resultPendingIntent =
stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
NotificationManager notificationManager =
(NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
}
catch (System.Exception e)
{
var error = e.Message;
}
}
private string CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return "";
}
var channelName = "My Messenger";
var channelDescription = "My Messenger Channel"; // GetString(Resource.String.channel_description);
var channel = new NotificationChannel("0", channelName, NotificationImportance.Default)
{
Description = channelDescription
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
return channelName;
}
}
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;
}
}
}
}
Код компилируется без ошибок, и я установил точки останова в коде, чтобы следовать потоку, и все, кажется, выполняется правильно, но уведомление не появляется.Чего мне не хватает?