2016-06-19 22 views
0

XamarinフォームでC#を使用してテキストベースのAndroidゲームを作成しようとしています。アプリが終了したときに通知をプッシュ

ストーリーでは、キャラクタータスクを設定したいと思います。 "私はこの穴を掘るつもりだ、私はあなたが終わったときにバズを与えるだろう"

設定時間後に通知を設定するにはどうすればよいですか?たとえば、上記の文に10分かかる場合、ユーザーはゲームを続行するための通知を受け取りますか?

私は1週間前にC#を始めました。だから、これがnoobishか、すでに尋ねられている場合はお詫び申し上げます。私はどこにでも見ましたが、いくつかの種類の通知があり、それを理解しようとすると私はフランス語を読んでいるようです。

答えて

0

私はPCLプロジェクトで2インターフェイスの作成から始めます:、Androidのプロジェクトでは、次に

public interface IAlarm { 
    void SetAlarm(); 
} 

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

を実装作成:

アラームヘルパー

[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が呼び出されます:PCLプロジェクトのゲームロジックで

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

次のように、新しいアラームを設定することができます。

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

Iアラーム&の通知ロジックを意図的に切り離して、通知を表示してそのタイトルとテキストを設定する必要がある場合は、10分後に確認することができます。代わりに、intent.putextraを使用してアラームを設定するときに、タイトルとテキストを渡すこともできます。

関連する問題