Skip to content

Commit

Permalink
Merge pull request #130 from CatalystCode/phongcao/mixer-improvements
Browse files Browse the repository at this point in the history
Integrate new features and improvements from Mixer
  • Loading branch information
phongcao authored Mar 21, 2020
2 parents 2c44b56 + 75593a5 commit 6518a1f
Show file tree
Hide file tree
Showing 27 changed files with 1,818 additions and 417 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public final class ReactNativeConstants {
public static final String KEY_REGISTRATION_CHANNELSHOWBADGE = "channelShowBadge";
public static final String KEY_REGISTRATION_CHANNELENABLELIGHTS = "channelEnableLights";
public static final String KEY_REGISTRATION_CHANNELENABLEVIBRATION = "channelEnableVibration";
public static final String KEY_REGISTRATION_TEMPLATENAME = "templateName";
public static final String KEY_REGISTRATION_TEMPLATE = "template";
public static final String KEY_REGISTRATION_ISTEMPLATE = "isTemplate";

// Shared prefs used in NotificationHubUtil
public static final String SHARED_PREFS_NAME = "com.azure.reactnative.notificationhub.NotificationHubUtil";
Expand All @@ -36,6 +39,10 @@ public final class ReactNativeConstants {
public static final String KEY_FOR_PREFS_CHANNELSHOWBADGE = "AzureNotificationHub_channelShowBadge";
public static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights";
public static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration";
public static final String KEY_FOR_PREFS_TEMPLATENAME = "AzureNotificationHub_templateName";
public static final String KEY_FOR_PREFS_TEMPLATE = "AzureNotificationHub_template";
public static final String KEY_FOR_PREFS_ISTEMPLATE = "AzureNotificationHub_isTemplate";
public static final String KEY_FOR_PREFS_UUID = "AzureNotificationHub_UUID";

// Remote notification payload
public static final String KEY_REMOTE_NOTIFICATION_MESSAGE = "message";
Expand Down Expand Up @@ -85,6 +92,10 @@ public final class ReactNativeConstants {
public static final String RESOURCE_NAME_NOTIFICATION = "ic_notification";
public static final String RESOURCE_NAME_LAUNCHER = "ic_launcher";

// Promise
public static final String KEY_PROMISE_RESOLVE_UUID = "uuid";
public static final String AZURE_NOTIFICATION_HUB_UNREGISTERED = "Unregistered successfully";

// Errors
public static final String ERROR_NO_ACTIVITY_CLASS = "No activity class found for the notification";
public static final String ERROR_NO_MESSAGE = "No message specified for the notification";
Expand All @@ -96,13 +107,21 @@ public final class ReactNativeConstants {
public static final String ERROR_INVALID_CONNECTION_STRING = "Connection string cannot be null.";
public static final String ERROR_INVALID_HUBNAME = "Hub name cannot be null.";
public static final String ERROR_INVALID_SENDER_ID = "Sender ID cannot be null.";
public static final String ERROR_INVALID_TEMPLATE_NAME = "Template Name cannot be null.";
public static final String ERROR_INVALID_TEMPLATE = "Template cannot be null.";
public static final String ERROR_PLAY_SERVICES = "E_PLAY_SERVICES";
public static final String ERROR_PLAY_SERVICES_DISABLED = "User must enable Google Play Services.";
public static final String ERROR_PLAY_SERVICES_UNSUPPORTED = "This device is not supported by Google Play Services.";
public static final String ERROR_NOTIFICATION_HUB = "E_NOTIFICATION_HUB";
public static final String ERROR_NOT_REGISTERED = "E_NOT_REGISTERED";
public static final String ERROR_NOT_REGISTERED_DESC = "No registration to Azure Notification Hub.";
public static final String ERROR_FETCH_IMAGE = "Error while fetching image.";
public static final String ERROR_GET_INIT_NOTIFICATION = "E_GET_INIT_NOTIF";
public static final String ERROR_ACTIVITY_IS_NULL = "Current activity is null";
public static final String ERROR_INTENT_EXTRAS_IS_NULL = "Intent get extras is null";
public static final String ERROR_ACTIVITY_INTENT_IS_NULL = "Activity intent is null";
public static final String ERROR_GET_UUID = "E_GET_UUID";
public static final String ERROR_NO_UUID_SET = "No uuid set";

private ReactNativeConstants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.core.app.NotificationManagerCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.WritableMap;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;

Expand Down Expand Up @@ -61,6 +64,29 @@ public boolean getIsForeground() {
return notificationHubUtil.getAppIsForeground();
}

@ReactMethod
public void getInitialNotification(Promise promise) {
Activity activity = getCurrentActivity();
if (activity == null) {
promise.reject(ERROR_GET_INIT_NOTIFICATION, ERROR_ACTIVITY_IS_NULL);
return;
}

Intent intent = activity.getIntent();
if (intent != null && intent.getAction() != null) {
if (intent.getExtras() == null) {
// In certain cases while app cold launches, i.getExtras() returns null.
// Adding the check to make sure app won't crash,
// and still successfully launches from notification
promise.reject(ERROR_GET_INIT_NOTIFICATION, ERROR_INTENT_EXTRAS_IS_NULL);
} else {
promise.resolve(ReactNativeUtil.convertBundleToMap(intent.getExtras()));
}
} else {
promise.reject(ERROR_GET_INIT_NOTIFICATION, ERROR_ACTIVITY_INTENT_IS_NULL);
}
}

@ReactMethod
public void register(ReadableMap config, Promise promise) {
ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil.getInstance();
Expand Down Expand Up @@ -95,6 +121,114 @@ public void register(ReadableMap config, Promise promise) {
notificationHubUtil.setConnectionString(reactContext, connectionString);
notificationHubUtil.setHubName(reactContext, hubName);
notificationHubUtil.setSenderID(reactContext, senderID);
notificationHubUtil.setTemplated(reactContext, false);
notificationHubUtil.setTags(reactContext, tags);

if (config.hasKey(KEY_REGISTRATION_CHANNELNAME)) {
String channelName = config.getString(KEY_REGISTRATION_CHANNELNAME);
notificationHubUtil.setChannelName(reactContext, channelName);
}

if (config.hasKey(KEY_REGISTRATION_CHANNELIMPORTANCE)) {
int channelImportance = config.getInt(KEY_REGISTRATION_CHANNELIMPORTANCE);
notificationHubUtil.setChannelImportance(reactContext, channelImportance);
}

if (config.hasKey(KEY_REGISTRATION_CHANNELSHOWBADGE)) {
boolean channelShowBadge = config.getBoolean(KEY_REGISTRATION_CHANNELSHOWBADGE);
notificationHubUtil.setChannelShowBadge(reactContext, channelShowBadge);
}

if (config.hasKey(KEY_REGISTRATION_CHANNELENABLELIGHTS)) {
boolean channelEnableLights = config.getBoolean(KEY_REGISTRATION_CHANNELENABLELIGHTS);
notificationHubUtil.setChannelEnableLights(reactContext, channelEnableLights);
}

if (config.hasKey(KEY_REGISTRATION_CHANNELENABLEVIBRATION)) {
boolean channelEnableVibration = config.getBoolean(KEY_REGISTRATION_CHANNELENABLEVIBRATION);
notificationHubUtil.setChannelEnableVibration(reactContext, channelEnableVibration);
}

String uuid = notificationHubUtil.getUUID(reactContext);
if (uuid == null) {
uuid = ReactNativeUtil.genUUID();
notificationHubUtil.setUUID(reactContext, uuid);
}

GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(reactContext);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
UiThreadUtil.runOnUiThread(
new GoogleApiAvailabilityRunnable(
getCurrentActivity(),
apiAvailability,
resultCode));
promise.reject(ERROR_PLAY_SERVICES, ERROR_PLAY_SERVICES_DISABLED);
} else {
promise.reject(ERROR_PLAY_SERVICES, ERROR_PLAY_SERVICES_UNSUPPORTED);
}
return;
}

Intent intent = ReactNativeNotificationHubUtil.IntentFactory.createIntent(
reactContext, ReactNativeRegistrationIntentService.class);
ReactNativeRegistrationIntentService.enqueueWork(reactContext, intent);

WritableMap res = Arguments.createMap();
res.putString(KEY_PROMISE_RESOLVE_UUID, uuid);
promise.resolve(res);
}

@ReactMethod
public void registerTemplate(ReadableMap config, Promise promise) {
ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil.getInstance();
String connectionString = config.getString(KEY_REGISTRATION_CONNECTIONSTRING);
if (connectionString == null) {
promise.reject(ERROR_INVALID_ARGUMENTS, ERROR_INVALID_CONNECTION_STRING);
return;
}

String hubName = config.getString(KEY_REGISTRATION_HUBNAME);
if (hubName == null) {
promise.reject(ERROR_INVALID_ARGUMENTS, ERROR_INVALID_HUBNAME);
return;
}

String senderID = config.getString(KEY_REGISTRATION_SENDERID);
if (senderID == null) {
promise.reject(ERROR_INVALID_ARGUMENTS, ERROR_INVALID_SENDER_ID);
return;
}

String templateName = config.getString(KEY_REGISTRATION_TEMPLATENAME);
if (templateName == null) {
promise.reject(ERROR_INVALID_ARGUMENTS, ERROR_INVALID_TEMPLATE_NAME);
return;
}

String template = config.getString(KEY_REGISTRATION_TEMPLATE);
if (template == null) {
promise.reject(ERROR_INVALID_ARGUMENTS, ERROR_INVALID_TEMPLATE);
return;
}

String[] tags = null;
if (config.hasKey(KEY_REGISTRATION_TAGS) && !config.isNull(KEY_REGISTRATION_TAGS)) {
ReadableArray tagsJson = config.getArray(KEY_REGISTRATION_TAGS);
tags = new String[tagsJson.size()];
for (int i = 0; i < tagsJson.size(); ++i) {
tags[i] = tagsJson.getString(i);
}
}

ReactContext reactContext = getReactApplicationContext();
notificationHubUtil.setConnectionString(reactContext, connectionString);
notificationHubUtil.setHubName(reactContext, hubName);
notificationHubUtil.setSenderID(reactContext, senderID);
notificationHubUtil.setTemplateName(reactContext, templateName);
notificationHubUtil.setTemplate(reactContext, template);
notificationHubUtil.setTemplated(reactContext, true);
notificationHubUtil.setTags(reactContext, tags);

if (config.hasKey(KEY_REGISTRATION_CHANNELNAME)) {
Expand Down Expand Up @@ -122,6 +256,12 @@ public void register(ReadableMap config, Promise promise) {
notificationHubUtil.setChannelEnableVibration(reactContext, channelEnableVibration);
}

String uuid = notificationHubUtil.getUUID(reactContext);
if (uuid == null) {
uuid = ReactNativeUtil.genUUID();
notificationHubUtil.setUUID(reactContext, uuid);
}

GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(reactContext);
if (resultCode != ConnectionResult.SUCCESS) {
Expand All @@ -141,6 +281,10 @@ public void register(ReadableMap config, Promise promise) {
Intent intent = ReactNativeNotificationHubUtil.IntentFactory.createIntent(
reactContext, ReactNativeRegistrationIntentService.class);
ReactNativeRegistrationIntentService.enqueueWork(reactContext, intent);

WritableMap res = Arguments.createMap();
res.putString(KEY_PROMISE_RESOLVE_UUID, uuid);
promise.resolve(res);
}

@ReactMethod
Expand All @@ -161,11 +305,63 @@ public void unregister(Promise promise) {
try {
hub.unregister();
notificationHubUtil.setRegistrationID(reactContext, null);
notificationHubUtil.setUUID(reactContext, null);
promise.resolve(AZURE_NOTIFICATION_HUB_UNREGISTERED);
} catch (Exception e) {
promise.reject(ERROR_NOTIFICATION_HUB, e);
}
}

@ReactMethod
public void unregisterTemplate(String templateName, Promise promise) {
ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil.getInstance();

ReactContext reactContext = getReactApplicationContext();
String connectionString = notificationHubUtil.getConnectionString(reactContext);
String hubName = notificationHubUtil.getHubName(reactContext);
String registrationId = notificationHubUtil.getRegistrationID(reactContext);

if (connectionString == null || hubName == null || registrationId == null) {
promise.reject(ERROR_NOT_REGISTERED, ERROR_NOT_REGISTERED_DESC);
return;
}

NotificationHub hub = ReactNativeUtil.createNotificationHub(hubName, connectionString, reactContext);
try {
hub.unregisterTemplate(templateName);
notificationHubUtil.setRegistrationID(reactContext, null);
notificationHubUtil.setUUID(reactContext, null);
promise.resolve(AZURE_NOTIFICATION_HUB_UNREGISTERED);
} catch (Exception e) {
promise.reject(ERROR_NOTIFICATION_HUB, e);
}
}

@ReactMethod
public void getUUID(Boolean autoGen, Promise promise) {
ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil.getInstance();
ReactContext reactContext = getReactApplicationContext();
String uuid = notificationHubUtil.getUUID(reactContext);

if (uuid != null) {
promise.resolve(uuid);
} else if (autoGen) {
uuid = ReactNativeUtil.genUUID();
notificationHubUtil.setUUID(reactContext, uuid);
promise.resolve(uuid);
} else {
promise.reject(ERROR_GET_UUID, ERROR_NO_UUID_SET);
}
}

@ReactMethod
public void isNotificationEnabledOnOSLevel(Promise promise) {
ReactContext reactContext = getReactApplicationContext();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(reactContext);
boolean areNotificationsEnabled = notificationManagerCompat.areNotificationsEnabled();
promise.resolve(areNotificationsEnabled);
}

@Override
public void onHostResume() {
setIsForeground(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,38 @@ public boolean hasChannelEnableVibration(Context context) {
return hasKey(context, KEY_FOR_PREFS_CHANNELENABLEVIBRATION);
}

public String getTemplateName(Context context) {
return getPref(context, KEY_FOR_PREFS_TEMPLATENAME);
}

public void setTemplateName(Context context, String templateName) {
setPref(context, KEY_FOR_PREFS_TEMPLATENAME, templateName);
}

public String getTemplate(Context context) {
return getPref(context, KEY_FOR_PREFS_TEMPLATE);
}

public void setTemplate(Context context, String template) {
setPref(context, KEY_FOR_PREFS_TEMPLATE, template);
}

public boolean isTemplated(Context context) {
return getPrefBoolean(context, KEY_FOR_PREFS_ISTEMPLATE);
}

public void setTemplated(Context context, boolean templated) {
setPrefBoolean(context, KEY_FOR_PREFS_ISTEMPLATE, templated);
}

public String getUUID(Context context) {
return getPref(context, KEY_FOR_PREFS_UUID);
}

public void setUUID(Context context, String uuid) {
setPref(context, KEY_FOR_PREFS_UUID, uuid);
}

public void setAppIsForeground(boolean isForeground) {
mIsForeground = isForeground;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ protected void onHandleWork(Intent intent) {
final String hubName = notificationHubUtil.getHubName(this);
final String storedToken = notificationHubUtil.getFCMToken(this);
final String[] tags = notificationHubUtil.getTags(this);
final boolean isTemplated = notificationHubUtil.isTemplated(this);
final String templateName = notificationHubUtil.getTemplateName(this);
final String template = notificationHubUtil.getTemplate(this);

if (connectionString == null || hubName == null) {
// The intent was triggered when no connection string has been set.
Expand All @@ -63,7 +66,13 @@ public void onSuccess(InstanceIdResult instanceIdResult) {
ReactNativeRegistrationIntentService.this);
Log.d(TAG, "NH Registration refreshing with token : " + token);

regID = hub.register(token, tags).getRegistrationId();
if (isTemplated) {
regID = hub.registerTemplate(
token, templateName, template, tags).getRegistrationId();
} else {
regID = hub.register(token, tags).getRegistrationId();
}

Log.d(TAG, "New NH Registration Successfully - RegId : " + regID);

notificationHubUtil.setRegistrationID(ReactNativeRegistrationIntentService.this, regID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down Expand Up @@ -335,6 +335,10 @@ public static Bitmap fetchImage(String urlString) {
}
}

public static String genUUID() {
return UUID.randomUUID().toString();
}

private ReactNativeUtil() {
}
}
Loading

0 comments on commit 6518a1f

Please sign in to comment.