Notifications on Windows 10

Discussion in 'HelpDesk' started by Mavrick, May 16, 2018.

  1. Mavrick

    Mavrick Registered User

    Joined:
    Feb 21, 2009
    Messages:
    453
    Likes Received:
    1,933
    This is driving me CRAZY and I'm not sure if I'm missing something or I've no understanding of the notification system on Win10 lol

    According to embarcadero docs '...notifications for a short period of time, after this, the notifications are available in the Action Center.' However, for me, the notification appears but DOES NOT them go to the Action Centre!

    I don't interact with the notification and allow it to timeout. HELP lol


    I have got a TNotificationCenter component on the form along with a button. The Buttons.OnClick event has the following.

    Code:
    procedure TForm3.Button1Click(Sender: TObject);
    var
      AppNotification: TNotification;
    begin
      AppNotification := NotificationCenter1.CreateNotification;
      try
        AppNotification.Name := 'Windows10Notification';
        AppNotification.Title := 'Windows 10 Notification #1';
        AppNotification.AlertBody := 'RAD Studio 10 Seattle';
    
        NotificationCenter1.PresentNotification(AppNotification);
    
      finally
        AppNotification.Free;
      end;
    end;
    
     
  2. Wi

    WillSmith2050 Registered User

    Joined:
    Sep 8, 2008
    Messages:
    85
    Likes Received:
    1,352
    It does not show these notifications by default.
    You have to enable it for your application.

    Code:
    http://www.itprotoday.com/windows-server/how-manage-notifications-windows-10
     
    Last edited: May 16, 2018
  3. Mavrick

    Mavrick Registered User

    Joined:
    Feb 21, 2009
    Messages:
    453
    Likes Received:
    1,933
    That didn't help much @WillSmith2050 as it was about how users can change notification. However, it did give me a train of thought to follow so thanks.

    After searching the net I found one solution that appears to work. I've put together a sample app below. Feedback on whether it works on your system would be very welcome.

    Mr. @Skydevil your thoughts on this would be great as it appears to be a poor implementation by Embarcadero for Desktop Windows 10.


    SPOILER below contains the DFM.

    Code:
    {
    You must register your application for "Show notifications in action center"
    "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\$prodName"
     -Name "ShowInActionCenter" -Type Dword -Value "1"
    }
    unit uNotify;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Notification, Vcl.StdCtrls,
      System.Hash, System.Win.Registry, System.UITypes;
    
    type
      TfmMain = class(TForm)
        NotificationCenter1: TNotificationCenter;
        btnShowNotification: TButton;
        btnToggleActionCentre: TButton;
        lblAC: TLabel;
        lblRegKeyExists: TLabel;
        procedure btnShowNotificationClick(Sender: TObject);
        function getRegisterToastMessageKey : String;
        procedure btnToggleActionCentreClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      fmMain: TfmMain;
    
    procedure InsertKeysToReg(HKey: Cardinal; OurRegKeyPath, ourKey: string; NewKeyValue: integer);
    procedure DeleteKeyFromReg(HKey: Cardinal; OurRegKeyPath: string);
    function DoesRegKeyExist: Boolean;
    procedure UpdateLabel;
    
    implementation
    
    var
      OurRegKeyPath: string;
      HKey: Cardinal = HKEY_CURRENT_USER;
    
    {$R *.dfm}
    
    procedure TfmMain.btnShowNotificationClick(Sender: TObject);
    var
      AppNotification: TNotification;
    begin
      AppNotification := NotificationCenter1.CreateNotification;
      try
        AppNotification.Name := 'Windows10Notification';
        AppNotification.Title := 'Windows 10 Notification #1';
        AppNotification.AlertBody := 'RAD Studio 10 Seattle';
    
        NotificationCenter1.PresentNotification(AppNotification);
    
      finally
        AppNotification.Free;
      end;
    end;
    
    procedure TfmMain.btnToggleActionCentreClick(Sender: TObject);
    begin
      if btnToggleActionCentre.Caption = 'Enable' then
      begin
        btnToggleActionCentre.Caption := 'Disable';
        InsertKeysToReg(HKey, OurRegKeyPath, 'ShowInActionCenter', 1);
      end
      else
      begin
        DeleteKeyFromReg(HKey, OurRegKeyPath);
        btnToggleActionCentre.Caption := 'Enable';
      end;
    
      UpdateLabel;
    
    end;
    
    procedure TfmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
      if DoesRegKeyExist then
        begin
          if MessageDlg('Remove RegKey before Exit?', mtConfirmation,
              [mbYes, mbNo], 0) = mrYes then DeleteKeyFromReg(HKey, OurRegKeyPath);
        end;
    end;
    
    procedure TfmMain.FormCreate(Sender: TObject);
    begin
      UpdateLabel;
    end;
    
    procedure InsertKeysToReg(HKey: Cardinal; OurRegKeyPath, ourKey: string; NewKeyValue: integer);
    var
      reg        : TRegistry;
      openResult : Boolean;
    begin
      reg := TRegistry.Create(KEY_READ OR KEY_WOW64_64KEY);
      reg.RootKey := HKey; //HKEY_USERS; //HKEY_LOCAL_MACHINE;
    
      { Checking if the values exist and inserting when neccesary }
      reg.Access := KEY_WRITE;
      openResult := reg.OpenKey(OurRegKeyPath, True);
    
      if not openResult = True then
        begin
          MessageDlg('Unable to create key! Exiting.',
                      mtError, mbOKCancel, 0);
          Exit();
        end;
    
      try
        //reg.RootKey := HKEY_CURRENT_USER;
        if reg.OpenKey(OurRegKeyPath, true) then
        try
          reg.WriteInteger(ourKey, NewKeyValue);
        finally
          reg.CloseKey;
        end;
      finally
        reg.Free;
      end;
    
    end;
    
    procedure DeleteKeyFromReg(HKey: Cardinal; OurRegKeyPath: string);
    var
      reg : TRegistry;
    begin
      reg := TRegistry.Create(KEY_WRITE);
      reg.RootKey := HKey;
    
      reg.DeleteKey(OurRegKeyPath);
    
      reg.CloseKey();
      reg.Free;
    
    end;
    
    function TfmMain.getRegisterToastMessageKey : String;
    const
      // It appears that you can't change this atm :(
      AppId = 'Embarcadero.DesktopToasts.';
    begin
      result := AppId + THashBobJenkins.GetHashString(ParamStr(0));
    end;
    
    function DoesRegKeyExist: Boolean;
    var
      reg:  TRegistry;
    begin
      // Set the global variable
      Result := False;
    
      OurRegKeyPath := '\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\';
      OurRegKeyPath := OurRegKeyPath + fmMain.getRegisterToastMessageKey; //$prodName
    
      reg := TRegistry.Create(KEY_READ);
      reg.RootKey := HKey;
    
      if reg.KeyExists(OurRegKeyPath) then Result := True;
    end;
    
    procedure UpdateLabel;
    begin
      if DoesRegKeyExist then fmMain.lblRegKeyExists.Caption := 'Reg key is PRESENT'
      else fmMain.lblRegKeyExists.Caption := 'Reg key is ABSENT';
    end;
    
    end.
    
    object fmMain: TfmMain
    Left = 0
    Top = 0
    Caption = 'Win10 Action Centre'
    ClientHeight = 144
    ClientWidth = 274
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    OldCreateOrder = False
    OnCloseQuery = FormCloseQuery
    OnCreate = FormCreate
    PixelsPerInch = 96
    TextHeight = 13
    object lblAC: TLabel
    Left = 189
    Top = 92
    Width = 66
    Height = 13
    Caption = 'Action Centre'
    end
    object lblRegKeyExists: TLabel
    Left = 8
    Top = 8
    Width = 3
    Height = 13
    end
    object btnShowNotification: TButton
    Left = 8
    Top = 111
    Width = 97
    Height = 25
    Caption = 'Show Notification'
    TabOrder = 0
    OnClick = btnShowNotificationClick
    end
    object btnToggleActionCentre: TButton
    Left = 185
    Top = 111
    Width = 75
    Height = 25
    Caption = 'Enable'
    TabOrder = 1
    OnClick = btnToggleActionCentreClick
    end
    object NotificationCenter1: TNotificationCenter
    Left = 128
    Top = 32
    end
    end
     
    Last edited: May 17, 2018
  4. Mavrick

    Mavrick Registered User

    Joined:
    Feb 21, 2009
    Messages:
    453
    Likes Received:
    1,933
    Decided to turn this into a class. Just save as uActionCentre.pas in your project folder. To use just add it to your project and add uActionCentre to your uses. I know I'm teaching you how to suck eggs lol
    UnHidden Content:
    [HIDDEN CONTENT]
     
    Last edited: May 21, 2018
    NewDelphiTec likes this.