为什么不';无法使用Xamarin NotificationManager.notify(..)方法发送本地通知

为什么不';无法使用Xamarin NotificationManager.notify(..)方法发送本地通知,xamarin,notifications,xamarin.android,Xamarin,Notifications,Xamarin.android,我只是想发送一个简单的通知。我从这个简单的界面开始: 公共接口INotifier { void LocalNotify(字符串标题、字符串消息); } 然后在.droid项目中实现如下接口: 类平台通知程序:INotifier { public void LocalNotify(字符串标题、字符串消息) { NotificationManager NotificationManager=Android.App.Application.Context.GetSystemService(Conte

我只是想发送一个简单的通知。我从这个简单的界面开始:

公共接口INotifier
{
void LocalNotify(字符串标题、字符串消息);
}
然后在.droid项目中实现如下接口:

类平台通知程序:INotifier
{
public void LocalNotify(字符串标题、字符串消息)
{
NotificationManager NotificationManager=Android.App.Application.Context.GetSystemService(Context.NotificationService)作为NotificationManager;
Notification.Builder=new Notification.Builder(Android.App.Application.Context)
.SetContentTitle(标题)
.SetContentText(消息)
.SetAutoCancel(真);
通知通知=builder.Build();
通知经理。通知(1,通知);
}
}
在我的实现的名称空间定义之前,我有一行代码使它成为依赖项服务

[assembly: Dependency(typeof(PlatformNotifier))]
最后,我使用以下代码调用该服务

private void DoneButton_单击(对象发送方,事件参数e)
{
INotifier通知程序=DependencyService.Get();
LocalNotify(“测试”,“这是一个通知”);
}

通过调试,我已经确认平台通知程序中的代码确实被访问,并且
Notify(…)
被调用,但是没有显示任何通知

它可能是您提供给
通知.Builder
的上下文。尝试当前活动而不是应用程序

您可以使用获取当前活动

通过Nuget将Plugin.CurrentActivity安装到Xamarin.Android项目中,然后尝试以下操作:

。。。
Notification.Builder=new Notification.Builder(CrossCurrentActivity.Current.Activity)。。。
...

它可能是您提供给
通知.Builder的上下文。尝试当前活动而不是应用程序

您可以使用获取当前活动

通过Nuget将Plugin.CurrentActivity安装到Xamarin.Android项目中,然后尝试以下操作:

。。。
Notification.Builder=new Notification.Builder(CrossCurrentActivity.Current.Activity)。。。
...
使用该代码,将起作用;)

在方法
LocalNotify
平台通知程序
类中:

Notification.Builder builder = new Notification.Builder(Android.App.Application.Context)
    .SetContentTitle(Title)
    .SetContentText(ContentMessage)
    .SetSmallIcon(Resource.Drawable.icon);

    // Build the notification:
    Notification notification = builder.Build();

    // Get the notification manager:
    NotificationManager notificationManager = Android.App.Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;

    // Publish the notification:
    const int notificationId = 0;
    notificationManager.Notify(notificationId, notification);
使用该代码,将起作用;)

在方法
LocalNotify
平台通知程序
类中:

Notification.Builder builder = new Notification.Builder(Android.App.Application.Context)
    .SetContentTitle(Title)
    .SetContentText(ContentMessage)
    .SetSmallIcon(Resource.Drawable.icon);

    // Build the notification:
    Notification notification = builder.Build();

    // Get the notification manager:
    NotificationManager notificationManager = Android.App.Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;

    // Publish the notification:
    const int notificationId = 0;
    notificationManager.Notify(notificationId, notification);

你的目标Android版本是什么?我的目标是5.1,我相信这可以排除需要
NotificationCompat.Builder
。似乎你的方法是正确的,所以我建议尝试当前的活动,而不是上下文应用程序。如何获得当前的活动?是否将上下文传递给LocalNotify方法?DoneButton_Clicked方法位于基本项目的*.xaml.cs类中。请看我发布的答案。它有指向提供当前活动的CrossCurrentActivity插件的链接。您的目标是哪一个Android版本?我的目标是5.1我相信可以排除需要
NotificationCompat.Builder
。似乎你的方法是正确的,所以我建议尝试当前的活动,而不是上下文应用程序。如何获得当前的活动?是否将上下文传递给LocalNotify方法?DoneButton_Clicked方法位于基本项目的*.xaml.cs类中。请看我发布的答案。它有指向提供当前活动的CrossCurrentActivity插件的链接。