Skip to content

Commit 2cebacf

Browse files
committed
Fixed notification channel creation.
1 parent 5de4ea9 commit 2cebacf

6 files changed

Lines changed: 46 additions & 49 deletions

android/react-native-azurenotificationhub.iml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
<orderEntry type="library" name="Gradle: com.squareup.okhttp3:okhttp:3.12.1@jar" level="project" />
102102
<orderEntry type="library" name="Gradle: com.squareup.okio:okio:1.15.0@jar" level="project" />
103103
<orderEntry type="library" name="Gradle: com.google.auto.value:auto-value-annotations:1.6@jar" level="project" />
104-
<orderEntry type="library" name="Gradle: __local_aars__:C.\Projects\react-native-azurenotificationhub\sample\node_modules\react-native-azurenotificationhub\android\libs\notifications-handler-release.aar:unspecified@jar" level="project" />
105104
<orderEntry type="library" name="Gradle: com.facebook.react:react-native:0.61.5@aar" level="project" />
106105
<orderEntry type="library" name="Gradle: androidx.appcompat:appcompat:1.0.2@aar" level="project" />
107106
<orderEntry type="library" name="Gradle: com.google.firebase:firebase-messaging:17.6.0@aar" level="project" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import android.app.NotificationManager;
55

66
public class NotificationChannelBuilder {
7-
private String mID = "rn-push-notification-channel-id";
7+
private String mID = ReactNativeNotificationsHandler.NOTIFICATION_CHANNEL_ID;
88
private CharSequence mName = "rn-push-notification-channel-name";
99
private String mDesc = "rn-push-notification-channel-description";
1010
private int mImportance = NotificationManager.IMPORTANCE_DEFAULT;

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package com.azure.reactnative.notificationhub;
22

33
import android.app.NotificationChannel;
4+
import android.app.NotificationManager;
45
import android.content.Context;
56
import android.content.Intent;
7+
import android.os.Build;
68
import android.os.Bundle;
79
import android.util.Log;
810

911
import com.google.firebase.messaging.FirebaseMessagingService;
1012
import com.google.firebase.messaging.RemoteMessage;
1113

12-
import java.util.Map;
13-
1414
public class ReactNativeFirebaseMessagingService extends FirebaseMessagingService {
1515

16-
private static final String TAG = "ReactNativeFirebaseMessagingService";
16+
private static final String TAG = "ReactNativeFMS";
1717

1818
private static ReactNativeNotificationsHandler notificationHandler;
19+
private static Context appContext;
20+
private static String notificationChannelID;
1921

2022
public static void createNotificationHandler(Context context) {
23+
appContext = context;
24+
2125
if (notificationHandler == null) {
2226
notificationHandler = new ReactNativeNotificationsHandler();
2327
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
@@ -38,8 +42,14 @@ public static void createNotificationHandler(Context context) {
3842
builder.enableVibration(notificationHubUtil.getChannelEnableVibration(context));
3943
}
4044

41-
NotificationChannel channel = builder.build();
42-
notificationHandler.createChannelAndHandleNotifications(context, channel);
45+
notificationChannelID = ReactNativeNotificationsHandler.NOTIFICATION_CHANNEL_ID;
46+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
47+
NotificationChannel channel = builder.build();
48+
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
49+
Context.NOTIFICATION_SERVICE);
50+
notificationManager.createNotificationChannel(channel);
51+
notificationChannelID = channel.getId();
52+
}
4353
}
4454
}
4555

@@ -61,7 +71,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
6171
}
6272

6373
Bundle bundle = remoteMessage.toIntent().getExtras();
64-
notificationHandler.sendNotification(bundle);
65-
notificationHandler.sendBroadcast(bundle, 0);
74+
notificationHandler.sendNotification(appContext, bundle, notificationChannelID);
75+
notificationHandler.sendBroadcast(appContext, bundle, 0);
6676
}
6777
}

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

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

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

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

3-
import android.app.AlarmManager;
4-
import android.app.Application;
53
import android.app.Notification;
6-
import android.app.NotificationChannel;
74
import android.app.NotificationManager;
85
import android.app.PendingIntent;
96
import android.content.Context;
@@ -32,13 +29,11 @@
3229
public class ReactNativeNotificationsHandler {
3330
public static final String TAG = "ReactNativeNotification";
3431

35-
private static final long DEFAULT_VIBRATION = 300L;
32+
public static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id";
3633

37-
private static Context appContext;
38-
private static boolean channelCreated;
39-
private static NotificationChannel notificationChannel;
34+
private static final long DEFAULT_VIBRATION = 300L;
4035

41-
public void sendBroadcast(final Bundle bundle, final long delay) {
36+
public void sendBroadcast(final Context context, final Bundle bundle, final long delay) {
4237
(new Thread() {
4338
public void run() {
4439
try {
@@ -55,17 +50,17 @@ public void run() {
5550
Intent event = new Intent(TAG);
5651
event.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
5752
event.putExtra("data", json.toString());
58-
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(appContext);
53+
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
5954
localBroadcastManager.sendBroadcast(event);
6055
} catch (Exception e) {
6156
}
6257
}
6358
}).start();
6459
}
6560

66-
public void sendNotification(Bundle bundle) {
61+
public void sendNotification(Context context, Bundle bundle, String notificationChannelID) {
6762
try {
68-
Class intentClass = getMainActivityClass();
63+
Class intentClass = getMainActivityClass(context);
6964
if (intentClass == null) {
7065
Log.e(TAG, "No activity class found for the notification");
7166
return;
@@ -82,13 +77,13 @@ public void sendNotification(Bundle bundle) {
8277
return;
8378
}
8479

85-
Resources res = appContext.getResources();
86-
String packageName = appContext.getPackageName();
80+
Resources res = context.getResources();
81+
String packageName = context.getPackageName();
8782

8883
String title = bundle.getString("title");
8984
if (title == null) {
90-
ApplicationInfo appInfo = appContext.getApplicationInfo();
91-
title = appContext.getPackageManager().getApplicationLabel(appInfo).toString();
85+
ApplicationInfo appInfo = context.getApplicationInfo();
86+
title = context.getPackageManager().getApplicationLabel(appInfo).toString();
9287
}
9388

9489
int priority = NotificationCompat.PRIORITY_DEFAULT;
@@ -113,7 +108,7 @@ public void sendNotification(Bundle bundle) {
113108
}
114109
}
115110

116-
NotificationCompat.Builder notification = new NotificationCompat.Builder(appContext, notificationChannel.getId())
111+
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, notificationChannelID)
117112
.setContentTitle(title)
118113
.setTicker(bundle.getString("ticker"))
119114
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
@@ -180,7 +175,7 @@ public void sendNotification(Bundle bundle) {
180175

181176
notification.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));
182177

183-
Intent intent = new Intent(appContext, intentClass);
178+
Intent intent = new Intent(context, intentClass);
184179
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
185180
bundle.putBoolean("userInteraction", true);
186181
intent.putExtra("notification", bundle);
@@ -196,14 +191,14 @@ public void sendNotification(Bundle bundle) {
196191
// The reason is to make the iOS and android javascript interfaces compatible
197192

198193
int resId;
199-
if (appContext.getResources().getIdentifier(soundName, "raw", appContext.getPackageName()) != 0) {
200-
resId = appContext.getResources().getIdentifier(soundName, "raw", appContext.getPackageName());
194+
if (context.getResources().getIdentifier(soundName, "raw", context.getPackageName()) != 0) {
195+
resId = context.getResources().getIdentifier(soundName, "raw", context.getPackageName());
201196
} else {
202197
soundName = soundName.substring(0, soundName.lastIndexOf('.'));
203-
resId = appContext.getResources().getIdentifier(soundName, "raw", appContext.getPackageName());
198+
resId = context.getResources().getIdentifier(soundName, "raw", context.getPackageName());
204199
}
205200

206-
soundUri = Uri.parse("android.resource://" + appContext.getPackageName() + "/" + resId);
201+
soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + resId);
207202
}
208203
}
209204
notification.setSound(soundUri);
@@ -224,7 +219,7 @@ public void sendNotification(Bundle bundle) {
224219

225220
int notificationID = notificationIdString.hashCode();
226221

227-
PendingIntent pendingIntent = PendingIntent.getActivity(appContext, notificationID, intent,
222+
PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationID, intent,
228223
PendingIntent.FLAG_UPDATE_CURRENT);
229224

230225
notification.setContentIntent(pendingIntent);
@@ -258,18 +253,19 @@ public void sendNotification(Bundle bundle) {
258253
}
259254

260255
Intent actionIntent = new Intent();
261-
actionIntent.setAction(appContext.getPackageName() + "." + action);
256+
actionIntent.setAction(context.getPackageName() + "." + action);
262257
// Add "action" for later identifying which button gets pressed.
263258
bundle.putString("action", action);
264259
actionIntent.putExtra("notification", bundle);
265-
PendingIntent pendingActionIntent = PendingIntent.getBroadcast(appContext, notificationID, actionIntent,
260+
PendingIntent pendingActionIntent = PendingIntent.getBroadcast(context, notificationID, actionIntent,
266261
PendingIntent.FLAG_UPDATE_CURRENT);
267262
notification.addAction(icon, action, pendingActionIntent);
268263
}
269264
}
270265

271266
Notification info = notification.build();
272-
NotificationManager notificationManager = appContext.getSystemService(NotificationManager.class);
267+
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
268+
Context.NOTIFICATION_SERVICE);
273269
if (bundle.containsKey("tag")) {
274270
String tag = bundle.getString("tag");
275271
notificationManager.notify(tag, notificationID, info);
@@ -281,19 +277,9 @@ public void sendNotification(Bundle bundle) {
281277
}
282278
}
283279

284-
public void createChannelAndHandleNotifications(Context context, NotificationChannel channel) {
285-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !channelCreated) {
286-
appContext = context;
287-
notificationChannel = channel;
288-
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
289-
notificationManager.createNotificationChannel(channel);
290-
channelCreated = true;
291-
}
292-
}
293-
294-
private Class getMainActivityClass() {
295-
String packageName = appContext.getPackageName();
296-
Intent launchIntent = appContext.getPackageManager().getLaunchIntentForPackage(packageName);
280+
private Class getMainActivityClass(Context context) {
281+
String packageName = context.getPackageName();
282+
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
297283
String className = launchIntent.getComponent().getClassName();
298284
try {
299285
return Class.forName(className);

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

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

7272
// Create notification handler
73-
ReactNativeFirebaseMessagingService.createNotificationHandler(ReactNativeRegistrationIntentService.this);
73+
ReactNativeFirebaseMessagingService.createNotificationHandler(
74+
ReactNativeRegistrationIntentService.this);
7475
}
7576
} catch (Exception e) {
7677
Log.e(TAG, "Failed to complete token refresh", e);

0 commit comments

Comments
 (0)