简体   繁体   English

在 PCL 项目 xamarin 上添加通知

[英]Adding Notifications On PCL project xamarin

I am trying to understand how notifications work on a pcl project.我试图了解通知如何在 pcl 项目上工作。 I followed this tutorial but it refers most on android applications, not so muck on a pcl project.我遵循了教程,但它主要指的是 android 应用程序,而不是 pcl 项目。 Can i implement notifications from the portable project?我可以实现来自便携式项目的通知吗? First i could use a button that pops a local notification.首先,我可以使用一个弹出本地通知的按钮。

For now i am interested in the android implementation more than the ios.现在我对 android 实现比 ios 更感兴趣。

There are plugins to display LocalNotification in XamarinForms PCL有插件可以在 XamarinForms PCL 中显示 LocalNotification

For example LocalNotificationsPlugin例如LocalNotificationsPlugin

API Usage接口使用

Call CrossLocalNotifications.Current from any project or PCL to gain access to APIs.从任何项目或 PCL 调用 CrossLocalNotifications.Current 以访问 API。

Display a local notification immediately立即显示本地通知

CrossLocalNotifications.Current.Show("title", "body");

Display a local notification at a scheduled date/time在预定的日期/时间显示本地通知

CrossLocalNotifications.Current.Show("title", "body", 101, DateTime.Now.AddSeconds(5));

Cancel a local notification取消本地通知

CrossLocalNotifications.Current.Cancel(101);

or或者

Toasts.Forms.Plugin Toasts.Forms.插件

Usage用法

Use dependency service in order to resolve IToastNotificator.使用依赖服务来解析 IToastNotificator。

var notificator = DependencyService.Get<IToastNotificator>();

var options = new NotificationOptions()
            {
                Title = "Title",
                Description = "Description"
            };

var result = await notificator.Notify(options);

The result that is returned is a NotificationResult with an Action inside with one of the following values.返回的结果是一个 NotificationResult,其中包含一个 Action,其中包含以下值之一。

[Flags]
public enum NotificationAction
{
    Timeout = 1, // Hides by itself
    Clicked = 2, // User clicked on notification
    Dismissed = 4, // User manually dismissed notification
    ApplicationHidden = 8, // Application went to background
    Failed = 16 // When failed to display the toast
}

If you want the Clicked NotificationAction you must set IsClickable = true in the NotificationOptions.如果您想要 Clicked NotificationAction,则必须在 NotificationOptions 中设置 IsClickable = true。

Another aritchie/notifications另一个阿奇奇/通知

Send a notification发送通知

await CrossNotifications.Current.Send("My Title", "My message for the notification");

Send a scheduled notification:发送预定通知:

await CrossNotifications.Current.Send("Happy Birthday", "I sent this a long time ago", when = TimeSpan.FromDays(50));

Get a list of scheduled notifications获取预定通知列表

var list = await CrossNotifications.Current.GetScheduledNotifications();

Cancel a specific notification取消特定通知

var id = await CrossNotifications.Current.Send("Hi", "This is my scheduled notification", when = TimeSpan.FromDays(1));
await CrossNotifications.Current.Cancel(id);

Cancel all scheduled notifications and clear badge:取消所有预定的通知并清除徽章:

CrossNotifications.Current.CancelAll();

To set a badge:要设置徽章:

Setting badges works on all platforms, though only select flavours of Android.设置徽章适用于所有平台,但仅适用于 Android 版本。 A 3rd party library is used to accomplish this.使用第 3 方库来完成此操作。

await CrossNotifications.Current.SetBadge(4);
await CrossNotifications.Current.GetBadge();

// 0 clears badge // 0 清除徽章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM