版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lile1234_show/article/details/81568143
需求:客户端主动发起的推送消息,不需要后端。(例如每2小时推送一些内容)
我的项目是Unity 导出的IL2CPP工程,代码是C++/Cx。
如果导出是C#的工程的可以直接参考官方文档。
1.参考代码:
void MainPage::ShowLocalMsg(String^ message) {
std::vector<std::wstring> Result = Instance->Split(message->Data(), '$');
String^ Id = ref new String(Result.at(0).c_str());
String^ Second_Str = ref new String(Result.at(1).c_str());
int Second = to_int(Second_Str);
String^ Xml = ref new String(Result.at(2).c_str());
if (!CancelLocalMsg(Id, Second))
{
XmlDocument^ toastXml = ref new XmlDocument();
toastXml->LoadXml(Xml);
auto calendar = ref new Windows::Globalization::Calendar();
calendar->SetToNow();
calendar->AddSeconds(Second);
auto toast = ref new ScheduledToastNotification(toastXml, calendar->GetDateTime());
toast->Id = Id;
ToastNotificationManager::CreateToastNotifier()->AddToSchedule(toast);
}
}
void MainPage::ClearLocalMsg()
{
auto toast = ToastNotificationManager::CreateToastNotifier();
auto list = toast->GetScheduledToastNotifications();
for (int i = 0; i < list->Size; i++)
{
toast->RemoveFromSchedule(list->GetAt(i));
}
}
bool MainPage::CancelLocalMsg(String^ Id,int second) {
auto toast = ToastNotificationManager::CreateToastNotifier();
auto list = toast->GetScheduledToastNotifications();
for (int i = 0; i < list->Size; i++)
{
if (list->GetAt(i)->Id == Id) {
toast->RemoveFromSchedule(list->GetAt(i));
}
}
if (second > 0)
{
return false;
}
else
{
return true;
}
}
2.后端主动推送—Part1
https://blog.csdn.net/blog_lee/article/details/76274697