Commit fb96bb2d authored by Libin Lu's avatar Libin Lu

clean up code

parent ee6f9fab
...@@ -65,14 +65,6 @@ ...@@ -65,14 +65,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
...@@ -81,6 +73,14 @@ ...@@ -81,6 +73,14 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
...@@ -89,18 +89,10 @@ ...@@ -89,18 +89,10 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.facebook.react/react-native/0.17.1/jars" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.facebook.react/react-native/0.17.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-runtime-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-verifier" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
......
...@@ -3,9 +3,6 @@ package com.evollu.react.fcm; ...@@ -3,9 +3,6 @@ package com.evollu.react.fcm;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.LifecycleEventListener;
...@@ -14,31 +11,19 @@ import com.facebook.react.bridge.ReactApplicationContext; ...@@ -14,31 +11,19 @@ import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap; import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.RemoteMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.json.*;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import org.json.JSONException;
import android.graphics.BitmapFactory; import org.json.JSONObject;
import android.content.res.Resources;
import android.app.PendingIntent; import java.util.HashMap;
import android.app.Notification; import java.util.Iterator;
import android.app.NotificationManager; import java.util.Map;
import android.media.RingtoneManager; import java.util.Set;
import android.net.Uri;
public class FCMModule extends ReactContextBaseJavaModule implements LifecycleEventListener { public class FCMModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
private final static String TAG = FCMModule.class.getCanonicalName(); private final static String TAG = FCMModule.class.getCanonicalName();
...@@ -46,13 +31,6 @@ public class FCMModule extends ReactContextBaseJavaModule implements LifecycleEv ...@@ -46,13 +31,6 @@ public class FCMModule extends ReactContextBaseJavaModule implements LifecycleEv
public FCMModule(ReactApplicationContext reactContext, Intent intent) { public FCMModule(ReactApplicationContext reactContext, Intent intent) {
super(reactContext); super(reactContext);
if (getReactApplicationContext().hasCurrentActivity()) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(reactContext);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("GcmMainActivity", getCurrentActivity().getClass().getSimpleName());
editor.apply();
}
getReactApplicationContext().addLifecycleEventListener(this); getReactApplicationContext().addLifecycleEventListener(this);
registerNotificationHandler(); registerNotificationHandler();
...@@ -106,136 +84,23 @@ public class FCMModule extends ReactContextBaseJavaModule implements LifecycleEv ...@@ -106,136 +84,23 @@ public class FCMModule extends ReactContextBaseJavaModule implements LifecycleEv
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (getReactApplicationContext().hasActiveCatalystInstance()) { if (getReactApplicationContext().hasActiveCatalystInstance()) {
WritableMap params = Arguments.createMap(); WritableMap params = Arguments.createMap();
try {
JSONObject data = new JSONObject(intent.getStringExtra("data"));
Iterator<String> keysIterator = data.keys();
while(keysIterator.hasNext()){
String key = keysIterator.next();
params.putString(key, (String) data.get(key));
}
sendEvent("FCMNotificationReceived", params);
} catch (JSONException e){
if(intent.getStringExtra("data") != null){
params.putString("data", intent.getStringExtra("data"));
} }
if(intent.getStringExtra("notification") != null){
params.putString("notification", intent.getStringExtra("notification"));
}
sendEvent("FCMNotificationReceived", params);
abortBroadcast(); abortBroadcast();
} }
} }
}, intentFilter); }, intentFilter);
} }
// @Override
// public Map<String, Object> getConstants() {
// final Map<String, Object> constants = new HashMap<>();
// if (mIntent != null) {
// Bundle bundle = mIntent.getBundleExtra("bundle");
// String bundleString = convertJSON(bundle);
// Log.d(TAG, "bundleString: " + bundleString);
// constants.put("initialNotification", bundleString);
// }
// return constants;
// }
// private void sendEvent(String eventName, Object params) {
// getReactApplicationContext()
// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
// .emit(eventName, params);
// }
// private void listenGcmRegistration() {
// IntentFilter intentFilter = new IntentFilter("RNGcmRegistrationServiceResult");
//
// getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
// @Override
// public void onReceive(Context context, Intent intent) {
// Bundle bundle = intent.getExtras();
// boolean success = bundle.getBoolean("success");
// if (success) {
// String token = bundle.getString("token");
// WritableMap params = Arguments.createMap();
// params.putString("registrationToken", token);
// registrationToken = token;
//
// sendEvent("GCMRemoteNotificationRegistered", params);
// } else {
// String message = bundle.getString("message");
// WritableMap params = Arguments.createMap();
// params.putString("error", message);
//
// sendEvent("GCMRemoteNotificationRegistered", params);
//
// }
// }
// }, intentFilter);
// }
// private String convertJSON(Bundle bundle) {
// JSONObject json = new JSONObject();
// Set<String> keys = bundle.keySet();
// for (String key : keys) {
// try {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// json.put(key, JSONObject.wrap(bundle.get(key)));
// } else {
// json.put(key, bundle.get(key));
// }
// } catch(JSONException e) {
// return null;
// }
// }
// return json.toString();
// }
// private void listenGcmReceiveNotification() {
// IntentFilter intentFilter = new IntentFilter("com.oney.gcm.GCMReceiveNotification");
//
// getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
// @Override
// public void onReceive(Context context, Intent intent) {
// Log.d(TAG, "GCMReceiveNotification BroadcastReceiver");
//
// if (getReactApplicationContext().hasActiveCatalystInstance()) {
// Bundle bundle = intent.getBundleExtra("bundle");
//
// String bundleString = convertJSON(bundle);
//
// WritableMap params = Arguments.createMap();
// params.putString("dataJSON", bundleString);
// params.putBoolean("isInForeground", mIsInForeground);
//
// sendEvent("GCMRemoteNotificationReceived", params);
// abortBroadcast();
// } else {
// }
// }
// }, intentFilter);
// }
// @ReactMethod
// public void stopService() {
// if (mIntent != null) {
// new android.os.Handler().postDelayed(new Runnable() {
// public void run() {
// getReactApplicationContext().stopService(mIntent);
// }
// }, 1000);
// }
// }
// private Class getMainActivityClass() {
// try {
// String packageName = getReactApplicationContext().getPackageName();
//
// SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getReactApplicationContext());
// String activityString = preferences.getString("GcmMainActivity", null);
// if (activityString == null) {
// Log.d(TAG, "GcmMainActivity is null");
// return null;
// } else {
// return Class.forName(packageName + "." + activityString);
// }
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// return null;
// }
// }
@Override @Override
public void onHostResume() { public void onHostResume() {
} }
......
package com.evollu.react.fcm; package com.evollu.react.fcm;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log; import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;;import org.json.JSONException; import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Map;
import java.util.Set;
public class MessagingService extends FirebaseMessagingService { public class MessagingService extends FirebaseMessagingService {
private static final String TAG = "MessagingService"; private static final String TAG = "MessagingService";
...@@ -21,42 +16,8 @@ public class MessagingService extends FirebaseMessagingService { ...@@ -21,42 +16,8 @@ public class MessagingService extends FirebaseMessagingService {
Log.d(TAG, "Remote message received"); Log.d(TAG, "Remote message received");
Intent i = new Intent("com.evollu.fcm.ReceiveNotification"); Intent i = new Intent("com.evollu.fcm.ReceiveNotification");
if(remoteMessage.getData() != null){ if(remoteMessage.getData() != null){
i.putExtra("data", convertJSON(remoteMessage.getData())); i.putExtra("data", new JSONObject(remoteMessage.getData()).toString());
} sendOrderedBroadcast(i, null);
if(remoteMessage.getNotification() != null){
i.putExtra("notification", buildNotificationJSON(remoteMessage.getNotification()));
}
sendOrderedBroadcast(i, null);
}
private String buildNotificationJSON(RemoteMessage.Notification notification){
JSONObject json = new JSONObject();
try{
json.put("title", notification.getTitle());
json.put("body", notification.getBody());
json.put("icon", notification.getIcon());
json.put("sound", notification.getSound());
json.put("color", notification.getColor());
} catch( JSONException e){
return null;
}
return json.toString();
}
private String convertJSON(Map data) {
JSONObject json = new JSONObject();
Set<String> keys = data.keySet();
for (String key : keys) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
json.put(key, JSONObject.wrap(data.get(key)));
} else {
json.put(key, data.get(key));
}
} catch(JSONException e) {
return null;
}
} }
return json.toString();
} }
} }
...@@ -20,8 +20,6 @@ NSString *const FCMNotificationReceived = @"FCMNotificationReceived"; ...@@ -20,8 +20,6 @@ NSString *const FCMNotificationReceived = @"FCMNotificationReceived";
@implementation FCMModule @implementation FCMModule
NSString* registrationToken;
RCT_EXPORT_MODULE() RCT_EXPORT_MODULE()
@synthesize bridge = _bridge; @synthesize bridge = _bridge;
...@@ -41,8 +39,6 @@ RCT_EXPORT_MODULE() ...@@ -41,8 +39,6 @@ RCT_EXPORT_MODULE()
- (void)setBridge:(RCTBridge *)bridge - (void)setBridge:(RCTBridge *)bridge
{ {
_bridge = bridge; _bridge = bridge;
[FIRApp configure];
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRemoteNotificationReceived:) selector:@selector(handleRemoteNotificationReceived:)
...@@ -100,8 +96,6 @@ RCT_EXPORT_METHOD(requestPermissions) ...@@ -100,8 +96,6 @@ RCT_EXPORT_METHOD(requestPermissions)
return; return;
} }
//UIApplication *app = RCTSharedApplication();
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7.1 or earlier // iOS 7.1 or earlier
UIRemoteNotificationType allNotificationTypes = UIRemoteNotificationType allNotificationTypes =
...@@ -126,44 +120,4 @@ RCT_EXPORT_METHOD(requestPermissions) ...@@ -126,44 +120,4 @@ RCT_EXPORT_METHOD(requestPermissions)
body:notification.userInfo]; body:notification.userInfo];
} }
//- (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
//{
// if([notification.userInfo objectForKey:@"deviceToken"] != nil){
// NSData* deviceToken = [notification.userInfo objectForKey:@"deviceToken"];
// __weak typeof(self) weakSelf = self;;
//
// NSDictionary *registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:deviceToken,
// kGGLInstanceIDAPNSServerTypeSandboxOption:@YES};
//
// NSString* gcmSenderID = [[[GGLContext sharedInstance] configuration] gcmSenderID];
//
// [[GGLInstanceID sharedInstance] tokenWithAuthorizedEntity:gcmSenderID scope:kGGLInstanceIDScopeGCM options:registrationOptions
// handler:^(NSString *token, NSError *error){
// if (token != nil) {
// NSLog(@"Registration Token: %@", token);
//
// weakSelf.connectedToGCM = YES;
// registrationToken = token;
//
// NSDictionary *userInfo = @{@"registrationToken":token};
// [_bridge.eventDispatcher sendDeviceEventWithName:GCMRemoteNotificationRegistered
// body:userInfo];
// } else {
// NSLog(@"Registration to GCM failed with error: %@", error.localizedDescription);
// NSDictionary *userInfo = @{@"error":error.localizedDescription};
// [_bridge.eventDispatcher sendDeviceEventWithName:GCMRemoteNotificationRegistered
// body:userInfo];
// }
// }];
// } else {
// [_bridge.eventDispatcher sendDeviceEventWithName:GCMRemoteNotificationRegistered
// body:notification.userInfo];
// }
//
//}
//-(void)onTokenRefresh {
// [self requestPermissions];
//}
@end @end
{ {
"name": "react-native-gcm-push-notification", "name": "react-native-fcm",
"version": "1.0.3", "version": "1.0.0",
"description": "React Native component for Google Cloud Messaging for IOS and Android", "description": "React Native component for FCM for IOS and Android",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/evollu/react-native-gcm-push-notification.git" "url": "git+https://github.com/evollu/react-native-fcm.git"
}, },
"keywords": [ "keywords": [
"React-Native", "React-Native",
"ios", "ios",
"android", "android",
"gcm" "fcm",
"firebase",
"cloud messaging"
], ],
"author": "Libin Lu", "author": "Libin Lu",
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
"url": "https://github.com/evollu/react-native-gcm-push-notification/issues" "url": "https://github.com/evollu/react-native-fcm/issues"
}, },
"homepage": "https://github.com/evollu/react-native-gcm-push-notification#readme" "homepage": "https://github.com/evollu/react-native-fcm"
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment