我正在尝试在我的应用中实现本地通知,但在一种情况下我无法构建应用程序第二种情况我有错误。
我的代码。 接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GNote
{
public interface INotification
{
void LocalNotification();
}
}
依赖服务:
DependencyService.Get<INotification>().LocalNotification();
Class 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 并在构造函数中使用 this Notification.Builder builder = new Notification.Builder(this)
但是在这种情况下我有错误:
参数1:无法从'GNote.Droid.NotificationService'转换为 'Android.Content.Context'
我找到了其他实施示例example 而不是这个使用 Application.Context 。 但后来我
发生未处理的异常。发生
尝试创建通知时。
你能告诉我怎么修理它?
谢谢。
答案 0 :(得分:0)
我会尝试更改
中的上下文参数值Notification.Builder builder = new Notification.Builder(this)
到
Notification.Builder builder = new Notification.Builder(Android.App.Application.Context)