Skip to content

Commit 2f06e40

Browse files
committed
Refactored notification handler.
1 parent e318e36 commit 2f06e40

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ public class ReactNativeFirebaseMessagingService extends FirebaseMessagingServic
1515

1616
private static final String TAG = "ReactNativeFMS";
1717

18-
private static ReactNativeNotificationsHandler notificationHandler;
1918
private static String notificationChannelID;
2019

21-
public static void createNotificationHandler(Context context) {
22-
if (notificationHandler == null) {
23-
notificationHandler = new ReactNativeNotificationsHandler();
20+
public static void createNotificationChannel(Context context) {
21+
if (notificationChannelID == null) {
2422
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
2523
NotificationChannelBuilder builder = new NotificationChannelBuilder();
2624
if (notificationHubUtil.hasChannelImportance(context)) {
@@ -62,12 +60,12 @@ public void onNewToken(String token) {
6260
public void onMessageReceived(RemoteMessage remoteMessage) {
6361
Log.d(TAG, "Remote message from: " + remoteMessage.getFrom());
6462

65-
if (notificationHandler == null) {
66-
createNotificationHandler(this);
63+
if (notificationChannelID == null) {
64+
createNotificationChannel(this);
6765
}
6866

6967
Bundle bundle = remoteMessage.toIntent().getExtras();
70-
notificationHandler.sendNotification(this, bundle, notificationChannelID);
71-
notificationHandler.sendBroadcast(this, bundle, 0);
68+
ReactNativeNotificationsHandler.sendNotification(this, bundle, notificationChannelID);
69+
ReactNativeNotificationsHandler.sendBroadcast(this, bundle, 0);
7270
}
7371
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void onHostResume() {
170170
if (intent != null) {
171171
Bundle bundle = intent.getBundleExtra("notification");
172172
if (bundle != null) {
173-
new ReactNativeNotificationsHandler().sendBroadcast(
173+
ReactNativeNotificationsHandler.sendBroadcast(
174174
mReactContext, bundle, NOTIFICATION_DELAY_ON_START);
175175
}
176176
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626

2727
import java.util.Set;
2828

29-
public class ReactNativeNotificationsHandler {
29+
public final class ReactNativeNotificationsHandler {
3030
public static final String TAG = "ReactNativeNotification";
3131

3232
public static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id";
3333

3434
private static final long DEFAULT_VIBRATION = 300L;
3535

36-
public void sendBroadcast(final Context context, final Bundle bundle, final long delay) {
36+
private ReactNativeNotificationsHandler() {
37+
}
38+
39+
public static void sendBroadcast(final Context context, final Bundle bundle, final long delay) {
3740
(new Thread() {
3841
public void run() {
3942
try {
@@ -58,7 +61,7 @@ public void run() {
5861
}).start();
5962
}
6063

61-
public void sendNotification(Context context, Bundle bundle, String notificationChannelID) {
64+
public static void sendNotification(Context context, Bundle bundle, String notificationChannelID) {
6265
try {
6366
Class intentClass = getMainActivityClass(context);
6467
if (intentClass == null) {
@@ -277,7 +280,7 @@ public void sendNotification(Context context, Bundle bundle, String notification
277280
}
278281
}
279282

280-
private Class getMainActivityClass(Context context) {
283+
private static Class getMainActivityClass(Context context) {
281284
String packageName = context.getPackageName();
282285
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
283286
String className = launchIntent.getComponent().getClassName();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void onSuccess(InstanceIdResult instanceIdResult) {
7070
localBroadcastManager.sendBroadcast(event);
7171

7272
// Create notification handler
73-
ReactNativeFirebaseMessagingService.createNotificationHandler(
73+
ReactNativeFirebaseMessagingService.createNotificationChannel(
7474
ReactNativeRegistrationIntentService.this);
7575
}
7676
} catch (Exception e) {

0 commit comments

Comments
 (0)