我希望在Xamarin Forms中使用C#创建一个基于文本的Android游戏。
在故事中,我想设置角色任务,这将花费他们一些时间,例如“我会挖掘这个洞,当我完成时,我会给你一个嗡嗡声。”
如何设置在设定时间后显示的通知?例如,上述声明可能需要10分钟,然后用户会收到继续游戏的通知吗?
我一周前才开始使用C#,所以如果这是noobish,或者已经被问过,我会道歉。我到处都看,但有几种类型的通知,当我试图理解它时,似乎我正在读法语。
答案 0 :(得分:0)
我首先要在PCL项目中创建2个接口:
$txt = $str.PHP_EOL;
然后,在Android Project中,创建实现:
警报助手
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");
}
}
我故意将闹钟和闹钟分开。通知逻辑,使您能够在10分钟后检查是否仍应显示通知并设置其标题和文本。另一种方法是在使用alarmHelper = DependencyService.Get<IAlarm>();
alarmSetter.SetAlarm(10); // 10 minutes from now
设置闹钟时传递标题和文字。
答案 1 :(得分:0)
将此行添加到您的AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
在您的android项目中实现BroadcastReceiver
[BroadcastReceiver]
public class NotificationBroadcastReceiver : BroadcastReceiver
{
private ApprovalDataStore _approvalDataStore => DependencyService.Get<ApprovalDataStore>();
public override async void OnReceive(Context context, Intent intent)
{
var pm = PowerManager.FromContext(context);
var wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "GCM Broadcast Reciever Tag");
wakeLock.Acquire();
//Write your code here
// When you are finished
wakeLock.Release();
}
}