Xamarin.forms上的本地通知

时间:2017-04-04 09:23:44

标签: xamarin.forms

美好的一天, 我需要在我的xamarin.forms上构建本地通知,所以我从android开始: 1.在core项目中,我创建了一个界面:

 public interface INotification
    {
        void CreateNotification();
    }

2。在Droid我已实施通知:

[assembly: Xamarin.Forms.Dependency(typeof(Notification))]
namespace Test.Droid
{

    class Notification : Interfaces.INotification
    {
        public void CreateNotification()
        {
            string content = @"Here is the text";
            int messageId = 999;

            NotificationCompat.Builder builder = new NotificationCompat.Builder(Application.Context)
                .SetAutoCancel(true)
                .SetContentTitle("My Notifications")
                .SetSmallIcon(Resource.Drawable.icon)
                .SetContentText("Click here to next Activity")
                .SetStyle(new NotificationCompat.BigTextStyle().BigText(content))
                ;

            NotificationManager notificationManager = (NotificationManager)Application.Context.GetSystemService(Context.NotificationService);
            notificationManager.Notify(messageId, builder.Build());


        }
    }
}
  1. 最后,我将依赖服务放在核心项目的主页上

    public MainPage()
            {
                InitializeComponent();
                DependencyService.Get<INotification>().CreateNotification();
    

    }

  2. 但是我没有看到通知! 附:它在xamarin.android应用程序上正常工作。

1 个答案:

答案 0 :(得分:0)

错误在于依赖:

[assembly: Xamarin.Forms.Dependency(typeof(Test.Droid.Notification))]

<强>解决