1、打开XE8,新建Mutli-Device Form-C++ Builder工程,在Form中加入组件NotificationCenter;
注意:系统通知只能在手机上运行,所以平台要更换为andriod或者iOS等
2、在.cpp中加入头文件
#include<FMX.Platform.hpp>
#include<System.IOUtils.hpp>
#include<FMX.Notification.hpp>
3、在.h文件中加入GetSoundName()方法的声明:UnicodeString __fastcall GetSoundName();
4、加入各个按钮,分别写入代码:
// ----------------------- 发送通知------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TNotification *myNotification=NotificationCenter1->CreateNotification();
__try{
myNotification->Number=3;
myNotification->AlertBody=L"发送通知";
//myNotification->FireDate=Now()+EncodeTime(0,0,10,0);
NotificationCenter1->ScheduleNotification(myNotification);
}__finally { //因为finally不在标准中,所以要加下划线
myNotification->DisposeOf();
}
}
//------------------ 稍后通知,如10秒后-------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(NotificationCenter1->Supported()){
TNotification *myNotification=NotificationCenter1->CreateNotification();
__try{
myNotification->Number=3;
myNotification->AlertBody=L"10秒后再提醒";
myNotification->FireDate=Now()+EncodeTime(0,0,10,0);
NotificationCenter1->ScheduleNotification(myNotification);
}__finally {
myNotification->DisposeOf();
}
}
}
//--------------- 重复通知-------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(NotificationCenter1->Supported()){
TNotification *myNotification=NotificationCenter1->CreateNotification();
__try{
myNotification->Name="myNotification";
myNotification->AlertBody=L"每分钟重复通知";
myNotification->FireDate= Now()+EncodeTime(0,0,10,0);
myNotification->RepeatInterval=TRepeatInterval::Minute;
NotificationCenter1->ScheduleNotification(myNotification);
} __finally{
myNotification->DisposeOf();
}
}
}
//--------------------- 取消通知-----------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
NotificationCenter1->CancelNotification("myNotification");
}
//--------------------------- 有铃声的通知(注意:手机设置功能里是否有关闭通知铃声)--------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
if(NotificationCenter1->Supported()){
TNotification *myNotification=NotificationCenter1->CreateNotification();
__try{
myNotification->Name="myNotification";
myNotification->AlertBody=L"这是我的通知,立即发送";
myNotification->EnableSound=true;
myNotification->SoundName=GetSoundName();
NotificationCenter1->PresentNotification(myNotification);
} __finally{
myNotification->DisposeOf();
}
}
}
//------------------------- GetSoundName()方法----------------------
UnicodeString __fastcall TForm1::GetSoundName(){
UnicodeString result=
System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetSharedDocumentsPath(),"soundsample.mp3");
return result;
}
5、点击NotificationCenter组件,双击Events里的OnReceiveLocalNotification事件,写入代码:
//--------------------------- 收到通知,提示“已阅读”--------------------
void __fastcall TForm1::NotificationCenter1ReceiveLocalNotification(TObject *Sender,
TNotification *ANotification)
{
ShowMessage("已阅读");
}
6、在手机上运行,点击每一个按钮即可有相应的响应。(其余功能,读者可自行润色)