2016-06-19 3 views
0

Xamarin Forms에서 C#을 사용하여 텍스트 기반 안드로이드 게임을 만들려고합니다.앱 종료시 알림 푸시

이야기에서 캐릭터 작업을 설정하고 싶습니다. "나는이 구멍을 파서 갈거야, 내가 끝나면 너 한테 화제를 줄거야."

설정 한 시간 후에 알림을 설정하려면 어떻게해야합니까? 예를 들어 위의 명령문에 10 분이 소요될 수 있습니다. 그런 다음 사용자는 게임을 계속하기 위해 알림을 받습니까?

저는 일주일 전만해도 C#을 시작 했으므로 사과하지 않습니다. 또는 이미 묻습니다. 나는 어디에서나 보았지만 몇 가지 유형의 알림이 있으며, 내가 그것을 이해하려고 할 때 나는 독서를하는 것처럼 보입니다.

답변

0

나는 PCL 프로젝트 2 개 인터페이스 작성하여 시작할 것 : 안드로이드 프로젝트에, 그리고

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가 호출됩니다

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

I을 다음과 같이 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"); 
    } 
} 

, 당신은 새로운 알람을 설정할 수 있습니다 알람 & 알림 로직을 의도적으로 분리하여 알림을 표시하고 제목과 텍스트를 설정해야하는 경우 10 분 후에 확인할 수 있습니다. 알람을 설정할 때 제목과 텍스트를 intent.putextra을 사용하여 전달하는 방법도 있습니다.