Delphi 西雅图 Android TNotificationCenter CancelAll 在触发一个通知后无法正常工作
Delphi Seattle Android TNotificationCenter CancelAll not working after one notification has fired
我正在使用 Delphi DX Seattle 并在 android 设备上进行测试
当用户单击按钮时,我正在创建多个预定通知
当用户点击另一个按钮时,我会清除剩余的通知
我似乎无法让 TNotificationCenter.CancelAll 在一个通知触发后实际取消通知
Similar Question Here
使用 Delphi 示例 SendCancelNotification 作为基本代码
我正在将 "Send Notification Immediately" 按钮更改为 "Send Multiple Notifications",代码如下所示
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;
单击按钮后将发送三个通知。
如果我点击 "Send Multiple Notifications" 按钮,然后在 5 秒前点击 "cancel all notifications" 按钮,通知将被成功取消。
procedure TNotificationsForm.SpeedButton2Click(Sender: TObject);
begin
NotificationC.CancelAll;
end;
但是如果我单击多通知按钮并等到第一个通知触发然后单击 "cancel all notifications" 按钮,它不会取消剩余的通知。例如第二个和第三个通知仍然 运行.
有人遇到过这个吗?或者对发送其中一个通知后 CancelAll 事件为何不起作用有任何想法
我已找到问题并已解决
此解决方案要求您不能将数字作为通知名称的第一个字符
如果您能想到更好的方法来解决此问题,请post您的回答
如果您 运行 遇到同样的问题,请将 System.Android.Notification.pas 从 Source code here 文件复制到您的项目文件夹中,然后试一试
发送三个或更多通知后,第一个通知在 android 上触发,通知丢失 #10 字符。
例如一=10#10二=11#10三=12
变成了 Two=11Three=12 然后取消全部没有得到名称匹配并且从不取消两个或三个
我已经复制了
Embarcadero\Studio.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;
我还必须更改调用
的任何地方
Notifications := TStringList.Create;
NotificationsStr := FPreference.getString(TJNotificationAlarm.JavaClass.SETTINGS_NOTIFICATION_IDS, nil);
Notifications.Text := JStringToString(NotificationsStr);
到新的 GetAllNotificationsNamesNonFiltered
如果上面的内容没有意义,请查看源代码Source code here
我正在使用 Delphi DX Seattle 并在 android 设备上进行测试
当用户单击按钮时,我正在创建多个预定通知
当用户点击另一个按钮时,我会清除剩余的通知
我似乎无法让 TNotificationCenter.CancelAll 在一个通知触发后实际取消通知
Similar Question Here
使用 Delphi 示例 SendCancelNotification 作为基本代码
我正在将 "Send Notification Immediately" 按钮更改为 "Send Multiple Notifications",代码如下所示
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;
单击按钮后将发送三个通知。
如果我点击 "Send Multiple Notifications" 按钮,然后在 5 秒前点击 "cancel all notifications" 按钮,通知将被成功取消。
procedure TNotificationsForm.SpeedButton2Click(Sender: TObject);
begin
NotificationC.CancelAll;
end;
但是如果我单击多通知按钮并等到第一个通知触发然后单击 "cancel all notifications" 按钮,它不会取消剩余的通知。例如第二个和第三个通知仍然 运行.
有人遇到过这个吗?或者对发送其中一个通知后 CancelAll 事件为何不起作用有任何想法
我已找到问题并已解决
此解决方案要求您不能将数字作为通知名称的第一个字符
如果您能想到更好的方法来解决此问题,请post您的回答
如果您 运行 遇到同样的问题,请将 System.Android.Notification.pas 从 Source code here 文件复制到您的项目文件夹中,然后试一试
发送三个或更多通知后,第一个通知在 android 上触发,通知丢失 #10 字符。
例如一=10#10二=11#10三=12 变成了 Two=11Three=12 然后取消全部没有得到名称匹配并且从不取消两个或三个
我已经复制了
Embarcadero\Studio.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;
我还必须更改调用
的任何地方Notifications := TStringList.Create;
NotificationsStr := FPreference.getString(TJNotificationAlarm.JavaClass.SETTINGS_NOTIFICATION_IDS, nil);
Notifications.Text := JStringToString(NotificationsStr);
到新的 GetAllNotificationsNamesNonFiltered
如果上面的内容没有意义,请查看源代码Source code here