首页 > 解决方案 > 本地通知实现

问题描述

我正在尝试使用 Xamarin.Forms 为我的 android 应用程序实现本地通知(带有点击事件和 HeadUP 通知)。我已经关注了本地通知的Microsoft 文档,但我不知道如何实现这一点。任何人都可以为此分享任何工作示例。

谢谢你。

标签: androidxamarin.formsxamarin.androidlocalnotification

解决方案


Android官方文档中,有两种设置重要性级别的方法。

在此处输入图像描述

第一个解决方案:(不工作)

如果通知的优先级标记为高、最大或全屏,则会收到提示通知。

可能触发平视通知的条件示例包括: 通知具有高优先级并使用铃声或振动。使用物理设备尝试如下:

builder.SetVibrate(new long[0]);
builder.SetPriority((int)NotificationPriority.High);

这是关于单挑通知的类似讨论。

不幸的是,上述解决方案不起作用。

第二种解决方案:(工作)

Xamarin Android 中的正确方法是设置Channel的属性。如果频道是由创建的NotificationImportance.High,则可以显示抬头。您可以参考这个官方示例。并修改您的CreateNotificationChannel方法如下:

void CreateNotificationChannel()
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            // Notification channels are new in API 26 (and not a part of the
            // support library). There is no need to create a notification 
            // channel on older versions of Android.
            return;
        }

        var name = Resources.GetString(Resource.String.channel_name);
        var description = GetString(Resource.String.channel_description);
        //Replace follow NotificationImportance.Default to NotificationImportance.High
        var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.High)
        {
            Description = description
        };



        var notificationManager = (NotificationManager) GetSystemService(NotificationService);
        notificationManager.CreateNotificationChannel(channel);
    }

在此处输入图像描述 在此处输入图像描述


推荐阅读