我是Xamarin的新手。我正在寻找一种方法来实现我的crossform应用程序的本地通知。
以下是我从Xamarin网站获得的代码:https://developer.xamarin.com/guides/android/application_fundamentals/notifications/local_notifications_in_android/
// 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_notification);
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager =
GetSystemService (Context.NotificationService) as NotificationManager;
// Publish the notification:
const int notificationId = 0;
notificationManager.Notify (notificationId, notification);
我只是想知道我怎么做,当我按下按钮时主动通知
先谢谢。
答案 0 :(得分:0)
您正在寻找AddAction(Notification.Action action)
功能。 Notification.Action
必须由Notification.Action.Builder
基本示例:
var actionBuilder = new Notification.Action.Builder(Resource.Drawable.ic_notification_action, "Click me", someActionIntent);
// 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_notification)
.AddAction(actionBuilder.Build());
请注意,您必须首先初始化someActionIntent
。
答案 1 :(得分:0)
如果您正在寻找显示本地通知的统一方式,请使用James Montemagno的以下插件。在PCL和Android项目中安装插件。 Local Notifications Plugin
private void MyButtonClick(object sender, EventArgs e)
{
CrossLocalNotifications.Current.Show("title", "body");
}
答案 2 :(得分:0)
对于Xamarin.Forms
应用,您需要DependencyService才能完成此任务:
在您的PCL中创建一个界面(ILocalNotificationService.cs
):
public interface ILocalNotificationService
{
void ShowLocalNotification(string title, string text, string icon);
}
在本地Xamarin.Android
项目中,创建一个类(LocalNotificationServiceImplementation.cs
)以实现ILocalNotificationService
并从Java.Lang.Object
继承,然后在顶部添加assembly:Dependency
属性命名空间:
[assembly:Dependency(typeof(LocalNotificationServiceImplementation))]
namespace LocalNotificationDemo.Droid
{
public class LocalNotificationServiceImplementation :Java.Lang.Object, ILocalNotificationService
{
public void ShowLocalNotification(string title, string text, string icon)
{
// Instantiate the builder and set notification elements:
Notification.Builder builder = new Notification.Builder(Forms.Context)
.SetContentTitle(title)
.SetContentText(text);
if (icon == "ic_notification")
{
builder.SetSmallIcon(Resource.Drawable.ic_notification);
}
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager =
Forms.Context.GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
}
}
}
在PCL的按钮点击事件处理程序中,调用依赖服务,如下所示:
private void Button_Clicked(object sender, EventArgs e)
{
DependencyService.Get<ILocalNotificationService>().ShowLocalNotification("My Title", "Hello World! This is my first notification!", "ic_notification");
}
注意:Xamarin调用Dependency Service类的默认构造函数,因此上下文不能通过构造函数传递,但xamarin提供Forms.Context
,它代表活动的当前上下文。