diff --git a/android/NotificationHelper.java b/android/NotificationHelper.java index a2b81d0..0e1335b 100644 --- a/android/NotificationHelper.java +++ b/android/NotificationHelper.java @@ -9,20 +9,16 @@ import android.graphics.Bitmap; import android.graphics.drawable.Icon; public class NotificationHelper { - public static NotificationChannel newChannel(Context ctx) { - String tag = "NotificationHelper"; + private final static String tag = "NotificationHelper"; + public static void newChannel(Context ctx, String channelID, String name, String description) { 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); + NotificationChannel channel = new NotificationChannel(channelID, 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 acb8202..43fba09 100644 --- a/android/notify_android.go +++ b/android/notify_android.go @@ -2,18 +2,16 @@ package android import ( "fmt" - "runtime" "gioui.org/app" "github.com/tailscale/tailscale-android/jni" ) type NotificationChannel struct { - javaObj jni.Object + id string } -func NewChannel(name, description string) (*NotificationChannel, error) { - var channel jni.Object +func NewChannel(id, name, description string) (*NotificationChannel, error) { if err := jni.Do(jni.JVMFor(app.JavaVM()), func(env jni.Env) error { appCtx := jni.Object(app.AppContext()) classLoader := jni.ClassLoaderFor(env, appCtx) @@ -21,26 +19,20 @@ func NewChannel(name, description string) (*NotificationChannel, error) { if err != nil { return err } - 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())) + newChannelMethod := jni.GetStaticMethodID(env, notifyClass, "newChannel", "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V") + jname := jni.Value(jni.JavaString(env, name)) + jdescription := jni.Value(jni.JavaString(env, description)) + jID := jni.Value(jni.JavaString(env, id)) + err = jni.CallStaticVoidMethod(env, notifyClass, newChannelMethod, jni.Value(app.AppContext()), jID, jname, jdescription) if err != nil { return err } - channel = jni.NewGlobalRef(env, channel) return nil }); err != nil { return nil, fmt.Errorf("failed creating notification channel: %w", err) } nc := &NotificationChannel{ - javaObj: channel, + id: id, } - 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 5459f00..c9e3a6f 100644 --- a/example/hello.go +++ b/example/hello.go @@ -31,7 +31,7 @@ func main() { } }() go func() { - channel, err := android.NewChannel("hello", "description") + channel, err := android.NewChannel("CHANNEL", "hello", "description") if err != nil { log.Printf("channel creation failed: %v", err) }