我想在我的应用中创建通知,这将在10秒内显示。它在应用程序运行时运行良好,但是当我关闭应用程序时,不会显示通知。这是我的代码:
我的通知服务:
ocamlopt -c tests.ml
类,它会启动通知:
[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);
感谢您的帮助。
答案 0 :(得分:0)
你可以试试这个。
<div class="form-group">
<label>Time Schedule:</label>
<div id="items">
<div class="col-md-4">
<input type="text" class="form-control" name="name" placeholder="12:00" data-timepicker>
</div>
<div class="col-md-2"><h5>TO</h5></div>
<div class="col-md-4">
<input type="text" class="form-control" name="name" placeholder="12:00" data-timepicker>
</div>
</div>
</div>
答案 1 :(得分:0)
当您销毁应用程序时,应该保持服务的活动。
return StartCommandResult.Sticky;
方法中添加OnStartCommand
。OnTaskRemoved
功能。使用Service
界面创建服务,IntentService
用于耗时的操作。
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);
}
}