2017-01-14 37 views
0

我想在我的應用程序中創建通知,該通知將在10秒內顯示。它運行良好,當應用程序正在運行時,但是當我關閉應用程序時,不會顯示通知。這裏是我的代碼:Android Xamarin - 已關閉的應用程序通知

我通知服務:

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

感謝您的幫助。

+0

添加codefor活動。 –

+0

活動代碼已結束 –

回答

0

你可以試試這個。

protected override void OnDestroy() 
     { 
     Notificator not = new Notificator(); 
     not.ShowNotification(this); 
      base.OnDestroy(); 
     } 
+0

它沒有幫助。 –

+0

你有沒有在你的活動中加入這個 –

+0

是的,但是它和以前一樣...當我在10秒內關閉應用程序時,沒有任何通知被觸發。 –

0

當你銷燬你的應用程序時,你應該保持你的服務活着。

  1. OnStartCommand方法中加上return StartCommandResult.Sticky;
  2. 啓動服務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); 
     } 
    } 

enter image description here

+0

我有華爲手機。當我不將應用程序手動移動到受保護的應用程序時,這是否正常工作? –

+0

@MikBig您可以在華爲手機上試用,我沒有華爲手機設備。我認爲它有效。 –

相關問題