本文介绍了发出一个通知后,Delphi Seattle Android TNotificationCenter取消全部不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi DX Seattle并在android设备上进行测试

,当用户单击一个按钮时,我正在创建多个计划的通知

,并且当用户单击另一个按钮时我正在清除剩余的通知

I'm using Delphi DX Seattle and testing on an android Device
when a user clicks a button i'm creating multiple scheduled notifications
and when a user clicks another button im clearing the remaining notifications

我似乎无法获得TNotificationCenter.CancelAll在发出一个通知后实际上取消了通知

I cant seem to get the TNotificationCenter.CancelAll to actually cancel notifications after one notification has fired
Similar Question Here

使用Delphi示例作为基本代码

我'm将立即发送通知按钮更改为发送多个通知,代码如下所示:

Using the Delphi sample SendCancelNotification as the base code
I'm Changing the "Send Notification Immediately" button to "Send Multiple Notifications" and the code looks like this

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;

这会在单击按钮后发送三个通知。

如果我单击发送多个通知按钮,然后在5秒钟前单击取消所有通知按钮,通知将被成功取消。

This sends three notifications after the button has been clicked.
if i click the "Send Multiple Notifications" button and then before 5 seconds click the "cancel all notifications" button the notifications are canceled successfully.

procedure TNotificationsForm.SpeedButton2Click(Sender: TObject);
begin
  NotificationC.CancelAll;
end;

但是,如果我单击多通知按钮并等到第一个通知触发后,请单击取消所有通知按钮,则不会取消剩余的通知。例如通知第二和第三仍在运行。

But if i click the multi notification button and wait until the first notification has fired then click the "cancel all notifications" button, it doesn't cancel the remaining notifications. e.g. notifications Second and Third still run.

有人遇到过吗?或对发送了通知之一后的CancelAll事件为何不起作用有任何想法

Has anyone come across this? or have any ideas on why the CancelAll event doesn't work after one of the notifications have been sent

推荐答案

问题并使其起作用

此解决方案要求您不要使用数字作为通知名称的首字符

,如果您能想到解决此问题的更好方法问题,请发布您的答案

I have found the issue and have got it to work
this solution requires you to not have a numerical number as the first character of your notification name
if you can think of a better way to resolve this issue please post your answer

如果您遇到同一问题,请从文件放入您的项目文件夹,并发送了三个或更多

if you are running into the same issue then copy the System.Android.Notification.pas from Source code here file into your project folder and give it a go

通知,并且第一个通知在android上触发,通知丢失了#10字符。

after sending off three or more notifications and the first notification is triggered on android the notifications were loosing the #10 character.

例如one = 10#10Two = 11#10Three = 12
变成Two = 11Three = 12,然后cancel all都没有名字匹配,也从未取消过两个或三个

e.g. one=10#10Two=11#10Three=12was becoming Two=11Three=12 and then the cancel all was not getting a name match and never canceling two or three

我已将

Embarcadero\Studio\17.0\source\rtl\common\System.Android.Notification.pas

文件复制到我的项目文件并对其进行了修改,以帮助您更好地了解正在发生的事情

I have copied the
Embarcadero\Studio\17.0\source\rtl\common\System.Android.Notification.pas
file to my project file and modified it to help make more sense of what was happening

这是我用来帮助查找问题的功能

this is the function that i was using to help track down the problem

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,则删除原始行,然后循环遍历=字符中的原始字符串,直到出现非数字,然后将其重新插入字符串列表中。

what i ended up doing was to check for more than 1 = character in the string list loop
if there were more than 1, Delete the original line then loop through the original string from the = character until a non numeric number occurred and re insert that into the string list

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

to the new GetAllNotificationsNamesNonFiltered

如果上面的内容没有意义,请查看源代码

have a look at the source code if something above doesn't make sense Source code here

这篇关于发出一个通知后,Delphi Seattle Android TNotificationCenter取消全部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:56