app, cmd/gogio: add android foreground permission and service#67
app, cmd/gogio: add android foreground permission and service#67mixmasala wants to merge 1 commit intogioui:mainfrom
Conversation
|
I've tested this with my application; but feel that there could be better integration with the notification library gioui.org/x/notify, so that the foreground service can share the same notification channels, in particular so that the text of the background notification can be updated. I'm not sure that this change should depend on that package though, but I think that the Service class needs to be declared in the application manifest. Any help resolving/improving the above details is very much appreciated. |
|
Nice work, thank you.
Why share notifications with niotify? This change is for foreground work, niotify is for notifications. However, see my review comments for a suggestion to customize foreground notification content text. |
|
Oh, and remember to sign-off your change and squash commits. |
well, the foreground service requires a notification - the code here just sets a static string which isn't very appealing. Android applications using foreground services tend to have some kind of content displayed in the service notification and it would be useful to have a clean way to interact with the notification - possibly the StartForeground method should return or accept this as an argument - and this is where it starts to overlap with the functionality provided by niotify. |
|
edit: fixed |
596a0f2 to
87a7c2f
Compare
|
I've refactored this PR to address issues raised above and changed the API to include setting the notification ID, title, and text. |
87a7c2f to
d295b22
Compare
c65a7be to
917b1da
Compare
|
edit: stopped using startForegroundService because I saw some crash with resuming the application from background with same error as: transistorsoft/nativescript-background-geolocation-lt#121 (comment) . Instead startService is used, which fixes this crash. |
206d76d to
37f30b7
Compare
eliasnaur
left a comment
There was a problem hiding this comment.
Leaving a few review comments for your follow-ups.
f0f3c2a to
84b41ef
Compare
84b41ef to
44a59e2
Compare
|
For some reason github wont let me comment on the change requests directly any more. I moved code out of gioui.org/app/permissions, but I was not able to find a solution to check that gioui.org/app/permissions/foreground was imported when built. For example, I thought that BuildInfo might have package-level resolution, but it does not: |
e2f1cfa to
c517124
Compare
I don't think you need to check for the import. It should be enough to detect the Java exception/error when trying to open a foreground notification. |
Good Idea. I tried to catch java.lang.SecurityException, but the exception is not raised to the caller of android.startForegroundService, as it is thrown in the method onStartCommand which is called by the android environment after the service is started. Do you have any ideas how to handle this? |
One idea is to make |
7fd3b23 to
079448b
Compare
eliasnaur
left a comment
There was a problem hiding this comment.
I somehow failed to send this review out after writing it. I apologize.
|
|
||
| static Intent startForegroundService(Context ctx, String title, String text) throws ClassNotFoundException { | ||
| Intent intent = new Intent(); | ||
| intent.setClass(ctx, ctx.getClassLoader().loadClass("org/gioui/GioForegroundService")); |
There was a problem hiding this comment.
There must be more to the error than what you pasted. A stack trace or similar that can tell us why the class cannot be loaded.
app/os_android.go
Outdated
| foregroundService.intent = C.jni_NewGlobalRef(env, foregroundService.intent) | ||
|
|
||
| } else { | ||
| panic(err) |
There was a problem hiding this comment.
You should propagate this error to the caller. In particular when the XXX issue above is resolved so that startForegroundService can fail for configuration reasons.
app/os_android.go
Outdated
| // Start starts the foreground service | ||
| func Start(title, text string) (stop func(), err error) { |
There was a problem hiding this comment.
The documentation needs expanding. To make the documentation visible for everyone, rename this function startForeground and add a documented StartForeground in package app that calls it.
app/os_nandroid.go
Outdated
| package app | ||
|
|
||
| // Start is a no-op on platforms other than android | ||
| func Start(title, text string) (stop func(), err error) { |
There was a problem hiding this comment.
As described above, rename this function to startForeground.
079448b to
1c97118
Compare
5a91e41 to
a1625b7
Compare
0d543a0 to
1686874
Compare
67c77c9 to
46cc311
Compare
f8029f2 to
026d3f9
Compare
4759601 to
f676fd2
Compare
This adds the permission android.permission.FOREGROUND_SERVICE and adds GioForegroundService which creates the tray Notification necessary to implement the Foreground Service. This adds the method Start to package app, which on android, notifies the system that the program will perform background work and that it shouldn't be killed. The foreground service is stopped using the cancel function returned by Start(). If multiple calls to Start are made, the foreground service will not be stopped until the final cancel function has been called. See https://developer.android.com/guide/components/foreground-services and https://developer.android.com/training/notify-user/build-notification Signed-off-by: Masala <masala@riseup.net>
|
Is there any progress on this? Write Android applications in pure Go by adding services and BroadcastReceiver support. |
This adds the permission android.permission.FOREGROUND_SERVICE and adds
GioForegroundService which creates the tray Notification necessary to
implement the Foreground Service. The package foreground includes the method
StartForeground, which on android, notifies the system that the program
will perform background work and that it shouldn't be killed. It returns
a channel that should be closed when the background work is complete.
See https://developer.android.com/guide/components/foreground-services
and https://developer.android.com/training/notify-user/build-notification