github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/notif/notif.go (about)

     1  package notif
     2  
     3  /*
     4  #cgo LDFLAGS: -llog -landroid
     5  #include <android/log.h>
     6  #include <jni.h>
     7  #include <stdlib.h>
     8  
     9  #define LOG_FATAL(...) __android_log_print(ANDROID_LOG_FATAL, "Go/fatal", __VA_ARGS__)
    10  #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, "Go/info", __VA_ARGS__)
    11  
    12  void notif_manager_init(void* java_vm, void* ctx, char* title, char* text) {
    13  	JavaVM* vm = (JavaVM*)(java_vm);
    14  	JNIEnv* env;
    15  	int err;
    16  	int attached = 0;
    17  
    18  	err = (*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6);
    19  	if (err != JNI_OK) {
    20  		if (err == JNI_EDETACHED) {
    21  			if ((*vm)->AttachCurrentThread(vm, &env, 0) != 0) {
    22  				LOG_FATAL("cannot attach JVM");
    23  			}
    24  			attached = 1;
    25  		} else {
    26  			LOG_FATAL("GetEnv unexpected error: %d", err);
    27  		}
    28  	}
    29  		
    30  	jstring javaTitle = (jstring)(*env)->NewStringUTF(env, (const char *)title);
    31  	jstring javaText = (jstring)(*env)->NewStringUTF(env, (const char *)text);
    32  		
    33  	jclass cls = (*env)->GetObjectClass(env, ctx);
    34  	jmethodID nJmethodID = (*env)->GetMethodID(env, cls, "notif", "(Ljava/lang/String;Ljava/lang/String;)V");
    35  	(jstring)(*env)->CallObjectMethod(env, ctx, nJmethodID, javaTitle, javaText);
    36  		
    37  
    38  	if (attached) {
    39  		(*vm)->DetachCurrentThread(vm);
    40  	}
    41  }
    42  */
    43  import "C"
    44  import (
    45  	"github.com/c-darwin/mobile/internal/mobileinit"
    46  )
    47  
    48  
    49  func SendNotif(title string, text string) {
    50  	ctx := mobileinit.Context{}
    51  	C.notif_manager_init(ctx.JavaVM(), ctx.AndroidContext(), C.CString(title), C.CString(text))
    52  }
    53  
    54