2017-02-07 130 views
0

我正在使用Xamarin.Fomrs PLC项目,我试图显示通知即使应用程序已关闭,现在它正在正常工作,因为应用程序正在运行,因为我穿上OnStart()通知线程,我应该怎么做才能让如果应用程序被关闭Xamarin.Forms如何显示通知即使应用程序已关闭

在下面的代码简要更清楚地通知甚至工作:

namespace X 
{ 
    public class App : Application 
    { 
     INotificationService service;   
    } 


    void CheckNotifications() 
    { 
     service = DependencyService.Get<INotificationService>(); 
     service.Notify (message); 
     System.Threading.Thread.Sleep (1000 * 60);   
    } 

    protected override void OnStart() 
    {   
      var threadStart = new System.Threading.ThreadStart (CheckNotifications); 
      var thread = new System.Threading.Thread (threadStart); 
      thread.IsBackground = true; 
      thread.Start(); 
    } 

} 

回答

0

我使用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); 
相关问题