2017-08-30 10 views
0

ich einen Hintergrunddienst in android habe implementiert als:Hintergrunddienst in Xamarin Forms

[Service] 
public class PeriodicService : Service 
{ 
    public override IBinder OnBind(Intent intent) 
    { 
     return null; 
    } 

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) 
    { 
     base.OnStartCommand(intent, flags, startId); 

     // From shared code or in your PCL] 
     Task.Run(() => { 
      MessagingCenter.Send<string>(this.Class.Name, "SendNoti"); 
     }); 

     return StartCommandResult.Sticky; 
    } 

} 

In MainActivity Klasse:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity 
    { 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     global::Xamarin.Forms.Forms.Init(this, bundle); 
     UserDialogs.Init(() => (Activity)Forms.Context); 
     LoadApplication(new App()); 

     StartService(new Intent(this, typeof(PeriodicService))); 
    } 
} 

In Xamarin Forms in Meiner Login-Seite:

public LoginPage() 
    { 
     InitializeComponent(); 

     int i = 0; 
     MessagingCenter.Subscribe<string>(this, "SendNoti", (e) => 
     { 
      Device.BeginInvokeOnMainThread(() => 
      { 
       i++; 

       CrossLocalNotifications.Current.Show("Some Text", "This is notification!");       

       } 
      }); 
     }); 

    } 

Das Hauptproblem hier ist mein periodischer Dienst sendet keine Nachricht außer zum ersten Mal. Die Benachrichtigung wird nur einmal angezeigt! Bitte helfen Sie.

+1

Sie rufen nur 'MessagingCenter.Send' ** einmal ** in Ihrem Service auf ... – SushiHangover

+0

@SushiHangover Vielen Dank für Ihre Antwort. Wie kann ich diese Benachrichtigung alle n Stunden senden? – Subash

+1

Die Verwendung eines sich wiederholenden Alarms über AlarmManager/SetRepeating ist eine viel bessere Möglichkeit, wiederkehrende Ereignisse zu planen, siehe meine Antwort hier: https://stackoverflow.com/a/45657600/4984832 – SushiHangover

Antwort

2

Neues IntentService Ihre Benachrichtigungen senden:

[Service(Label = "NotificationIntentService")] 
public class NotificationIntentService : IntentService 
{ 
    protected override void OnHandleIntent(Intent intent) 
    { 
     var notification = new Notification.Builder(this) 
          .SetSmallIcon(Android.Resource.Drawable.IcDialogInfo) 
          .SetContentTitle("StackOverflow") 
          .SetContentText("Some text.......") 
          .Build(); 
     ((NotificationManager)GetSystemService(NotificationService)).Notify((new Random()).Next(), notification); 
    } 
} 

dann die AlarmManager Setup verwenden, um eine Wiederholung Alarm eine anhängige Absicht mit, dass "ruft" Ihre IntentService:

using (var manager = (Android.App.AlarmManager)GetSystemService(AlarmService)) 
{ 
    // Send a Notification in ~60 seconds and then every ~90 seconds after that.... 
    var alarmIntent = new Intent(this, typeof(NotificationIntentService)); 
    var pendingIntent = PendingIntent.GetService(this, 0, alarmIntent, PendingIntentFlags.CancelCurrent); 
    manager.SetInexactRepeating(AlarmType.RtcWakeup, 1000 * 60, 1000 * 90, pendingIntent); 
} 
+0

Ich bin mir sicher, dass das funktioniert, aber bitte ein wenig mehr Hilfe, wie setze ich jeden Tag um 10 Uhr, 14 Uhr und 17 Uhr? Auch wenn das Gerät zu diesem Zeitpunkt ausgeschaltet war, muss ich später eine Benachrichtigung senden. – Subash

+0

Danke, das habe ich als Antwort markiert und auch die Lösung zu meiner Frage gefunden :) – Subash