delphi移动应用程序允许权限后更新权限状态

发布于 2025-01-13 02:20:47 字数 1035 浏览 0 评论 0原文

我正在 Android 和 iOS 平台上开发一个 Delphi 移动应用程序,需要显示通知。当我第一次单击该按钮时,它将请求通知权限。但是,当我单击提示权限的对话框上的“允许”按钮后,通知不显示。我需要再次按下按钮才能显示通知。

有没有办法让我的应用程序在我允许应用程序的通知权限后立即获得状态?

下面是我的原型代码:

procedure TForm1.Button3Click(Sender: TObject);
begin
  var notiCenter := TNotificationCenter.Create(nil);
  notiCenter.OnPermissionRequestResult := NotificationCenterPermissionRequestResult;
  
  if notiCenter.AuthorizationStatus <> TAuthorizationStatus.Authorized then
    notiCenter.RequestPermission;
  
  if FIsNotificationAllowed then begin
    var noti := notiCenter.CreateNotification;
    try
      noti.Name := 'Button Click';
      noti.Title := 'Button Click';
      noti.AlertBody := 'Button Click';
      notiCenter.PresentNotification(noti);
    finally
      noti.DisposeOf;
    end;
  end;
end;


procedure TForm1.NotificationCenterPermissionRequestResult(Sender: TObject;
  const AIsGranted: Boolean);
begin
  if AIsGranted then FIsNotificationAllowed := true
  else FIsNotificationAllowed := false;
end;

I am developing a Delphi mobile app on both Android and iOS platforms that requires to show notifications. When I first click on the button, it will request notification permission. However, after I click on the 'Allow' button on the dialog that prompts for permission, the notification doesn't show. I need to press the button again in order to show the notification.

Is there any way to let my application get the status immediately after I allow notification permission for the application?

Below is my prototype code:

procedure TForm1.Button3Click(Sender: TObject);
begin
  var notiCenter := TNotificationCenter.Create(nil);
  notiCenter.OnPermissionRequestResult := NotificationCenterPermissionRequestResult;
  
  if notiCenter.AuthorizationStatus <> TAuthorizationStatus.Authorized then
    notiCenter.RequestPermission;
  
  if FIsNotificationAllowed then begin
    var noti := notiCenter.CreateNotification;
    try
      noti.Name := 'Button Click';
      noti.Title := 'Button Click';
      noti.AlertBody := 'Button Click';
      notiCenter.PresentNotification(noti);
    finally
      noti.DisposeOf;
    end;
  end;
end;


procedure TForm1.NotificationCenterPermissionRequestResult(Sender: TObject;
  const AIsGranted: Boolean);
begin
  if AIsGranted then FIsNotificationAllowed := true
  else FIsNotificationAllowed := false;
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文