Xamarin本地通知不工作
问题描述:
我试图在我的应用程序中实现本地通知,但在一种情况下,我无法构建应用程序第二种情况我有错误。Xamarin本地通知不工作
我的代码。 接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GNote
{
public interface INotification
{
void LocalNotification();
}
}
Dependecy服务:
DependencyService.Get<INotification>().LocalNotification();
类NotificationService:
using Android.App;
using Android.Content;
namespace GNote.Droid
{
class NotificationService : INotification
{
public void LocalNotification()
{
// Instantiate the builder and set notification elements:
Notification.Builder builder = new Notification.Builder(this)
.SetContentTitle("Sample Notification")
.SetContentText("Hello World! This is my first notification!")
.SetSmallIcon(Resource.Drawable.ic_audiotrack_dark);
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager =
Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
}
}
}
我跟着xamarin文档导向Documentation 和在构造函数中使用此 Notification.Bui lder连续建设者=新Notification.Builder(本)
但在这种情况下,我有错误:
Argument 1: cannot convert from 'GNote.Droid.NotificationService' to 'Android.Content.Context'
我发现执行example 的其他例子,而不是这使用Application.Context 。 但后来我有
An unhandled exception occured. occurred
当尝试创建通知。
你能告诉我如何解决它吗?
谢谢。
答
我会尝试在
Notification.Builder builder = new Notification.Builder(this)
可以更改上下文参数值
Notification.Builder builder = new Notification.Builder(Android.App.Application.Context)