【问题标题】:Xamarin.Forms How to show a notification even app is closedXamarin.Forms 如何在应用程序关闭时显示通知
【发布时间】:2017-02-07 10:41:45
【问题描述】:

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

关于下面的代码要更清楚:

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 ();
     }

}

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多