feat: support actually sending notifications
This commit is contained in:
parent
420149d9e9
commit
4c377a2f20
|
@ -21,4 +21,14 @@ public class NotificationHelper {
|
||||||
Log.e(tag,String.format("manager: %s",notificationManager));
|
Log.e(tag,String.format("manager: %s",notificationManager));
|
||||||
notificationManager.createNotificationChannel(channel);
|
notificationManager.createNotificationChannel(channel);
|
||||||
}
|
}
|
||||||
|
public static void sendNotification(Context ctx, String channelID, int notificationID, String title, String text) {
|
||||||
|
Notification.Builder builder = new Notification.Builder(ctx, channelID)
|
||||||
|
.setContentTitle(title)
|
||||||
|
.setSmallIcon(Icon.createWithBitmap(Bitmap.createBitmap(1,1,Bitmap.Config.ALPHA_8)))
|
||||||
|
.setContentText(text)
|
||||||
|
.setPriority(Notification.PRIORITY_DEFAULT);
|
||||||
|
|
||||||
|
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
|
||||||
|
notificationManager.notify(notificationID, builder.build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,25 @@ package android
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"gioui.org/app"
|
"gioui.org/app"
|
||||||
"github.com/tailscale/tailscale-android/jni"
|
"github.com/tailscale/tailscale-android/jni"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
idlock sync.Mutex
|
||||||
|
nextNotificationID int32
|
||||||
|
)
|
||||||
|
|
||||||
|
func nextID() int32 {
|
||||||
|
idlock.Lock()
|
||||||
|
defer idlock.Unlock()
|
||||||
|
id := nextNotificationID
|
||||||
|
nextNotificationID++
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
type NotificationChannel struct {
|
type NotificationChannel struct {
|
||||||
id string
|
id string
|
||||||
}
|
}
|
||||||
|
@ -36,3 +50,33 @@ func NewChannel(id, name, description string) (*NotificationChannel, error) {
|
||||||
}
|
}
|
||||||
return nc, nil
|
return nc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Notification struct {
|
||||||
|
id int32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nc *NotificationChannel) Send(title, text string) (*Notification, error) {
|
||||||
|
notificationID := nextID()
|
||||||
|
if err := jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error {
|
||||||
|
appCtx := jni.Object(app.AppContext())
|
||||||
|
classLoader := jni.ClassLoaderFor(env, appCtx)
|
||||||
|
notifyClass, err := jni.LoadClass(env, classLoader, "ht/sr/git/whereswaldon/niotify/NotificationHelper")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
newChannelMethod := jni.GetStaticMethodID(env, notifyClass, "sendNotification", "(Landroid/content/Context;Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)V")
|
||||||
|
jtitle := jni.Value(jni.JavaString(env, title))
|
||||||
|
jtext := jni.Value(jni.JavaString(env, text))
|
||||||
|
jID := jni.Value(jni.JavaString(env, nc.id))
|
||||||
|
err = jni.CallStaticVoidMethod(env, notifyClass, newChannelMethod, jni.Value(app.AppContext()), jID, jni.Value(notificationID), jtitle, jtext)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed creating notification channel: %w", err)
|
||||||
|
}
|
||||||
|
return &Notification{
|
||||||
|
id: notificationID,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
|
|
||||||
//go:generate javac -target 1.8 -source 1.8 -bootclasspath $ANDROID_HOME/platforms/android-29/android.jar ../android/NotificationHelper.java
|
//go:generate javac -target 1.8 -source 1.8 -bootclasspath $ANDROID_HOME/platforms/android-29/android.jar ../android/NotificationHelper.java
|
||||||
//go:generate jar cf NotificationHelper.jar ../android/NotificationHelper.class
|
//go:generate jar cf NotificationHelper.jar ../android/NotificationHelper.class
|
||||||
//go:generate rm ../android/NotificationHelper.class
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -30,19 +29,13 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
go func() {
|
|
||||||
channel, err := android.NewChannel("CHANNEL", "hello", "description")
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("channel creation failed: %v", err)
|
|
||||||
}
|
|
||||||
log.Println(channel)
|
|
||||||
}()
|
|
||||||
app.Main()
|
app.Main()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loop(w *app.Window) error {
|
func loop(w *app.Window) error {
|
||||||
th := material.NewTheme(gofont.Collection())
|
th := material.NewTheme(gofont.Collection())
|
||||||
var ops op.Ops
|
var ops op.Ops
|
||||||
|
first := true
|
||||||
for {
|
for {
|
||||||
e := <-w.Events()
|
e := <-w.Events()
|
||||||
switch e := e.(type) {
|
switch e := e.(type) {
|
||||||
|
@ -56,6 +49,21 @@ func loop(w *app.Window) error {
|
||||||
l.Alignment = text.Middle
|
l.Alignment = text.Middle
|
||||||
l.Layout(gtx)
|
l.Layout(gtx)
|
||||||
e.Frame(gtx.Ops)
|
e.Frame(gtx.Ops)
|
||||||
|
if first {
|
||||||
|
first = false
|
||||||
|
go func() {
|
||||||
|
channel, err := android.NewChannel("CHANNEL", "hello", "description")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("channel creation failed: %v", err)
|
||||||
|
}
|
||||||
|
log.Println(channel)
|
||||||
|
notif, err := channel.Send("hello!", "IS GIO OUT THERE?")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("notification send failed: %v", err)
|
||||||
|
}
|
||||||
|
log.Println(notif)
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user