Android Xamarin - 关闭应用通知 [英] Android Xamarin - Closed app notification

查看:80
本文介绍了Android Xamarin - 关闭应用通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中创建通知,该通知将在 10 秒后显示.它运行良好,当应用程序运行时,但当我关闭应用程序时,没有显示通知.这是我的代码:

I'd like to create notificiation in my app, which is going to be showed in 10 seconds. It works well, when application is running, but when I close the application, notification is not showed. Here is my code:

我的通知服务:

[Service]
class NotifyEvent : IntentService
{
    protected override void OnHandleIntent(Intent intent)
    {

        PendingIntent pIntent = PendingIntent.GetActivity(this, 0, intent, 0);

        Notification.Builder builder = new Notification.Builder(this);
        builder.SetContentTitle(Resources.GetString(Resource.String.NotifikaceNadpis));
        builder.SetContentText(Resources.GetString(Resource.String.NotifikaceText));
        builder.SetSmallIcon(Resource.Drawable.Icon);
        builder.SetPriority(1);
        builder.SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate);
        builder.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis());
        Notification notifikace = builder.Build();

        NotificationManager notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

        const int notificationId = 0;
        notificationManager.Notify(notificationId, notifikace);
    }
}

类,开始通知:

public class Notificator
{
    public void ShowNotification(Context context)
    {
        Intent intent = new Intent(context, typeof(NotifyEvent));
        var pendingServiceIntent = PendingIntent.GetService(context, 0, intent, PendingIntentFlags.UpdateCurrent);

        AlarmManager alarm = (AlarmManager)context.GetSystemService(Context.AlarmService);

        alarm.Set(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + 10000, pendingServiceIntent);
    }
}

活动中的方法:

Notificator not = new Notificator();
not.ShowNotification(this);

我的活动:

[Activity(Label = "Nastavení")]
public class SettingsActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here
        SetContentView(Resource.Layout.Settings);
        Button vynulovatButton = FindViewById<Button>(Resource.Id.buttonRestartDne);

        vynulovatButton.Click += VynulovatDen;

    }

    ...

    protected void VynulovatDen(object sender, EventArgs e)
    {
        Notificator not = new Notificator();
        not.ShowNotification(this);
    }
}

感谢大家的帮助.

推荐答案

当你销毁你的应用程序时,你应该让你的服务保持活动状态.

You should keep your service alive when you destroy your application.

  1. OnStartCommand方法中添加return StartCommandResult.Sticky;.
  2. 启动服务 OnTaskRemoved 函数.
  1. add return StartCommandResult.Sticky; in the OnStartCommand method.
  2. start the service OnTaskRemoved function.

使用Service接口创建你的服务,IntentService用于耗时操作.

Create your service with the Service interface, the IntentService is for Time-consuming operation.

 class NotifyEvent : Service
    {
        [return: GeneratedEnum]
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            new Task(() => {

                PendingIntent pIntent = PendingIntent.GetActivity(this, 0, intent, 0);
                Notification.Builder builder = new Notification.Builder(this);
                builder.SetContentTitle("hello");
                builder.SetContentText("hello");
                builder.SetSmallIcon(Resource.Drawable.Icon);
                builder.SetPriority(1);
                builder.SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate);
                builder.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis());
                Notification notifikace = builder.Build();
                NotificationManager notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
                const int notificationId = 0;
                notificationManager.Notify(notificationId, notifikace);

            }).Start();
            return StartCommandResult.Sticky;
        }

        public override IBinder OnBind(Intent intent)
        {
            return null;
        }
        public override void OnTaskRemoved(Intent rootIntent)
        {
            Intent restartService = new Intent(ApplicationContext, typeof(NotifyEvent));
            restartService.SetPackage(PackageName);
            var pendingServiceIntent = PendingIntent.GetService(ApplicationContext, 0, restartService, PendingIntentFlags.UpdateCurrent);
            AlarmManager alarm = (AlarmManager)ApplicationContext.GetSystemService(Context.AlarmService);
            alarm.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 1000, pendingServiceIntent);

            System.Console.WriteLine("service OnTaskRemoved");
            base.OnTaskRemoved(rootIntent);
        }
    }

这篇关于Android Xamarin - 关闭应用通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆