Skip to content

Commit 8133b0d

Browse files
committed
Refactored notification hub handler.
1 parent 103e68c commit 8133b0d

File tree

6 files changed

+359
-193
lines changed

6 files changed

+359
-193
lines changed

android/src/main/java/com/azure/reactnative/notificationhub/NotificationHubUtil.java

Lines changed: 174 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package com.azure.reactnative.notificationhub;
22

3+
import android.app.PendingIntent;
34
import android.content.Context;
45
import android.content.Intent;
56
import android.content.SharedPreferences;
7+
import android.content.res.Resources;
8+
import android.media.RingtoneManager;
9+
import android.net.Uri;
610
import android.os.Bundle;
11+
import android.util.Log;
12+
13+
import androidx.core.app.NotificationCompat;
714

815
import java.util.Arrays;
916
import java.util.HashSet;
@@ -14,9 +21,30 @@
1421
import com.facebook.react.bridge.ReactContext;
1522
import com.microsoft.windowsazure.messaging.NotificationHub;
1623

24+
import org.json.JSONArray;
1725
import org.json.JSONException;
1826
import org.json.JSONObject;
1927

28+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.ERROR_COVERT_ACTIONS;
29+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.ERROR_GET_ACTIONS_ARRAY;
30+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_INTENT_NOTIFICATION;
31+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_ACTION;
32+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_ACTIONS;
33+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_COLDSTART;
34+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_FOREGROUND;
35+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_SMALL_ICON;
36+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_SOUND_NAME;
37+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_USER_INTERACTION;
38+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_HIGH;
39+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_LOW;
40+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_MAX;
41+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_MIN;
42+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.REMOTE_NOTIFICATION_PRIORITY_NORMAL;
43+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.RESOURCE_DEF_TYPE_MIPMAP;
44+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.RESOURCE_DEF_TYPE_RAW;
45+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.RESOURCE_NAME_LAUNCHER;
46+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.RESOURCE_NAME_NOTIFICATION;
47+
2048
public class NotificationHubUtil {
2149
private static NotificationHubUtil sharedNotificationHubUtilInstance = null;
2250

@@ -149,6 +177,10 @@ public boolean getAppIsForeground() {
149177
return mIsForeground;
150178
}
151179

180+
public void runInWorkerThread(Runnable runnable) {
181+
mPool.execute(runnable);
182+
}
183+
152184
public NotificationHub createNotificationHub(String hubName, String connectionString, ReactContext reactContext) {
153185
NotificationHub hub = new NotificationHub(hubName, connectionString, reactContext);
154186
return hub;
@@ -175,8 +207,148 @@ public Intent createBroadcastIntent(String action, JSONObject json) {
175207
return intent;
176208
}
177209

178-
public void runInWorkerThread(Runnable runnable) {
179-
mPool.execute(runnable);
210+
public Class getMainActivityClass(Context context) {
211+
String packageName = context.getPackageName();
212+
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
213+
String className = launchIntent.getComponent().getClassName();
214+
try {
215+
return Class.forName(className);
216+
} catch (ClassNotFoundException e) {
217+
e.printStackTrace();
218+
return null;
219+
}
220+
}
221+
222+
public int getNotificationCompatPriority(String priorityString) {
223+
int priority = NotificationCompat.PRIORITY_DEFAULT;
224+
if (priorityString != null) {
225+
switch (priorityString.toLowerCase()) {
226+
case REMOTE_NOTIFICATION_PRIORITY_MAX:
227+
priority = NotificationCompat.PRIORITY_MAX;
228+
break;
229+
case REMOTE_NOTIFICATION_PRIORITY_HIGH:
230+
priority = NotificationCompat.PRIORITY_HIGH;
231+
break;
232+
case REMOTE_NOTIFICATION_PRIORITY_LOW:
233+
priority = NotificationCompat.PRIORITY_LOW;
234+
break;
235+
case REMOTE_NOTIFICATION_PRIORITY_MIN:
236+
priority = NotificationCompat.PRIORITY_MIN;
237+
break;
238+
case REMOTE_NOTIFICATION_PRIORITY_NORMAL:
239+
priority = NotificationCompat.PRIORITY_DEFAULT;
240+
break;
241+
}
242+
}
243+
244+
return priority;
245+
}
246+
247+
public int getSmallIcon(Bundle bundle, Resources res, String packageName) {
248+
int smallIconResId;
249+
String smallIcon = bundle.getString(KEY_REMOTE_NOTIFICATION_SMALL_ICON);
250+
251+
if (smallIcon != null) {
252+
smallIconResId = res.getIdentifier(smallIcon, RESOURCE_DEF_TYPE_MIPMAP, packageName);
253+
} else {
254+
smallIconResId = res.getIdentifier(RESOURCE_NAME_NOTIFICATION, RESOURCE_DEF_TYPE_MIPMAP, packageName);
255+
}
256+
257+
if (smallIconResId == 0) {
258+
smallIconResId = res.getIdentifier(RESOURCE_NAME_LAUNCHER, RESOURCE_DEF_TYPE_MIPMAP, packageName);
259+
260+
if (smallIconResId == 0) {
261+
smallIconResId = android.R.drawable.ic_dialog_info;
262+
}
263+
}
264+
265+
return smallIconResId;
266+
}
267+
268+
public int getLargeIcon(Bundle bundle, String largeIcon, Resources res, String packageName) {
269+
int largeIconResId;
270+
271+
if (largeIcon != null) {
272+
largeIconResId = res.getIdentifier(largeIcon, RESOURCE_DEF_TYPE_MIPMAP, packageName);
273+
} else {
274+
largeIconResId = res.getIdentifier(RESOURCE_NAME_LAUNCHER, RESOURCE_DEF_TYPE_MIPMAP, packageName);
275+
}
276+
277+
return largeIconResId;
278+
}
279+
280+
public Uri getSoundUri(Context context, Bundle bundle) {
281+
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
282+
String soundName = bundle.getString(KEY_REMOTE_NOTIFICATION_SOUND_NAME);
283+
if (soundName != null) {
284+
if (!"default".equalsIgnoreCase(soundName)) {
285+
286+
// sound name can be full filename, or just the resource name.
287+
// So the strings 'my_sound.mp3' AND 'my_sound' are accepted
288+
// The reason is to make the iOS and android javascript interfaces compatible
289+
290+
int resId;
291+
if (context.getResources().getIdentifier(soundName, RESOURCE_DEF_TYPE_RAW, context.getPackageName()) != 0) {
292+
resId = context.getResources().getIdentifier(soundName, RESOURCE_DEF_TYPE_RAW, context.getPackageName());
293+
} else {
294+
soundName = soundName.substring(0, soundName.lastIndexOf('.'));
295+
resId = context.getResources().getIdentifier(soundName, RESOURCE_DEF_TYPE_RAW, context.getPackageName());
296+
}
297+
298+
soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + resId);
299+
}
300+
}
301+
302+
return soundUri;
303+
}
304+
305+
public Intent createNotificationIntent(Context context, Bundle bundle, Class intentClass) {
306+
Intent intent = new Intent(context, intentClass);
307+
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
308+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, true);
309+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, false);
310+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_COLDSTART, false);
311+
intent.putExtra(KEY_INTENT_NOTIFICATION, bundle);
312+
313+
return intent;
314+
}
315+
316+
public void processNotificationActions(Context context, Bundle bundle,
317+
NotificationCompat.Builder notification,
318+
int notificationID) {
319+
final String tag = ReactNativeNotificationsHandler.TAG;
320+
JSONArray actionsArray = null;
321+
try {
322+
actionsArray = bundle.getString(KEY_REMOTE_NOTIFICATION_ACTIONS) != null ?
323+
new JSONArray(bundle.getString(KEY_REMOTE_NOTIFICATION_ACTIONS)) : null;
324+
} catch (JSONException e) {
325+
Log.e(tag, ERROR_COVERT_ACTIONS, e);
326+
}
327+
328+
if (actionsArray != null) {
329+
// No icon for now. The icon value of 0 shows no icon.
330+
int icon = 0;
331+
332+
// Add button for each actions.
333+
for (int i = 0; i < actionsArray.length(); i++) {
334+
String action;
335+
try {
336+
action = actionsArray.getString(i);
337+
} catch (JSONException e) {
338+
Log.e(tag, ERROR_GET_ACTIONS_ARRAY, e);
339+
continue;
340+
}
341+
342+
Intent actionIntent = new Intent();
343+
actionIntent.setAction(context.getPackageName() + "." + action);
344+
// Add "action" for later identifying which button gets pressed.
345+
bundle.putString(KEY_REMOTE_NOTIFICATION_ACTION, action);
346+
actionIntent.putExtra(KEY_INTENT_NOTIFICATION, bundle);
347+
PendingIntent pendingActionIntent = PendingIntent.getBroadcast(context, notificationID, actionIntent,
348+
PendingIntent.FLAG_UPDATE_CURRENT);
349+
notification.addAction(icon, action, pendingActionIntent);
350+
}
351+
}
180352
}
181353

182354
private String getPref(Context context, String key) {

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import com.google.firebase.messaging.FirebaseMessagingService;
1212
import com.google.firebase.messaging.RemoteMessage;
1313

14+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_COLDSTART;
15+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_FOREGROUND;
16+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_USER_INTERACTION;
17+
1418
public class ReactNativeFirebaseMessagingService extends FirebaseMessagingService {
1519

1620
private static final String TAG = "ReactNativeFMS";
@@ -67,9 +71,9 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
6771

6872
Bundle bundle = remoteMessage.toIntent().getExtras();
6973
if (notificationHubUtil.getAppIsForeground()) {
70-
bundle.putBoolean("foreground", true);
71-
bundle.putBoolean("userInteraction", false);
72-
bundle.putBoolean("coldstart", false);
74+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, true);
75+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, false);
76+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_COLDSTART, false);
7377
} else {
7478
ReactNativeNotificationsHandler.sendNotification(this, bundle, notificationChannelID);
7579
}

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
import com.facebook.react.bridge.ReadableMap;
2727
import com.facebook.react.bridge.UiThreadUtil;
2828

29+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_INTENT_NOTIFICATION;
30+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_COLDSTART;
31+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_FOREGROUND;
32+
import static com.azure.reactnative.notificationhub.ReactNativeNotificationsHandler.KEY_REMOTE_NOTIFICATION_USER_INTERACTION;
33+
2934
public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements
3035
ActivityEventListener, LifecycleEventListener {
3136
public static final String AZURE_NOTIFICATION_HUB_NAME = "AzureNotificationHub";
@@ -183,12 +188,12 @@ public void onHostResume() {
183188
if (activity != null) {
184189
Intent intent = activity.getIntent();
185190
if (intent != null) {
186-
Bundle bundle = intent.getBundleExtra("notification");
191+
Bundle bundle = intent.getBundleExtra(KEY_INTENT_NOTIFICATION);
187192
if (bundle != null) {
188-
intent.removeExtra("notification");
189-
bundle.putBoolean("foreground", false);
190-
bundle.putBoolean("userInteraction", true);
191-
bundle.putBoolean("coldstart", true);
193+
intent.removeExtra(KEY_INTENT_NOTIFICATION);
194+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, false);
195+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, true);
196+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_COLDSTART, true);
192197
ReactNativeNotificationsHandler.sendBroadcast(
193198
mReactContext, bundle, NOTIFICATION_DELAY_ON_START);
194199
}
@@ -207,10 +212,10 @@ public void onHostDestroy() {
207212

208213
@Override
209214
public void onNewIntent(Intent intent) {
210-
Bundle bundle = intent.getBundleExtra("notification");
215+
Bundle bundle = intent.getBundleExtra(KEY_INTENT_NOTIFICATION);
211216
if (bundle != null) {
212-
bundle.putBoolean("foreground", false);
213-
bundle.putBoolean("userInteraction", true);
217+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, false);
218+
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, true);
214219
ReactNativeNotificationsHandler.sendBroadcast(
215220
mReactContext, bundle, NOTIFICATION_DELAY_ON_START);
216221
}

0 commit comments

Comments
 (0)