Skip to content

Commit 5de4ea9

Browse files
committed
Removed notification handler lib file. Supported channel's importance.
1 parent 6a584aa commit 5de4ea9

13 files changed

Lines changed: 409 additions & 166 deletions

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies {
2525
implementation 'com.facebook.react:react-native:+'
2626
implementation 'com.google.firebase:firebase-messaging:17.6.0'
2727
implementation 'com.android.support:appcompat-v7:28.0.0'
28-
implementation 'com.microsoft.azure:notification-hubs-android-sdk:0.4@aar'
28+
implementation 'com.microsoft.azure:notification-hubs-android-sdk:0.6@aar'
2929
}
3030

3131
repositories {
-56.5 KB
Binary file not shown.

android/react-native-azurenotificationhub.iml

Lines changed: 94 additions & 65 deletions
Large diffs are not rendered by default.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.azure.reactnative.notificationhub;
2+
3+
import android.app.NotificationChannel;
4+
import android.app.NotificationManager;
5+
6+
public class NotificationChannelBuilder {
7+
private String mID = "rn-push-notification-channel-id";
8+
private CharSequence mName = "rn-push-notification-channel-name";
9+
private String mDesc = "rn-push-notification-channel-description";
10+
private int mImportance = NotificationManager.IMPORTANCE_DEFAULT;
11+
private boolean mShowBadge = true;
12+
private boolean mEnableLights = true;
13+
private boolean mEnableVibration = true;
14+
15+
public NotificationChannelBuilder() {
16+
}
17+
18+
public NotificationChannel build() {
19+
NotificationChannel channel = new NotificationChannel(mID, mName, mImportance);
20+
channel.setDescription(mDesc);
21+
channel.setShowBadge(mShowBadge);
22+
channel.enableLights(mEnableLights);
23+
channel.enableVibration(mEnableVibration);
24+
return channel;
25+
}
26+
27+
public NotificationChannelBuilder setName(CharSequence name) {
28+
this.mName = name;
29+
return this;
30+
}
31+
32+
public NotificationChannelBuilder setImportance(int importance) {
33+
this.mImportance = importance;
34+
return this;
35+
}
36+
37+
public NotificationChannelBuilder setDescription(String desc) {
38+
this.mDesc = desc;
39+
return this;
40+
}
41+
42+
public NotificationChannelBuilder setShowBadge(boolean showBadge) {
43+
this.mShowBadge = showBadge;
44+
return this;
45+
}
46+
47+
public NotificationChannelBuilder enableLights(boolean enableLights) {
48+
this.mEnableLights = enableLights;
49+
return this;
50+
}
51+
52+
public NotificationChannelBuilder enableVibration(boolean enableVibration) {
53+
this.mEnableVibration = enableVibration;
54+
return this;
55+
}
56+
}

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

Lines changed: 99 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,17 @@
1313
public class NotificationHubUtil {
1414
private static NotificationHubUtil sharedNotificationHubUtilInstance = null;
1515

16-
private static final String SHARED_PREFS_NAME =
17-
"com.azure.reactnative.notificationhub.NotificationHubUtil";
18-
private static final String KEY_FOR_PREFS_REGISTRATIONID =
19-
"AzureNotificationHub_registrationID";
20-
private static final String KEY_FOR_PREFS_CONNECTIONSTRING =
21-
"AzureNotificationHub_connectionString";
22-
private static final String KEY_FOR_PREFS_HUBNAME =
23-
"AzureNotificationHub_hubName";
24-
private static final String KEY_FOR_PREFS_FCMTOKEN =
25-
"AzureNotificationHub_FCMToken";
26-
private static final String KEY_FOR_PREFS_TAGS =
27-
"AzureNotificationHub_Tags";
16+
private static final String SHARED_PREFS_NAME = "com.azure.reactnative.notificationhub.NotificationHubUtil";
17+
private static final String KEY_FOR_PREFS_REGISTRATIONID = "AzureNotificationHub_registrationID";
18+
private static final String KEY_FOR_PREFS_CONNECTIONSTRING = "AzureNotificationHub_connectionString";
19+
private static final String KEY_FOR_PREFS_HUBNAME = "AzureNotificationHub_hubName";
20+
private static final String KEY_FOR_PREFS_FCMTOKEN = "AzureNotificationHub_FCMToken";
21+
private static final String KEY_FOR_PREFS_TAGS = "AzureNotificationHub_Tags";
22+
private static final String KEY_FOR_PREFS_SENDERID = "AzureNotificationHub_senderID";
23+
private static final String KEY_FOR_PREFS_CHANNELIMPORTANCE = "AzureNotificationHub_channelImportance";
24+
private static final String KEY_FOR_PREFS_CHANNELSHOWBADGE = "AzureNotificationHub_channelShowBadge";
25+
private static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights";
26+
private static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration";
2827

2928
public static NotificationHubUtil getInstance() {
3029
if (sharedNotificationHubUtilInstance == null) {
@@ -75,6 +74,62 @@ public void setTags(Context context, String[] tags) {
7574
setPrefSet(context, KEY_FOR_PREFS_TAGS, set);
7675
}
7776

77+
public String getSenderID(Context context) {
78+
return getPref(context, KEY_FOR_PREFS_SENDERID);
79+
}
80+
81+
public void setSenderID(Context context, String senderID) {
82+
setPref(context, KEY_FOR_PREFS_SENDERID, senderID);
83+
}
84+
85+
public int getChannelImportance(Context context) {
86+
return getPrefInt(context, KEY_FOR_PREFS_CHANNELIMPORTANCE);
87+
}
88+
89+
public void setChannelImportance(Context context, int channelImportance) {
90+
setPrefInt(context, KEY_FOR_PREFS_CHANNELIMPORTANCE, channelImportance);
91+
}
92+
93+
public boolean hasChannelImportance(Context context) {
94+
return hasKey(context, KEY_FOR_PREFS_CHANNELIMPORTANCE);
95+
}
96+
97+
public boolean getChannelShowBadge(Context context) {
98+
return getPrefBoolean(context, KEY_FOR_PREFS_CHANNELSHOWBADGE);
99+
}
100+
101+
public void setChannelShowBadge(Context context, boolean channelShowBadge) {
102+
setPrefBoolean(context, KEY_FOR_PREFS_CHANNELSHOWBADGE, channelShowBadge);
103+
}
104+
105+
public boolean hasChannelShowBadge(Context context) {
106+
return hasKey(context, KEY_FOR_PREFS_CHANNELSHOWBADGE);
107+
}
108+
109+
public boolean getChannelEnableLights(Context context) {
110+
return getPrefBoolean(context, KEY_FOR_PREFS_CHANNELENABLELIGHTS);
111+
}
112+
113+
public void setChannelEnableLights(Context context, boolean channelEnableLights) {
114+
setPrefBoolean(context, KEY_FOR_PREFS_CHANNELENABLELIGHTS, channelEnableLights);
115+
}
116+
117+
public boolean hasChannelEnableLights(Context context) {
118+
return hasKey(context, KEY_FOR_PREFS_CHANNELENABLELIGHTS);
119+
}
120+
121+
public boolean getChannelEnableVibration(Context context) {
122+
return getPrefBoolean(context, KEY_FOR_PREFS_CHANNELENABLEVIBRATION);
123+
}
124+
125+
public void setChannelEnableVibration(Context context, boolean channelEnableVibration) {
126+
setPrefBoolean(context, KEY_FOR_PREFS_CHANNELENABLEVIBRATION, channelEnableVibration);
127+
}
128+
129+
public boolean hasChannelEnableVibration(Context context) {
130+
return hasKey(context, KEY_FOR_PREFS_CHANNELENABLEVIBRATION);
131+
}
132+
78133
public NotificationHub createNotificationHub(String hubName, String connectionString, ReactContext reactContext) {
79134
NotificationHub hub = new NotificationHub(hubName, connectionString, reactContext);
80135
return hub;
@@ -86,6 +141,18 @@ private String getPref(Context context, String key) {
86141
return prefs.getString(key, null);
87142
}
88143

144+
private int getPrefInt(Context context, String key) {
145+
SharedPreferences prefs =
146+
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
147+
return prefs.getInt(key, 0);
148+
}
149+
150+
private boolean getPrefBoolean(Context context, String key) {
151+
SharedPreferences prefs =
152+
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
153+
return prefs.getBoolean(key, false);
154+
}
155+
89156
private Set<String> getPrefSet(Context context, String key) {
90157
SharedPreferences prefs =
91158
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
@@ -99,10 +166,30 @@ private void setPref(Context context, String key, String value) {
99166
editor.apply();
100167
}
101168

169+
private void setPrefInt(Context context, String key, int value) {
170+
SharedPreferences.Editor editor =
171+
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE).edit();
172+
editor.putInt(key, value);
173+
editor.apply();
174+
}
175+
176+
private void setPrefBoolean(Context context, String key, boolean value) {
177+
SharedPreferences.Editor editor =
178+
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE).edit();
179+
editor.putBoolean(key, value);
180+
editor.apply();
181+
}
182+
102183
private void setPrefSet(Context context, String key, Set<String> value) {
103184
SharedPreferences.Editor editor =
104185
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE).edit();
105186
editor.putStringSet(key, value);
106187
editor.apply();
107188
}
189+
190+
private boolean hasKey(Context context, String key) {
191+
SharedPreferences prefs =
192+
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
193+
return prefs.contains(key);
194+
}
108195
}
Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
11
package com.azure.reactnative.notificationhub;
22

3+
import android.app.NotificationChannel;
4+
import android.content.Context;
35
import android.content.Intent;
6+
import android.os.Bundle;
47
import android.util.Log;
58

69
import com.google.firebase.messaging.FirebaseMessagingService;
10+
import com.google.firebase.messaging.RemoteMessage;
11+
12+
import java.util.Map;
713

814
public class ReactNativeFirebaseMessagingService extends FirebaseMessagingService {
915

10-
private static final String TAG = "ReactNativeFirebaseMS";
16+
private static final String TAG = "ReactNativeFirebaseMessagingService";
17+
18+
private static ReactNativeNotificationsHandler notificationHandler;
19+
20+
public static void createNotificationHandler(Context context) {
21+
if (notificationHandler == null) {
22+
notificationHandler = new ReactNativeNotificationsHandler();
23+
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
24+
NotificationChannelBuilder builder = new NotificationChannelBuilder();
25+
if (notificationHubUtil.hasChannelImportance(context)) {
26+
builder.setImportance(notificationHubUtil.getChannelImportance(context));
27+
}
28+
29+
if (notificationHubUtil.hasChannelShowBadge(context)) {
30+
builder.setShowBadge(notificationHubUtil.getChannelShowBadge(context));
31+
}
32+
33+
if (notificationHubUtil.hasChannelEnableLights(context)) {
34+
builder.enableLights(notificationHubUtil.getChannelEnableLights(context));
35+
}
36+
37+
if (notificationHubUtil.hasChannelEnableVibration(context)) {
38+
builder.enableVibration(notificationHubUtil.getChannelEnableVibration(context));
39+
}
40+
41+
NotificationChannel channel = builder.build();
42+
notificationHandler.createChannelAndHandleNotifications(context, channel);
43+
}
44+
}
1145

1246
@Override
1347
public void onNewToken(String token) {
@@ -16,4 +50,18 @@ public void onNewToken(String token) {
1650
Intent intent = new Intent(this, ReactNativeRegistrationIntentService.class);
1751
startService(intent);
1852
}
53+
54+
@Override
55+
public void onMessageReceived(RemoteMessage remoteMessage) {
56+
Log.d(TAG, "From: " + remoteMessage.getFrom());
57+
58+
if (notificationHandler == null) {
59+
Log.e(TAG, "Notification handler hasn't been created");
60+
return;
61+
}
62+
63+
Bundle bundle = remoteMessage.toIntent().getExtras();
64+
notificationHandler.sendNotification(bundle);
65+
notificationHandler.sendBroadcast(bundle, 0);
66+
}
1967
}

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.facebook.react.bridge.ReadableArray;
2525
import com.facebook.react.bridge.ReadableMap;
2626
import com.facebook.react.bridge.UiThreadUtil;
27-
import com.microsoft.windowsazure.notifications.NotificationsManager;
2827

2928
public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
3029
public static final String AZURE_NOTIFICATION_HUB_NAME = "AzureNotificationHub";
@@ -97,8 +96,29 @@ public void register(ReadableMap config, Promise promise) {
9796
ReactContext reactContext = getReactApplicationContext();
9897
notificationHubUtil.setConnectionString(reactContext, connectionString);
9998
notificationHubUtil.setHubName(reactContext, hubName);
99+
notificationHubUtil.setSenderID(reactContext, senderID);
100100
notificationHubUtil.setTags(reactContext, tags);
101101

102+
if (config.hasKey("channelImportance")) {
103+
int channelImportance = config.getInt("channelImportance");
104+
notificationHubUtil.setChannelImportance(reactContext, channelImportance);
105+
}
106+
107+
if (config.hasKey("channelShowBadge")) {
108+
boolean channelShowBadge = config.getBoolean("channelShowBadge");
109+
notificationHubUtil.setChannelShowBadge(reactContext, channelShowBadge);
110+
}
111+
112+
if (config.hasKey("channelEnableLights")) {
113+
boolean channelEnableLights = config.getBoolean("channelEnableLights");
114+
notificationHubUtil.setChannelEnableLights(reactContext, channelEnableLights);
115+
}
116+
117+
if (config.hasKey("channelEnableVibration")) {
118+
boolean channelEnableVibration = config.getBoolean("channelEnableVibration");
119+
notificationHubUtil.setChannelEnableVibration(reactContext, channelEnableVibration);
120+
}
121+
102122
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
103123
int resultCode = apiAvailability.isGooglePlayServicesAvailable(reactContext);
104124
if (resultCode != ConnectionResult.SUCCESS) {
@@ -137,7 +157,6 @@ public void unregister(Promise promise) {
137157
try {
138158
hub.unregister();
139159
notificationHubUtil.setRegistrationID(reactContext, null);
140-
NotificationsManager.stopHandlingNotifications(reactContext);
141160
} catch (Exception e) {
142161
promise.reject(ERROR_NOTIFICATION_HUB, e);
143162
}
@@ -151,7 +170,7 @@ public void onHostResume() {
151170
if (intent != null) {
152171
Bundle bundle = intent.getBundleExtra("notification");
153172
if (bundle != null) {
154-
new ReactNativeNotificationsHandler().sendBroadcast(mReactContext, bundle, NOTIFICATION_DELAY_ON_START);
173+
new ReactNativeNotificationsHandler().sendBroadcast(bundle, NOTIFICATION_DELAY_ON_START);
155174
}
156175
}
157176
}

0 commit comments

Comments
 (0)