From fb96bb2d37864a1157f15f25f3fb44300b93f105 Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Sun, 29 May 2016 11:32:17 -0400 Subject: [PATCH] clean up code --- android/react-native-fcm.iml | 24 +-- .../java/com/evollu/react/fcm/FCMModule.java | 167 ++---------------- .../evollu/react/fcm/MessagingService.java | 45 +---- ios/FCMModule.m | 46 ----- package.json | 19 +- 5 files changed, 36 insertions(+), 265 deletions(-) diff --git a/android/react-native-fcm.iml b/android/react-native-fcm.iml index 12a2cf5..070a3d8 100644 --- a/android/react-native-fcm.iml +++ b/android/react-native-fcm.iml @@ -65,14 +65,6 @@ - - - - - - - - @@ -81,6 +73,14 @@ + + + + + + + + @@ -89,18 +89,10 @@ - - - - - - - - diff --git a/android/src/main/java/com/evollu/react/fcm/FCMModule.java b/android/src/main/java/com/evollu/react/fcm/FCMModule.java index 459ffb2..59111c0 100644 --- a/android/src/main/java/com/evollu/react/fcm/FCMModule.java +++ b/android/src/main/java/com/evollu/react/fcm/FCMModule.java @@ -3,9 +3,6 @@ package com.evollu.react.fcm; import android.content.BroadcastReceiver; import android.content.Intent; 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.LifecycleEventListener; @@ -14,31 +11,19 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.WritableMap; -import com.facebook.react.bridge.ReadableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; 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.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.content.res.Resources; -import android.app.PendingIntent; -import android.app.Notification; -import android.app.NotificationManager; -import android.media.RingtoneManager; -import android.net.Uri; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; public class FCMModule extends ReactContextBaseJavaModule implements LifecycleEventListener { private final static String TAG = FCMModule.class.getCanonicalName(); @@ -46,13 +31,6 @@ public class FCMModule extends ReactContextBaseJavaModule implements LifecycleEv public FCMModule(ReactApplicationContext reactContext, Intent intent) { 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); registerNotificationHandler(); @@ -106,136 +84,23 @@ public class FCMModule extends ReactContextBaseJavaModule implements LifecycleEv public void onReceive(Context context, Intent intent) { if (getReactApplicationContext().hasActiveCatalystInstance()) { WritableMap params = Arguments.createMap(); + try { + JSONObject data = new JSONObject(intent.getStringExtra("data")); + Iterator 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(); } } }, intentFilter); } -// @Override -// public Map getConstants() { -// final Map 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 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 public void onHostResume() { } diff --git a/android/src/main/java/com/evollu/react/fcm/MessagingService.java b/android/src/main/java/com/evollu/react/fcm/MessagingService.java index ece810a..ff57dc3 100644 --- a/android/src/main/java/com/evollu/react/fcm/MessagingService.java +++ b/android/src/main/java/com/evollu/react/fcm/MessagingService.java @@ -1,17 +1,12 @@ package com.evollu.react.fcm; import android.content.Intent; -import android.os.Build; -import android.os.Bundle; import android.util.Log; 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 java.util.Map; -import java.util.Set; - public class MessagingService extends FirebaseMessagingService { private static final String TAG = "MessagingService"; @@ -21,42 +16,8 @@ public class MessagingService extends FirebaseMessagingService { Log.d(TAG, "Remote message received"); Intent i = new Intent("com.evollu.fcm.ReceiveNotification"); if(remoteMessage.getData() != null){ - i.putExtra("data", convertJSON(remoteMessage.getData())); - } - 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 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; - } + i.putExtra("data", new JSONObject(remoteMessage.getData()).toString()); + sendOrderedBroadcast(i, null); } - return json.toString(); } } diff --git a/ios/FCMModule.m b/ios/FCMModule.m index 7bca8ff..c12fe0f 100644 --- a/ios/FCMModule.m +++ b/ios/FCMModule.m @@ -20,8 +20,6 @@ NSString *const FCMNotificationReceived = @"FCMNotificationReceived"; @implementation FCMModule -NSString* registrationToken; - RCT_EXPORT_MODULE() @synthesize bridge = _bridge; @@ -41,8 +39,6 @@ RCT_EXPORT_MODULE() - (void)setBridge:(RCTBridge *)bridge { _bridge = bridge; - - [FIRApp configure]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRemoteNotificationReceived:) @@ -100,8 +96,6 @@ RCT_EXPORT_METHOD(requestPermissions) return; } - //UIApplication *app = RCTSharedApplication(); - if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { // iOS 7.1 or earlier UIRemoteNotificationType allNotificationTypes = @@ -126,44 +120,4 @@ RCT_EXPORT_METHOD(requestPermissions) 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 diff --git a/package.json b/package.json index fb56d73..570041e 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,23 @@ { - "name": "react-native-gcm-push-notification", - "version": "1.0.3", - "description": "React Native component for Google Cloud Messaging for IOS and Android", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, + "name": "react-native-fcm", + "version": "1.0.0", + "description": "React Native component for FCM for IOS and Android", "repository": { "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": [ "React-Native", "ios", "android", - "gcm" + "fcm", + "firebase", + "cloud messaging" ], "author": "Libin Lu", "license": "MIT", "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" } -- 2.26.2