Skip to content

Commit 79c7c80

Browse files
authored
Merge pull request #94 from CatalystCode/thcao/new-sdk
[Android] Update SDK, support notification channel's settings and fix bugs
2 parents 6a584aa + d79bddc commit 79c7c80

13 files changed

+469
-173
lines changed

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: 93 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 = ReactNativeNotificationsHandler.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: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,52 @@
11
package com.azure.reactnative.notificationhub;
22

3+
import android.app.NotificationChannel;
4+
import android.app.NotificationManager;
5+
import android.content.Context;
36
import android.content.Intent;
7+
import android.os.Build;
8+
import android.os.Bundle;
49
import android.util.Log;
510

611
import com.google.firebase.messaging.FirebaseMessagingService;
12+
import com.google.firebase.messaging.RemoteMessage;
713

814
public class ReactNativeFirebaseMessagingService extends FirebaseMessagingService {
915

10-
private static final String TAG = "ReactNativeFirebaseMS";
16+
private static final String TAG = "ReactNativeFMS";
17+
18+
private static String notificationChannelID;
19+
20+
public static void createNotificationChannel(Context context) {
21+
if (notificationChannelID == null) {
22+
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
23+
NotificationChannelBuilder builder = new NotificationChannelBuilder();
24+
if (notificationHubUtil.hasChannelImportance(context)) {
25+
builder.setImportance(notificationHubUtil.getChannelImportance(context));
26+
}
27+
28+
if (notificationHubUtil.hasChannelShowBadge(context)) {
29+
builder.setShowBadge(notificationHubUtil.getChannelShowBadge(context));
30+
}
31+
32+
if (notificationHubUtil.hasChannelEnableLights(context)) {
33+
builder.enableLights(notificationHubUtil.getChannelEnableLights(context));
34+
}
35+
36+
if (notificationHubUtil.hasChannelEnableVibration(context)) {
37+
builder.enableVibration(notificationHubUtil.getChannelEnableVibration(context));
38+
}
39+
40+
notificationChannelID = ReactNativeNotificationsHandler.NOTIFICATION_CHANNEL_ID;
41+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
42+
NotificationChannel channel = builder.build();
43+
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
44+
Context.NOTIFICATION_SERVICE);
45+
notificationManager.createNotificationChannel(channel);
46+
notificationChannelID = channel.getId();
47+
}
48+
}
49+
}
1150

1251
@Override
1352
public void onNewToken(String token) {
@@ -16,4 +55,17 @@ public void onNewToken(String token) {
1655
Intent intent = new Intent(this, ReactNativeRegistrationIntentService.class);
1756
startService(intent);
1857
}
58+
59+
@Override
60+
public void onMessageReceived(RemoteMessage remoteMessage) {
61+
Log.d(TAG, "Remote message from: " + remoteMessage.getFrom());
62+
63+
if (notificationChannelID == null) {
64+
createNotificationChannel(this);
65+
}
66+
67+
Bundle bundle = remoteMessage.toIntent().getExtras();
68+
ReactNativeNotificationsHandler.sendNotification(this, bundle, notificationChannelID);
69+
ReactNativeNotificationsHandler.sendBroadcast(this, bundle, 0);
70+
}
1971
}

0 commit comments

Comments
 (0)