【发布时间】:2023-03-27 03:54:01
【问题描述】:
我的 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;
}
}
}
}
代码编译没有错误,我在代码中设置断点以遵循流程,一切似乎都在正常执行,但没有出现任何通知。我错过了什么?
【问题讨论】:
-
我看不到您在哪里创建服务连接?根据您的描述,您使用绑定服务发送通知,这里是绑定服务,您可以看看创建服务连接:docs.microsoft.com/en-us/xamarin/android/app-fundamentals/…
标签: c# xamarin.android visual-studio-2017