应用关闭时推送通知 [英] Pushing notifications when app is closed
问题描述
我希望在 Xamarin Forms 中使用 C# 创建一个基于文本的 Android 游戏.
I am looking to create a Text-Based Android game using C# in Xamarin Forms.
在故事中,我想设置角色任务,这需要一些时间,例如我去挖这个洞,挖完我给你打个电话."
In the story, I want to set the character tasks, which will take them some time e.g. "I'll go dig this hole, I'll give you a buzz when I'm done."
如何设置通知以在设定时间后显示?例如上面的语句可能需要 10 分钟,然后用户收到通知继续游戏?
How do I set up notifications to appear after set times? For example the above statement may take 10 minutes, then the user receives a notification to continue the game?
我一周前才开始使用 C#,所以如果这是菜鸟,或者已经被问到,我深表歉意.我到处找,但有几种类型的通知,当我试图理解它时,我似乎正在阅读法语.
I've only started C# a week ago, so I apologize if this is noobish, or already been asked. I have looked everywhere but there are several types of notification, and it seems like I'm reading french when I try to make sense of it.
推荐答案
将此行添加到您的 AndroidManifest.xml
Add this line to your AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
在你的安卓项目中实现 BroadcastReceiver
Implement BroadcastReceiver in your android project
[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();
}
}
这篇关于应用关闭时推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!