[messaging][android] Bit of tidy up of Android messaging; Update messaging event names to match iOS

This commit is contained in:
Chris Bianca 2017-05-30 09:52:04 +01:00
parent abc4de2257
commit 0a645c2261
6 changed files with 48 additions and 51 deletions

View File

@ -39,11 +39,11 @@ public class Utils {
/**
* send a JS event
**/
public static void sendEvent(final ReactContext context, final String eventName, final WritableMap params) {
public static void sendEvent(final ReactContext context, final String eventName, Object body) {
if (context != null) {
context
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
.emit(eventName, body);
} else {
Log.d(TAG, "Missing context - cannot send event!");
}

View File

@ -29,7 +29,11 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* com.facebook.internal is solely for the use of other packages within the Facebook SDK for
@ -41,7 +45,7 @@ import java.util.*;
* If other types are found, an IllegalArgumentException is thrown.
*/
public class BundleJSONConverter {
private static final Map<Class<?>, Setter> SETTERS = new HashMap<Class<?>, Setter>();
private static final Map<Class<?>, Setter> SETTERS = new HashMap<>();
static {
SETTERS.put(Boolean.class, new Setter() {

View File

@ -16,7 +16,6 @@ public class InstanceIdService extends FirebaseInstanceIdService {
* the previous token had been compromised. This call is initiated by the
* InstanceID provider.
*/
// [START refresh_token]
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
@ -24,7 +23,6 @@ public class InstanceIdService extends FirebaseInstanceIdService {
Log.d(TAG, "Refreshed token: " + refreshedToken);
// Broadcast refreshed token
Intent i = new Intent("io.invertase.firebase.messaging.FCMRefreshToken");
Bundle bundle = new Bundle();
bundle.putString("token", refreshedToken);

View File

@ -25,7 +25,7 @@ public class MessagingService extends FirebaseMessagingService {
sendOrderedBroadcast(i, null);
}
public void handleBadge(RemoteMessage remoteMessage) {
private void handleBadge(RemoteMessage remoteMessage) {
BadgeHelper badgeHelper = new BadgeHelper(this);
if (remoteMessage.getData() == null) {
return;
@ -44,7 +44,7 @@ public class MessagingService extends FirebaseMessagingService {
}
}
public void buildLocalNotification(RemoteMessage remoteMessage) {
private void buildLocalNotification(RemoteMessage remoteMessage) {
if(remoteMessage.getData() == null){
return;
}

View File

@ -1,6 +1,10 @@
package io.invertase.firebase.messaging;
import android.app.*;
import android.app.AlarmManager;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
@ -14,7 +18,6 @@ import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.util.Patterns;
import android.content.SharedPreferences;
import org.json.JSONException;
import org.json.JSONObject;
@ -36,23 +39,7 @@ public class RNFirebaseLocalMessagingHelper {
public RNFirebaseLocalMessagingHelper(Application context) {
mContext = context;
sharedPreferences = (SharedPreferences) mContext.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE);
}
public Class getMainActivityClass() {
String packageName = mContext.getPackageName();
Intent launchIntent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
private AlarmManager getAlarmManager() {
return (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
sharedPreferences = mContext.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE);
}
public void sendNotification(Bundle bundle) {
@ -292,12 +279,6 @@ public class RNFirebaseLocalMessagingHelper {
notificationManager.cancelAll();
}
public void cancelAlarm(String notificationId) {
Intent notificationIntent = new Intent(mContext, RNFirebaseLocalMessagingPublisher.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, notificationId.hashCode(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
getAlarmManager().cancel(pendingIntent);
}
public ArrayList<Bundle> getScheduledLocalNotifications(){
ArrayList<Bundle> array = new ArrayList<Bundle>();
java.util.Map<String, ?> keyMap = sharedPreferences.getAll();
@ -317,7 +298,29 @@ public class RNFirebaseLocalMessagingHelper {
mIsForeground = foreground;
}
public Bitmap getBitmapFromURL(String strURL) {
private Class getMainActivityClass() {
String packageName = mContext.getPackageName();
Intent launchIntent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
private AlarmManager getAlarmManager() {
return (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
}
private void cancelAlarm(String notificationId) {
Intent notificationIntent = new Intent(mContext, RNFirebaseLocalMessagingPublisher.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, notificationId.hashCode(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
getAlarmManager().cancel(pendingIntent);
}
private Bitmap getBitmapFromURL(String strURL) {
try {
URL url = new URL(strURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

View File

@ -1,9 +1,13 @@
package io.invertase.firebase.messaging;
import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
@ -16,22 +20,16 @@ import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
import com.google.firebase.messaging.RemoteMessage.Notification;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;
import android.content.Context;
import io.invertase.firebase.Utils;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public class RNFirebaseMessaging extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener {
private final static String TAG = RNFirebaseMessaging.class.getCanonicalName();
@ -136,12 +134,6 @@ public class RNFirebaseMessaging extends ReactContextBaseJavaModule implements L
promise.resolve(mBadgeHelper.getBadgeCount());
}
private void sendEvent(String eventName, Object params) {
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
private void registerTokenRefreshHandler() {
IntentFilter intentFilter = new IntentFilter("io.invertase.firebase.messaging.FCMRefreshToken");
getReactApplicationContext().registerReceiver(new BroadcastReceiver() {
@ -149,7 +141,7 @@ public class RNFirebaseMessaging extends ReactContextBaseJavaModule implements L
public void onReceive(Context context, Intent intent) {
if (getReactApplicationContext().hasActiveCatalystInstance()) {
String token = intent.getStringExtra("token");
sendEvent("FCMTokenRefreshed", token);
Utils.sendEvent(getReactApplicationContext(), "messaging_token_refreshed", token);
}
}
}, intentFilter);
@ -211,7 +203,7 @@ public class RNFirebaseMessaging extends ReactContextBaseJavaModule implements L
params.putString(key, data.get(key));
}
}
sendEvent("FCMNotificationReceived", params);
Utils.sendEvent(getReactApplicationContext(), "messaging_notification_received", params);
}
}
@ -225,7 +217,7 @@ public class RNFirebaseMessaging extends ReactContextBaseJavaModule implements L
@Override
public void onReceive(Context context, Intent intent) {
if (getReactApplicationContext().hasActiveCatalystInstance()) {
sendEvent("FCMNotificationReceived", Arguments.fromBundle(intent.getExtras()));
Utils.sendEvent(getReactApplicationContext(), "messaging_notification_received", Arguments.fromBundle(intent.getExtras()));
}
}
}, intentFilter);
@ -275,6 +267,6 @@ public class RNFirebaseMessaging extends ReactContextBaseJavaModule implements L
@Override
public void onNewIntent(Intent intent) {
// todo hmm?
sendEvent("FCMNotificationReceived", parseIntent(intent));
Utils.sendEvent(getReactApplicationContext(), "messaging_notification_received", parseIntent(intent));
}
}