diff --git a/android/NotificationHelper.java b/android/NotificationHelper.java index ed04040..a2b81d0 100644 --- a/android/NotificationHelper.java +++ b/android/NotificationHelper.java @@ -1,5 +1,6 @@ package ht.sr.git.whereswaldon.niotify; +import android.content.Context; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; @@ -8,7 +9,20 @@ import android.graphics.Bitmap; import android.graphics.drawable.Icon; public class NotificationHelper { - public static void newChannel() { - Log.w("NotificationHelper",String.format("newChannel invoked")); + public static NotificationChannel newChannel(Context ctx) { + String tag = "NotificationHelper"; + Log.w(tag,String.format("newChannel invoked")); + String CHANNEL_ID = "CHANNEL_ID"; + CharSequence name = "notification_channel_name"; + String description = "notification_channel_description"; + int importance = NotificationManager.IMPORTANCE_DEFAULT; + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); + Log.e(tag,String.format("channel: %s",channel)); + channel.setDescription(description); + + NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class); + Log.e(tag,String.format("manager: %s",notificationManager)); + notificationManager.createNotificationChannel(channel); + return channel; } } diff --git a/android/notify_android.go b/android/notify_android.go index 1112b99..acb8202 100644 --- a/android/notify_android.go +++ b/android/notify_android.go @@ -2,27 +2,45 @@ package android import ( "fmt" + "runtime" "gioui.org/app" "github.com/tailscale/tailscale-android/jni" ) type NotificationChannel struct { + javaObj jni.Object } -func NewChannel(jvm uintptr, name, description string) (*NotificationChannel, error) { - if err := jni.Do(jni.JVMFor(jvm), func(env jni.Env) error { - classLoader := jni.ClassLoaderFor(env, jni.Object(app.AppContext())) +func NewChannel(name, description string) (*NotificationChannel, error) { + var channel jni.Object + 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, "newChannel", "()V") - jname := jni.Value(jni.JavaString(env, name)) - jdescription := jni.Value(jni.JavaString(env, description)) - return jni.CallStaticVoidMethod(env, notifyClass, newChannelMethod, jname, jdescription) + newChannelMethod := jni.GetStaticMethodID(env, notifyClass, "newChannel", "(Landroid/content/Context;)Landroid/app/NotificationChannel;") + // jname := jni.Value(jni.JavaString(env, name)) + // jdescription := jni.Value(jni.JavaString(env, description)) + channel, err = jni.CallStaticObjectMethod(env, notifyClass, newChannelMethod, jni.Value(app.AppContext())) + if err != nil { + return err + } + channel = jni.NewGlobalRef(env, channel) + return nil }); err != nil { return nil, fmt.Errorf("failed creating notification channel: %w", err) } - return &NotificationChannel{}, nil + nc := &NotificationChannel{ + javaObj: channel, + } + runtime.SetFinalizer(nc, func(obj *NotificationChannel) { + _ = jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error { + jni.DeleteGlobalRef(env, obj.javaObj) + return nil + }) + }) + return nc, nil } diff --git a/example/hello.go b/example/hello.go index 81ff0b7..5459f00 100644 --- a/example/hello.go +++ b/example/hello.go @@ -31,7 +31,7 @@ func main() { } }() go func() { - channel, err := android.NewChannel(app.JavaVM(), "hello", "description") + channel, err := android.NewChannel("hello", "description") if err != nil { log.Printf("channel creation failed: %v", err) }