2017-01-14 26 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); 

My活動:

[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. return StartCommandResult.Sticky;OnStartCommand方法で加える。
  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

私はHuawei電話を持っています。私のアプリをProtectedアプリに手動で移動しないと、これは動作しますか? –

+0

@MikBigあなたはHuaweiの電話でそれを試すことができます、私はHuaweiの電話デバイスを持っていません。私はそれが動作すると思います。 –

関連する問題