2016-06-19 202 views
0

我期待在Xamarin Forms中使用C#創建基於文本的Android遊戲。當應用程序關閉時推送通知

在故事中,我想設置角色任務,這會花費他們一些時間,例如, 「我會去挖這個洞,當我完成後,我會給你一個嗡嗡聲。」

如何設置通知以在設置的時間之後出現?例如上述說法可能需要10分鐘,那麼用戶會收到通知以繼續遊戲?

我一個星期前纔開始C#,所以我很抱歉,如果這是不好的,或已經被問到。我到處尋找,但有幾種類型的通知,並且當我嘗試理解它時,似乎我正在讀法語。

回答

0

我將通過創建2個接口在PCL項目啓動:

public interface IAlarm { 
    void SetAlarm(); 
} 

public interface INotification { 
    void Notify(LocalNotification notification); 
} 

然後,在Android項目,創建實現:

報警助手

[assembly: Dependency(typeof(AlarmHelper))] 
namespace Test.Droid 
{ 
    class AlarmHelper: IAlarm 
    { 
     public void SetAlarm(int minutes) 
     { 
      var alarmTime = Calendar.Instance; 
      alarmTime.Add(CalendarField.Minute, minutes); 

      var intent = new Intent(Android.App.Application.Context, typeof(ScheduledAlarmHandler)); 
      var pendingIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, intent, PendingIntentFlags.CancelCurrent); 
      var alarmManager = Android.App.Application.Context.GetSystemService(Context.AlarmService) as AlarmManager; 

      alarmManager.Set(AlarmType.RtcWakeup, alarmTime.TimeInMillis, pendingIntent); 
     } 
    } 
} 

通知助手

[assembly: Dependency(typeof(NotificationHelper))] 
namespace Test.Droid 
{ 
    class NotificationHelper : INotification 
    { 
     public void Notify (string title, string text) 
     {    
      NotificationManager notificationManager = (NotificationManager) Android.App.Application.Context.GetSystemService(Context.NotificationService); 

      Intent intent = new Intent(Android.App.Application.Context, typeof(MainActivity)); 
      PendingIntent pIntent = PendingIntent.GetActivity(Android.App.Application.Context, 0, intent, PendingIntentFlags.OneShot); 

      Notification nativeNotification = new Notification(); 

      var builder = new NotificationCompat.Builder(Android.App.Application.Context) 
      .SetContentTitle(title) 
      .SetContentText(text) 
      .SetSmallIcon(Resource.Drawable.ic_notif) // 24x24 dp 
      .SetLargeIcon(BitmapFactory.DecodeResource(Android.App.Application.Context.Resources, Android.App.Application.Context.ApplicationInfo.Icon)) 
      .SetPriority((int)NotificationPriority.Default) 
      .SetAutoCancel(true) 
      .SetContentIntent(pIntent); 

      notificationManager.Notify(0, builder.Build()); // Id 0 can be random 
     } 
    } 
} 

當等待時間結束後,BroadCastReceiver將被稱爲:

alarmHelper = DependencyService.Get<IAlarm>(); 
alarmSetter.SetAlarm(10); // 10 minutes from now 

我:

[BroadcastReceiver] 
class ScheduledAlarmHandler : WakefulBroadcastReceiver 
{ 
    public override void OnReceive(Context context, Intent intent) 
    { 
     // Implement quick checking logic here if notification is still required, plus its tittle and text 
     // you have 10 seconds max in this method and cannot use 'await' 

     var notificationHelper = new NotificationHelper(); 
     notificationHelper.Notify("Title","Text"); 
    } 
} 

在PCL項目遊戲邏輯,你可以按照如下設置新鬧鈴有意分開了通知邏輯,以便在10分鐘後檢查是否仍應顯示通知並設置其標題和文本。另一種方法是在使用intent.putextra設置鬧鐘時傳遞標題和文本。

相關問題