Delphi Seattle Android TNotificationCenter在触发一个通知后取消所有不工作
我正在使用Delphi DX,并在android设备上进行测试Delphi Seattle Android TNotificationCenter在触发一个通知后取消所有不工作,android,delphi,notifications,firemonkey,Android,Delphi,Notifications,Firemonkey,我正在使用Delphi DX,并在android设备上进行测试 当用户单击按钮时,我正在创建多个计划通知 当用户点击另一个按钮时,我会清除剩余的通知 我似乎无法让TNotificationCenter.CancelAll在一个通知触发后实际取消通知 使用Delphi示例作为基本代码 我将“立即发送通知”按钮更改为“发送多个通知”,代码如下所示 procedure TNotificationsForm.btnSendMultipleNotificationsClick( Sender: TOb
当用户单击按钮时,我正在创建多个计划通知
当用户点击另一个按钮时,我会清除剩余的通知 我似乎无法让TNotificationCenter.CancelAll在一个通知触发后实际取消通知
使用Delphi示例作为基本代码
我将“立即发送通知”按钮更改为“发送多个通知”,代码如下所示
procedure TNotificationsForm.btnSendMultipleNotificationsClick(
Sender: TObject);
procedure MyNotificationCreate(aNotificationName : String; aSecondDelay : Word);
var
Notification: TNotification;
begin
if (aSecondDelay < 0) or (aSecondDelay > 60) then
raise Exception.Create('Seconds must be between 0 and 60');
{ verify if the service is actually supported }
Notification := NotificationC.CreateNotification;
try
Notification.Name := aNotificationName;
Notification.AlertBody := aNotificationName + ' ran after ' + inttostr(aSecondDelay) + ' seconds';
{ Fired in 10 second }
Notification.FireDate := Now + EncodeTime(0,0,aSecondDelay,0);
{ Send notification in Notification Center }
NotificationC.ScheduleNotification(Notification);
finally
Notification.DisposeOf;
end;
end;
begin
MyNotificationCreate('First' , 5);
MyNotificationCreate('Second', 10);
MyNotificationCreate('Third' , 15);
end;
但是,如果单击“多通知”按钮并等待第一个通知发出,然后单击“取消所有通知”按钮,则不会取消其余通知。e、 g.第二次和第三次通知仍在运行
有人见过这个吗?或者,您是否知道为什么在发送一个通知后CancelAll事件不起作用我发现了问题并使其起作用
此解决方案要求通知名的第一个字符不是数字
如果你能想出更好的方法来解决这个问题,请发布你的答案 如果您遇到相同的问题,请将System.Android.Notification.pas从文件复制到项目文件夹中,并尝试一下 发送三个或更多通知后,第一个通知在android上触发,通知失去了#10字符 e、 g.一=10#二=11#三=12 变为2=113=12,然后“全部取消”没有获得名称匹配,并且从未取消两个或三个 我已经复制了
Embarcadero\Studio\17.0\source\rtl\common\System.Android.Notification.pas
将文件添加到我的项目文件中,并对其进行修改,以帮助更清楚地了解正在发生的事情 这是我用来帮助追踪问题的函数
function TAndroidPreferenceAdapter.GetAllNotificationsNames: TStringList;
var
Notifications: TStringList;
NotificationsStr: JString;
I: Integer;
begin
Notifications := TStringList.Create;
NotificationsStr := FPreference.getString(TJNotificationAlarm.JavaClass.SETTINGS_NOTIFICATION_IDS, nil);
Notifications.Text := JStringToString(NotificationsStr);
for I := 0 to Notifications.Count - 1 do
Notifications[I] := ExtractName(Notifications[I]);
Result := Notifications;
end;
我最后做的是检查字符串列表循环中是否有超过1个=字符如果大于1,则删除原始行,然后从=字符循环原始字符串,直到出现非数字数字,然后将其重新插入字符串列表
function TAndroidPreferenceAdapter.GetAllNotificationsNamesNonFiltered: TStringList;
var
Notifications: TStringList;
NotificationsStr: JString;
I: Integer;
function OccurrencesOfChar(const S: string; const C: char): integer;
var
i: Integer;
begin
result := 0;
for i := 1 to Length(S) do
if S[i] = C then
inc(result);
end;
function InsertLineBreaks(const S: string) : String;
var sNewString, sRemaining : String;
i : integer;
const validChars = '0123456789';
begin
try
sNewString := '';
sRemaining := S;
for I := pos('=',sRemaining,1) to length(sRemaining) -1 do begin
if pos( sRemaining[i], validChars ) > 0 then begin
//continue as its still an integer
end else begin
sNewString := copy( sRemaining, 0, i);
sRemaining := copy( sRemaining, i+1, Length(s));
if OccurrencesOfChar(sRemaining, '=') > 1 then begin
InsertLineBreaks(sRemaining);
sRemaining := '';
end;
break;
end;
end;
if sNewString <> '' then
Notifications.Add( sNewString ) ;
if sRemaining <> '' then
Notifications.Add( sRemaining ) ;
Result := sNewString + sRemaining;
except
on E:Exception do begin
e.Message := e.Message + #13#10 + 'fn[TAndroidPreferenceAdapter.GetAllNotificationsNamesNonFiltered.InsertLineBreaks]';
raise;
end;
end;
end;
var sTemp : String;
begin
Notifications := TStringList.Create;
NotificationsStr := FPreference.getString(TJNotificationAlarm.JavaClass.SETTINGS_NOTIFICATION_IDS, nil);
Notifications.Text := JStringToString(NotificationsStr);
for I := 0 to Notifications.Count - 1 do begin
if OccurrencesOfChar(Notifications[I], '=') > 1 then begin
sTemp := Notifications[I];
Notifications.Delete( i );
InsertLineBreaks( sTemp );
break;
end;
end;
Result := Notifications;
end;
到新的GetAllNotificationsNameNoFiltered
如果上面的内容不合理,请查看源代码
function TAndroidPreferenceAdapter.GetAllNotificationsNamesNonFiltered: TStringList;
var
Notifications: TStringList;
NotificationsStr: JString;
I: Integer;
function OccurrencesOfChar(const S: string; const C: char): integer;
var
i: Integer;
begin
result := 0;
for i := 1 to Length(S) do
if S[i] = C then
inc(result);
end;
function InsertLineBreaks(const S: string) : String;
var sNewString, sRemaining : String;
i : integer;
const validChars = '0123456789';
begin
try
sNewString := '';
sRemaining := S;
for I := pos('=',sRemaining,1) to length(sRemaining) -1 do begin
if pos( sRemaining[i], validChars ) > 0 then begin
//continue as its still an integer
end else begin
sNewString := copy( sRemaining, 0, i);
sRemaining := copy( sRemaining, i+1, Length(s));
if OccurrencesOfChar(sRemaining, '=') > 1 then begin
InsertLineBreaks(sRemaining);
sRemaining := '';
end;
break;
end;
end;
if sNewString <> '' then
Notifications.Add( sNewString ) ;
if sRemaining <> '' then
Notifications.Add( sRemaining ) ;
Result := sNewString + sRemaining;
except
on E:Exception do begin
e.Message := e.Message + #13#10 + 'fn[TAndroidPreferenceAdapter.GetAllNotificationsNamesNonFiltered.InsertLineBreaks]';
raise;
end;
end;
end;
var sTemp : String;
begin
Notifications := TStringList.Create;
NotificationsStr := FPreference.getString(TJNotificationAlarm.JavaClass.SETTINGS_NOTIFICATION_IDS, nil);
Notifications.Text := JStringToString(NotificationsStr);
for I := 0 to Notifications.Count - 1 do begin
if OccurrencesOfChar(Notifications[I], '=') > 1 then begin
sTemp := Notifications[I];
Notifications.Delete( i );
InsertLineBreaks( sTemp );
break;
end;
end;
Result := Notifications;
end;
Notifications := TStringList.Create;
NotificationsStr := FPreference.getString(TJNotificationAlarm.JavaClass.SETTINGS_NOTIFICATION_IDS, nil);
Notifications.Text := JStringToString(NotificationsStr);