自动从应用服务器发送推送通知



当用户完成任务时,有没有办法从服务器发送推送通知?例如:待办事项应用程序将在该日期通过推送通知进行通知。我想使用 firebase 和 firestore 来存储用户令牌。 警报管理器可以是我找到的解决方案,但我不想使用它。

是的,您可以使用调度程序将通知从服务器发送到您的应用程序: 您可以遵循我的工作代码:

Emplement 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(用户在安卓应用中添加任务 2(安卓应用程序向服务器发送请求以保存任务 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

相关内容

  • 没有找到相关文章