当用户完成任务时,是否可以通过任何方式从服务器发送推送通知?例如:待办事项应用程序将在该日期通过推送通知进行通知。我想使用Firebase和Firestore来存储用户令牌。 警报管理器可以是我找到的解决方案,但我不想使用它。
答案 0 :(得分:1)
是的,您可以使用调度程序将通知从服务器发送到您的应用程序: 您可以按照我的工作代码进行操作:
补充IJob:
public class SendNotificationViaFcm: IJob
{
public void Execute(IJobExecutionContext context)
{
bool isNotificationSent=false;
try
{
var taskToSendNotification = FirebaseCloudMessaging.SendMessage();
Task.WaitAll(taskToSendNotification);
isNotificationSent = taskToSendNotification.Result;
}
catch (Exception exception)
when (
exception is ObjectDisposedException || exception is ArgumentNullException ||
exception is AggregateException)
{
}
catch (Exception exception) when (exception is InvalidOperationException)
{
}
catch (Exception exception)
{
// ignored
}
}
}
从服务器呼叫FCM Api:
public class FirebaseCloudMessaging
{
private static readonly Uri FcmUri = new Uri(
uriString: @"https://fcm.googleapis.com",
uriKind: UriKind.Absolute);
private const string FcmApiKey = "Your Legacy Server Key";
public static async Task<bool> SendMessage()
{
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = FcmUri;
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization",
"key=" + FcmApiKey);
var response = await httpClient.PostAsJsonAsync(@"/fcm/send", new
{
to = "/topics/global",
priority = "high",
data = new
{
title = "Warning",
message = "Please start app to track movemoent!"
}
//to = "/topics/global",
//priority = "high",
//notification = new
//{
// title = "Warning!",
// body = "Please start app to track movemoent!"
//}
});
Debug.Write(response.Content.ReadAsStringAsync());
var ck = response.IsSuccessStatusCode;
return response.IsSuccessStatusCode;
}
}
}
在您的时间间隔内实施时间表:
public class Scheduler
{
public static void Start()
{
try
{
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
// scheduler.Shutdown();
var sentCloudNotification = JobBuilder.Create<SendNotificationViaFcm>().Build();
var cloudNotificationTrigger = TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever()).Build();
scheduler.ScheduleJob(sentCloudNotification, cloudNotificationTrigger);
}
catch (SchedulerException exception)
{
Debug.Write(exception.Message);
}
catch (Exception exception)
{
Debug.Write(exception.Message);
}
}
}
最后从您的Global.asax.cs
运行protected void Application_Start()
{
Scheduler.Start();
}
答案 1 :(得分:0)
听起来您正在寻找一种可以安排事务通知的工具。您正在使用哪种服务器技术?
从高层次上,您可以执行以下操作: 1)用户在Android应用程序中添加任务 2)android应用程序向服务器发送请求以保存任务 3)您有一些在某种任务保存回调中运行的代码,该回调计划使用crontab,celery或类似工具安排将来运行的代码块。 4)将来运行的代码块是对twilio的api调用,用于发送推送通知
相关链接:https://www.twilio.com,https://firebase.google.com/docs/cloud-messaging/,http://www.celeryproject.org/,https://en.wikipedia.org/wiki/Cron