尝试使用 xamarin 显示通知



我正在按照本指南实现使用邻近信标的应用程序:

https://github.com/Estimote/Xamarin-Bindings

我从github下载了ExampleApp文件夹并安装了Estimote.Android.Proximity。它工作正常。

但这是我的问题:

//ExampleApps/Example.Android.Proximity/MainActivity.cs
class MyEnterHandler : Java.Lang.Object, Kotlin.Jvm.Functions.IFunction1
{
public Java.Lang.Object Invoke(Java.Lang.Object p0)
{
IProximityZoneContext context = (IProximityZoneContext)p0;
Log.Debug("app", $"MyEnterHandler, context = {context}");
return null;
}
}

而不是日志调试,我想创建我的通知。我该怎么做?

我尝试使用此脚本创建通知:

if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
// Android 8.0 and up require a channel for the notifications
var channel = new NotificationChannel(channelId, "Bluetooth activity", NotificationImportance.Low);
var notificationManager = this.GetSystemService(Context.NotificationService) as NotificationManager;
notificationManager.CreateNotificationChannel(channel);
}
var notification = new NotificationCompat.Builder(this, channelId)
.SetSmallIcon(global::Android.Resource.Drawable.IcDialogInfo)
.SetContentTitle("Proximity")
.SetContentText("Proximity demo is scanning for beacons")
.Build();

但是我对这个.getSystemService有问题(我没有"MyEnterHandler类"中的上下文(

  • 创建通知通道
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
return;
}
var channel = new NotificationChannel(CHANNEL_ID, Resources.GetString(Resource.String.app_name), NotificationImportance.Default)
{
Description = ""
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
  • 使用通知管理器发送通知
var pendingIntent = PendingIntent.GetActivity(this, MainActivity.NOTIFICATION_ID, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.outline_message_24)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManagerCompat.From(this);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());

相关内容

  • 没有找到相关文章

最新更新