Xamarin.Forms如何显示通知甚至应用程序已关闭

时间:2017-02-07 10:41:46

标签: android xamarin.forms portable-class-library

我正在使用 Xamarin.Fomrs PLC项目,我正在尝试显示通知甚至应用程序已关闭,现在它在应用程序运行时工作正常,因为我将通知线程放在{{ 1}},即使应用已关闭,我应该怎样做才能使通知正常工作

以下简要说明代码:

OnStart()

1 个答案:

答案 0 :(得分:1)

  

我正在使用Xamarin.Fomrs PLC项目,我试图显示通知甚至应用已关闭

您需要Register a start Service并在此启动服务中显示通知:

[Service(Exported =true,Name = "demo.winffee.MYSERVICE")]
[IntentFilter(new string[] {"demo.winffee.MYSERVICE"})]
public class MyService : Service
{
    public override IBinder OnBind(Intent intent)
    {
        return null;
    }

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        string msg=intent.GetStringExtra("msg");
        if (!String.IsNullOrEmpty(msg)) {
            Toast.MakeText(this, msg, ToastLength.Short).Show();
        }
        return base.OnStartCommand(intent, flags, startId);
    }

    public override void OnDestroy()
    {
        base.OnDestroy();
    }
}

注意:[Service(Exported =true,Name = "demo.winffee.MYSERVICE")]是将此服务从应用程序中调用的必要部分。

从其他应用程序调用该服务:

Intent intent = new Intent("demo.winffee.MYSERVICE");
intent.PutExtra("msg", "this is the text message from another application");
StartService(intent);