Android 实现本地通知
我是Xamarin的新手。我正在寻找一种方法来实现本地通知到我的交叉表单应用程序 以下是我从Xamarin网站获得的代码: 我只是想知道,当我按下一个按钮时,我是如何激活通知的 提前感谢。您正在寻找函数<代码>通知。操作必须由创建 基本示例:Android 实现本地通知,android,xamarin,xamarin.android,Android,Xamarin,Xamarin.android,我是Xamarin的新手。我正在寻找一种方法来实现本地通知到我的交叉表单应用程序 以下是我从Xamarin网站获得的代码: 我只是想知道,当我按下一个按钮时,我是如何激活通知的 提前感谢。您正在寻找函数通知。操作必须由创建 基本示例: var actionBuilder = new Notification.Action.Builder(Resource.Drawable.ic_notification_action, "Click me", someActionIntent); // Ins
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
。如果您正在寻找显示本地通知的统一方式,请使用James Montemagno的以下插件。在PCL和Android项目中安装插件。
对于
Xamarin.Forms
App,您需要完成以下任务:
ILocalNotificationService.cs
):
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);
}
}
}
private void Button_Clicked(object sender, EventArgs e)
{
DependencyService.Get<ILocalNotificationService>().ShowLocalNotification("My Title", "Hello World! This is my first notification!", "ic_notification");
}
private void按钮\u已单击(对象发送者,事件参数e)
{
DependencyService.Get().ShowLocalNotification(“我的标题”,“你好,世界!这是我的第一个通知!”,“ic_通知”);
}
注意:Xamarin调用依赖项服务类的默认构造函数,因此上下文不能通过构造函数传递,但Xamarin提供了
Forms.context
,它代表活动的当前上下文。这是Xamarin.Forms应用程序还是Xamarin.Android应用程序?复制代码,将其发布到按钮单击事件中。因此,单击该按钮,将显示通知。@Heshan是否为Xammarin.formsapp@ad1Diama谢谢,有没有办法让它用于跨表单应用程序?或者只是为了android@hugo抱歉,我不确定我是否尝试过实现您的解决方案,但没有发生任何事情。当我单击按钮时,没有收到通知。AML:@hugo是完整的演示代码,请尝试一下。我正在使用VS2017进行开发。如果仍然没有通知,请在其他设备上尝试。
public interface ILocalNotificationService
{
void ShowLocalNotification(string title, string text, string icon);
}
[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);
}
}
}
private void Button_Clicked(object sender, EventArgs e)
{
DependencyService.Get<ILocalNotificationService>().ShowLocalNotification("My Title", "Hello World! This is my first notification!", "ic_notification");
}