Skip to content

Instantly share code, notes, and snippets.

@renganatha10
Created February 22, 2017 17:54
Show Gist options
  • Save renganatha10/96ce8c845d981008d2a76da756954b25 to your computer and use it in GitHub Desktop.
Save renganatha10/96ce8c845d981008d2a76da756954b25 to your computer and use it in GitHub Desktop.
Set of Example for Bridging android native Modules in React Native
package com.musicapp;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Environment;
import android.support.v4.app.RemoteInput;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.appcompat.R;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.uimanager.annotations.ReactProp;
import java.io.File;
public class MusicManager extends ReactContextBaseJavaModule implements LifecycleEventListener {
private ResponseReceiver receiver;
private int notificationId;
private static final String KEY_TEXT_REPLY = "key_text_reply";
public MusicManager(ReactApplicationContext reactContext) {
super(reactContext);
this.notificationId = 1;
}
@Override
public String getName() {
return "MusicAndroid";
}
@Override
public void initialize() {
receiver = new ResponseReceiver();
getReactApplicationContext().addLifecycleEventListener(this);
}
@ReactMethod
public void startService() {
Intent intent = new Intent(this.getCurrentActivity(), MusicService.class);
intent.putExtra(MusicService.PARAM_IN_MSG, "Renga");
intent
this.getCurrentActivity().startService(intent);
}
@ReactMethod
public void stopService() {
Intent intent = new Intent(this.getCurrentActivity(), MusicService.class);
this.getCurrentActivity().stopService(intent);
}
@ReactMethod
public void listDirectory(int tag,Callback successCallBack, Callback failureCallBack){
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
File directory = new File(path);
File[] files = directory.listFiles();
WritableArray array = Arguments.createArray();
for (int i = 0; i < files.length; i++)
{
String fileName = "FileName:" + files[i].toURI();
Log.d("Files",fileName);
array.pushString(fileName);
}
array.pushInt(10);
array.pushInt(10);
array.pushInt(10);
array.pushInt(10);
if(tag == 10) {
successCallBack.invoke(array);
} else {
failureCallBack.invoke("somehing");
}
}
public int getId() {
return this.notificationId++;
}
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
@ReactMethod
public void sendNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this.getCurrentActivity());
mBuilder.setSmallIcon(R.drawable.notification_template_icon_bg);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!").setNumber(getId());
Intent notificationIntent = new Intent(this.getCurrentActivity(), MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getCurrentActivity(), 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
.setLabel("Reply")
.build();
NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.notification_template_icon_bg,"Reply", contentIntent).addRemoteInput(remoteInput).build();
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
inboxStyle.setBigContentTitle("Event tracker details:");
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
mBuilder.setStyle(inboxStyle);
mBuilder.addAction(action);
NotificationManager mNotificationManager = (NotificationManager) getReactApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(this.getId(), mBuilder.build());
}
@Override
public void onHostResume() {
IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
filter.addCategory(Intent.CATEGORY_DEFAULT);
this.getReactApplicationContext().registerReceiver(receiver, filter);
}
@Override
public void onHostPause() {
this.getReactApplicationContext().unregisterReceiver(receiver);
}
@Override
public void onHostDestroy() {
}
public class ResponseReceiver extends BroadcastReceiver {
public static final String ACTION_RESP = "com.musicapp.intent.action.MESSAGE_PROCESSED";
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("ON broad Cast Received");
String resultTxt = intent.getStringExtra(MusicService.PARAM_OUT_MSG);
Toast.makeText(getReactApplicationContext(), resultTxt, Toast.LENGTH_SHORT).show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment