Skip to content

Commit

Permalink
:construciton: Android Example Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
p-samuel committed Sep 11, 2024
1 parent 6ec39b3 commit a22242d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
2 changes: 1 addition & 1 deletion samples/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

<service
android:name="com.embarcadero.services.NtfyServiceLocal"
android:exported="false"
android:exported="true"
android:process=":NtfyServiceLocal"
android:foregroundServiceType="dataSync">
</service>
Expand Down
2 changes: 1 addition & 1 deletion samples/android/NtfyAndroid.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ uses
FMX.Forms,
View.Main in 'src\View.Main.pas' {Form1},
Intent.Service.Helper in 'src\Intent.Service.Helper.pas',
Service.Local.Ntfy in 'services\ntfy-service\src\Service.Local.Ntfy.pas',
Service.Local.Ntfy in 'services\ntfy-service\src\Service.Local.Ntfy.pas' {DM: TAndroidService},
FMX.Skia {DM: TAndroidService};

{$R *.res}
Expand Down
74 changes: 48 additions & 26 deletions samples/android/services/ntfy-service/src/Service.Local.Ntfy.pas
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ interface
System.Generics.Collections,
System.Notification,
System.SyncObjs,
System.DateUtils,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.App,
Androidapi.JNI.Os,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.Support,
Androidapi.JNIBridge,
Androidapi.Helpers,
Notify;

type
Expand All @@ -27,7 +29,7 @@ TDM = class(TAndroidService)
private
FNotificationManager: JNotificationManager;
FNotificationBuilder: Japp_NotificationCompat_Builder;
FNotificationID: Integer;
FChannelId: String;
FThreads: TObjectList<TThread>;
FWakeLock: JPowerManager_WakeLock;
procedure ShowUpNotification(const Text: String);
Expand All @@ -40,8 +42,7 @@ TDM = class(TAndroidService)
implementation

uses
System.DateUtils, Androidapi.Helpers, Notify.Facade, Intent.Service.Helper;

Intent.Service.Helper;

const
FOREGROUND_SERVICE_TYPE_DATA_SYNC = $00000001;
Expand All @@ -64,30 +65,33 @@ procedure TDM.AndroidServiceCreate(Sender: TObject);
FThreads := TObjectList<TThread>.Create;

// Initialize Notification Manager and Builder
FChannelId := 'ntfy-for-delphi';
FNotificationManager := TJNotificationManager.Wrap((TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE)));
FNotificationBuilder := TJapp_NotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);
FNotificationBuilder.setSmallIcon(TAndroidHelper.Context.getApplicationInfo.icon);
FNotificationBuilder.setContentTitle(StrToJCharSequence('Ntfy service started'));
FNotificationBuilder.setContentText(StrToJCharSequence('Listening messages'));
FNotificationBuilder.setContentText(StrToJCharSequence('Syncing...'));
FNotificationBuilder.setAutoCancel(True);
FNotificationID := 98437;
FNotificationBuilder.setChannelId(StringToJString(FChannelId));

//Creates notification channel if not exists
if TJBuild_VERSION.JavaClass.SDK_INT >= 26 then
begin
LChannel := TJNotificationChannel.JavaClass.init(
StringToJString('ntfy-for-delphi'),
StrToJCharSequence('Android Ntfy Subscription'),
TJNotificationManager.JavaClass.IMPORTANCE_HIGH);
LChannel.setLightColor(TJColor.JavaClass.BLUE);
LChannel.setLockscreenVisibility(TJNotification.JavaClass.VISIBILITY_PRIVATE);
FNotificationManager.createNotificationChannel(LChannel);
FNotificationBuilder.setChannelId(StringToJString(IntToStr(FNotificationID)));
if FNotificationManager.getNotificationChannel(StringToJString(FChannelId)) = nil then
begin
LChannel := TJNotificationChannel.JavaClass.init(
StringToJString(FChannelId),
StrToJCharSequence('Ntfy Subscription'),
TJNotificationManager.JavaClass.IMPORTANCE_HIGH);
LChannel.setLightColor(TJColor.JavaClass.BLUE);
LChannel.setLockscreenVisibility(TJNotification.JavaClass.VISIBILITY_PRIVATE);
FNotificationManager.createNotificationChannel(LChannel);
end;
end;

// Acquire wake lock
// Creates wake lock object
LPowerManager := TJPowerManager.Wrap(TAndroidHelper.Context.getSystemService(TJContext.JavaClass.POWER_SERVICE));
FWakeLock := LPowerManager.newWakeLock(TJPowerManager.JavaClass.PARTIAL_WAKE_LOCK, StringToJString('MyApp::NtfyWakeLock'));
FWakeLock.acquire();

except
on E: Exception do
Expand All @@ -114,12 +118,14 @@ function TDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIn
LIntentService: TIntentServiceHelper;
begin
try
//1 - Will result to restart the service with the same intent
Result := TJService.JavaClass.START_REDELIVER_INTENT;
LIntentService := TIntentServiceHelper.Create(Intent);

if LIntentService.Data.IsEmpty then
Exit;

//2 - Create a thread to be inserted in the thread object list to let the service running without stopping
LThread := TThread.CreateAnonymousThread(
procedure
var
Expand All @@ -129,12 +135,18 @@ function TDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIn
try
while True do
begin
//3 - Builds a notification and an event object
LJNotification := FNotificationBuilder.build();
LEvent := TEvent.Create;

//4 - Subscribe to Ntfy
Ntfy.Subscribe(LIntentService.Data, ShowUpNtfy);
TJService.Wrap(JavaService).startForeground(FNotificationID, LJNotification, FOREGROUND_SERVICE_TYPE_DATA_SYNC);
FNotificationBuilder.setContentText(StrToJCharSequence('Syncing...'));
FNotificationManager.notify(FNotificationID, FNotificationBuilder.build());

//5 - Starts as foreground and notifies
TJService.Wrap(JavaService).startForeground(1, LJNotification, FOREGROUND_SERVICE_TYPE_DATA_SYNC);
FNotificationManager.notify(1, LJNotification);

//6 - Waits until infinite, so that the service can be kept running "forever"
LEvent.WaitFor(INFINITE);
JavaService.stopSelf(StartId);
end;
Expand All @@ -144,6 +156,7 @@ function TDM.AndroidServiceStartCommand(const Sender: TObject; const Intent: JIn
end;
end);

// 7 - Insert and start the thread
LThread.FreeOnTerminate := False;
FThreads.Add(LThread);
LThread.Start;
Expand All @@ -160,7 +173,7 @@ procedure TDM.ShowUpNotification(const Text: String);
begin
LNotification := NotificationCenter.CreateNotification();
try
LNotification.ChannelId := IntToStr(FNotificationID);
LNotification.ChannelId := FChannelId;
LNotification.Name := IntToStr(Random(1000));
LNotification.Title := 'Ntfy Service';
LNotification.AlertBody := Text;
Expand All @@ -174,15 +187,24 @@ procedure TDM.ShowUpNtfy(AEvent: INotifyEvent);
var
LNotification: TNotification;
begin
LNotification := NotificationCenter.CreateNotification();
if FWakeLock <> nil then
FWakeLock.acquire;

try
LNotification.ChannelId := IntToStr(FNotificationID);
LNotification.Name := IntToStr(Random(1000));
LNotification.Title := AEvent.Title;
LNotification.AlertBody := AEvent.MessageContent;
NotificationCenter.PresentNotification(LNotification);
LNotification := NotificationCenter.CreateNotification();
try
LNotification.ChannelId := FChannelId;
LNotification.Name := IntToStr(Random(1000));
LNotification.Title := AEvent.Title;
LNotification.AlertBody := AEvent.MessageContent;
NotificationCenter.PresentNotification(LNotification);
finally
LNotification.Free;
end;
finally
LNotification.Free;
if FWakeLock <> nil then
if FWakeLock.isHeld then
FWakeLock.release();
end;
end;

Expand Down
6 changes: 3 additions & 3 deletions samples/android/src/View.Main.fmx
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ object Form1: TForm1
end
end
object Text2: TText
Position.X = 96.000000000000000000
Position.Y = 527.000000000000000000
Size.Width = 193.000000000000000000
Align = Bottom
Position.Y = 593.000000000000000000
Size.Width = 385.000000000000000000
Size.Height = 48.000000000000000000
Size.PlatformDefault = False
Text = 'Demo'
Expand Down

0 comments on commit a22242d

Please sign in to comment.