Skip to content

Commit bdf8825

Browse files
committed
Fixed several issues with 'notification' payload type.
1 parent b1fd6f0 commit bdf8825

File tree

8 files changed

+164
-69
lines changed

8 files changed

+164
-69
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ public final class ReactNativeConstants {
44
// Notification
55
public static final String AZURE_NOTIFICATION_HUB_NAME = "AzureNotificationHub";
66
public static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id";
7+
public static final String KEY_NOTIFICATION_PAYLOAD_TYPE = "notification";
8+
public static final String KEY_DATA_PAYLOAD_TYPE = "data";
79

810
// Notification hub events
911
public static final String EVENT_REMOTE_NOTIFICATION_RECEIVED = "remoteNotificationReceived";
@@ -37,6 +39,7 @@ public final class ReactNativeConstants {
3739

3840
// Remote notification payload
3941
public static final String KEY_REMOTE_NOTIFICATION_MESSAGE = "message";
42+
public static final String KEY_REMOTE_NOTIFICATION_BODY = "body";
4043
public static final String KEY_REMOTE_NOTIFICATION_ID = "google.message_id";
4144
public static final String KEY_REMOTE_NOTIFICATION_TITLE = "title";
4245
public static final String KEY_REMOTE_NOTIFICATION_PRIORITY = "google.original_priority";
@@ -69,7 +72,6 @@ public final class ReactNativeConstants {
6972
public static final String REMOTE_NOTIFICATION_PRIORITY_NORMAL = "normal";
7073

7174
// Intent
72-
public static final String KEY_INTENT_NOTIFICATION = "notification";
7375
public static final String KEY_INTENT_EVENT_NAME = "eventName";
7476
public static final String KEY_INTENT_EVENT_TYPE = "eventType";
7577
public static final String KEY_INTENT_EVENT_STRING_DATA = "eventStringData";
@@ -85,7 +87,6 @@ public final class ReactNativeConstants {
8587
// Errors
8688
public static final String ERROR_NO_ACTIVITY_CLASS = "No activity class found for the notification";
8789
public static final String ERROR_NO_MESSAGE = "No message specified for the notification";
88-
public static final String ERROR_NO_NOTIF_ID = "No notification ID specified for the notification";
8990
public static final String ERROR_COVERT_ACTIONS = "Exception while converting actions to JSON object.";
9091
public static final String ERROR_GET_ACTIONS_ARRAY = "Exception while getting action from actionsArray.";
9192
public static final String ERROR_SEND_PUSH_NOTIFICATION = "failed to send push notification";

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ public static void createNotificationChannel(Context context) {
4949
NotificationChannel channel = builder.build();
5050
NotificationManager notificationManager = (NotificationManager) context.getSystemService(
5151
Context.NOTIFICATION_SERVICE);
52-
notificationManager.createNotificationChannel(channel);
53-
notificationChannelID = channel.getId();
52+
if (notificationManager != null) {
53+
notificationManager.createNotificationChannel(channel);
54+
notificationChannelID = channel.getId();
55+
}
5456
}
5557
}
5658
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,12 @@ public void onHostResume() {
174174
if (activity != null) {
175175
Intent intent = activity.getIntent();
176176
if (intent != null) {
177-
Bundle bundle = intent.getBundleExtra(KEY_INTENT_NOTIFICATION);
177+
Bundle bundle = ReactNativeUtil.getBundleFromIntent(intent);
178178
if (bundle != null) {
179-
intent.removeExtra(KEY_INTENT_NOTIFICATION);
179+
if (intent.hasExtra(KEY_NOTIFICATION_PAYLOAD_TYPE)) {
180+
intent.removeExtra(KEY_NOTIFICATION_PAYLOAD_TYPE);
181+
}
182+
180183
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, false);
181184
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, true);
182185
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_COLDSTART, true);
@@ -198,7 +201,7 @@ public void onHostDestroy() {
198201

199202
@Override
200203
public void onNewIntent(Intent intent) {
201-
Bundle bundle = intent.getBundleExtra(KEY_INTENT_NOTIFICATION);
204+
Bundle bundle = ReactNativeUtil.getBundleFromIntent(intent);
202205
if (bundle != null) {
203206
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, false);
204207
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, true);

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public final class ReactNativeNotificationsHandler {
2626

2727
private static final long DEFAULT_VIBRATION = 300L;
2828

29+
/**
30+
* Used for both "notification" and "data" payload types in order to notify a running ReactJS app.
31+
*
32+
* Example:
33+
* {"data":{"message":"Notification Hub test notification"}} // data
34+
* {"notification":{"body":"Notification Hub test notification"}} // notification
35+
*/
2936
public static void sendBroadcast(final Context context, final Intent intent, final long delay) {
3037
ReactNativeUtil.runInWorkerThread(new Runnable() {
3138
public void run() {
@@ -39,6 +46,13 @@ public void run() {
3946
});
4047
}
4148

49+
/**
50+
* Used for both "notification" and "data" payload types in order to notify a running ReactJS app.
51+
*
52+
* Example:
53+
* {"data":{"message":"Notification Hub test notification"}} // data
54+
* {"notification":{"body":"Notification Hub test notification"}} // notification
55+
*/
4256
public static void sendBroadcast(final Context context, final Bundle bundle, final long delay) {
4357
ReactNativeUtil.runInWorkerThread(new Runnable() {
4458
public void run() {
@@ -53,6 +67,12 @@ public void run() {
5367
});
5468
}
5569

70+
/**
71+
* Used for "data" payload type in order to create a notification and announce it using
72+
* notification service.
73+
*
74+
* Example: {"data":{"message":"Notification Hub test notification"}}
75+
*/
5676
public static void sendNotification(Context context, Bundle bundle, String notificationChannelID) {
5777
try {
5878
Class intentClass = ReactNativeUtil.getMainActivityClass(context);
@@ -61,14 +81,13 @@ public static void sendNotification(Context context, Bundle bundle, String notif
6181
return;
6282
}
6383

64-
if (bundle.getString(KEY_REMOTE_NOTIFICATION_MESSAGE) == null) {
65-
Log.e(TAG, ERROR_NO_MESSAGE);
66-
return;
84+
String message = bundle.getString(KEY_REMOTE_NOTIFICATION_MESSAGE);
85+
if (message == null) {
86+
message = bundle.getString(KEY_REMOTE_NOTIFICATION_BODY);
6787
}
6888

69-
String notificationIdString = bundle.getString(KEY_REMOTE_NOTIFICATION_ID);
70-
if (notificationIdString == null) {
71-
Log.e(TAG, ERROR_NO_NOTIF_ID);
89+
if (message == null) {
90+
Log.e(TAG, ERROR_NO_MESSAGE);
7291
return;
7392
}
7493

@@ -97,7 +116,7 @@ public static void sendNotification(Context context, Bundle bundle, String notif
97116
notificationBuilder.setGroup(group);
98117
}
99118

100-
notificationBuilder.setContentText(bundle.getString(KEY_REMOTE_NOTIFICATION_MESSAGE));
119+
notificationBuilder.setContentText(message);
101120

102121
String subText = bundle.getString(KEY_REMOTE_NOTIFICATION_SUB_TEXT);
103122
if (subText != null) {
@@ -121,10 +140,9 @@ public static void sendNotification(Context context, Bundle bundle, String notif
121140

122141
String bigText = bundle.getString(KEY_REMOTE_NOTIFICATION_BIG_TEXT);
123142
if (bigText == null) {
124-
bigText = bundle.getString(KEY_REMOTE_NOTIFICATION_MESSAGE);
143+
bigText = message;
125144
}
126-
127-
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));
145+
notificationBuilder.setStyle(ReactNativeUtil.getBigTextStyle(bigText));
128146

129147
// Create notification intent
130148
Intent intent = ReactNativeUtil.createNotificationIntent(context, bundle, intentClass);
@@ -147,11 +165,9 @@ public static void sendNotification(Context context, Bundle bundle, String notif
147165
}
148166
}
149167

150-
int notificationID = notificationIdString.hashCode();
151-
168+
int notificationID = bundle.getString(KEY_REMOTE_NOTIFICATION_ID).hashCode();
152169
PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationID, intent,
153170
PendingIntent.FLAG_UPDATE_CURRENT);
154-
155171
notificationBuilder.setContentIntent(pendingIntent);
156172

157173
if (!bundle.containsKey(KEY_REMOTE_NOTIFICATION_VIBRATE) || bundle.getBoolean(KEY_REMOTE_NOTIFICATION_VIBRATE)) {

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public static Intent createNotificationIntent(Context context, Bundle bundle, Cl
229229
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, true);
230230
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, false);
231231
bundle.putBoolean(KEY_REMOTE_NOTIFICATION_COLDSTART, false);
232-
intent.putExtra(KEY_INTENT_NOTIFICATION, bundle);
232+
intent.putExtra(KEY_NOTIFICATION_PAYLOAD_TYPE, bundle);
233233

234234
return intent;
235235
}
@@ -263,7 +263,7 @@ public static void processNotificationActions(Context context, Bundle bundle,
263263
actionIntent.setAction(context.getPackageName() + "." + action);
264264
// Add "action" for later identifying which button gets pressed.
265265
bundle.putString(KEY_REMOTE_NOTIFICATION_ACTION, action);
266-
actionIntent.putExtra(KEY_INTENT_NOTIFICATION, bundle);
266+
actionIntent.putExtra(KEY_NOTIFICATION_PAYLOAD_TYPE, bundle);
267267
PendingIntent pendingActionIntent = PendingIntent.getBroadcast(context, notificationID, actionIntent,
268268
PendingIntent.FLAG_UPDATE_CURRENT);
269269
notification.addAction(icon, action, pendingActionIntent);
@@ -288,6 +288,21 @@ public static NotificationCompat.Builder initNotificationCompatBuilder(Context c
288288
return notificationBuilder;
289289
}
290290

291+
public static Bundle getBundleFromIntent(Intent intent) {
292+
Bundle bundle = null;
293+
if (intent.hasExtra(KEY_NOTIFICATION_PAYLOAD_TYPE)) {
294+
bundle = intent.getBundleExtra(KEY_NOTIFICATION_PAYLOAD_TYPE);
295+
} else if (intent.hasExtra(KEY_REMOTE_NOTIFICATION_ID)) {
296+
bundle = intent.getExtras();
297+
}
298+
299+
return bundle;
300+
}
301+
302+
public static NotificationCompat.BigTextStyle getBigTextStyle(String bigText) {
303+
return new NotificationCompat.BigTextStyle().bigText(bigText);
304+
}
305+
291306
private ReactNativeUtil() {
292307
}
293308
}

sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeNotificationHubModuleTest.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,18 +359,55 @@ public void testUnregisterThrowException() throws Exception {
359359
}
360360

361361
@Test
362-
public void testOnHostResume() {
362+
public void testOnHostResumeNoNotification() {
363+
Activity activity = PowerMockito.mock(Activity.class);
364+
Intent intent = PowerMockito.mock(Intent.class);
365+
when(mReactApplicationContext.getCurrentActivity()).thenReturn(activity);
366+
when(activity.getIntent()).thenReturn(intent);
367+
368+
mHubModule.onHostResume();
369+
370+
verify(mNotificationHubUtil, times(1)).setAppIsForeground(true);
371+
verify(intent, times(0)).hasExtra(KEY_NOTIFICATION_PAYLOAD_TYPE);
372+
}
373+
374+
@Test
375+
public void testOnHostResumeNotificationPayload() {
376+
Activity activity = PowerMockito.mock(Activity.class);
377+
Intent intent = PowerMockito.mock(Intent.class);
378+
Bundle bundle = PowerMockito.mock(Bundle.class);
379+
when(mReactApplicationContext.getCurrentActivity()).thenReturn(activity);
380+
when(activity.getIntent()).thenReturn(intent);
381+
when(intent.hasExtra(KEY_NOTIFICATION_PAYLOAD_TYPE)).thenReturn(true);
382+
when(ReactNativeUtil.getBundleFromIntent(intent)).thenReturn(bundle);
383+
384+
mHubModule.onHostResume();
385+
386+
verify(mNotificationHubUtil, times(1)).setAppIsForeground(true);
387+
verify(intent, times(1)).hasExtra(KEY_NOTIFICATION_PAYLOAD_TYPE);
388+
verify(intent, times(1)).removeExtra(KEY_NOTIFICATION_PAYLOAD_TYPE);
389+
verify(bundle, times(1)).putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, false);
390+
verify(bundle, times(1)).putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, true);
391+
verify(bundle, times(1)).putBoolean(KEY_REMOTE_NOTIFICATION_COLDSTART, true);
392+
PowerMockito.verifyStatic(ReactNativeNotificationsHandler.class);
393+
ReactNativeNotificationsHandler.sendBroadcast(eq(mReactApplicationContext), eq(bundle), anyLong());
394+
}
395+
396+
@Test
397+
public void testOnHostResumeDataPayload() {
363398
Activity activity = PowerMockito.mock(Activity.class);
364399
Intent intent = PowerMockito.mock(Intent.class);
365400
Bundle bundle = PowerMockito.mock(Bundle.class);
366401
when(mReactApplicationContext.getCurrentActivity()).thenReturn(activity);
367402
when(activity.getIntent()).thenReturn(intent);
368-
when(intent.getBundleExtra(KEY_INTENT_NOTIFICATION)).thenReturn(bundle);
403+
when(intent.hasExtra(KEY_NOTIFICATION_PAYLOAD_TYPE)).thenReturn(false);
404+
when(ReactNativeUtil.getBundleFromIntent(intent)).thenReturn(bundle);
369405

370406
mHubModule.onHostResume();
371407

372408
verify(mNotificationHubUtil, times(1)).setAppIsForeground(true);
373-
verify(intent, times(1)).removeExtra(KEY_INTENT_NOTIFICATION);
409+
verify(intent, times(1)).hasExtra(KEY_NOTIFICATION_PAYLOAD_TYPE);
410+
verify(intent, times(0)).removeExtra(KEY_NOTIFICATION_PAYLOAD_TYPE);
374411
verify(bundle, times(1)).putBoolean(KEY_REMOTE_NOTIFICATION_FOREGROUND, false);
375412
verify(bundle, times(1)).putBoolean(KEY_REMOTE_NOTIFICATION_USER_INTERACTION, true);
376413
verify(bundle, times(1)).putBoolean(KEY_REMOTE_NOTIFICATION_COLDSTART, true);
@@ -389,7 +426,7 @@ public void testOnHostPause() {
389426
public void testOnNewIntent() {
390427
Intent intent = PowerMockito.mock(Intent.class);
391428
Bundle bundle = PowerMockito.mock(Bundle.class);
392-
when(intent.getBundleExtra(KEY_INTENT_NOTIFICATION)).thenReturn(bundle);
429+
when(ReactNativeUtil.getBundleFromIntent(intent)).thenReturn(bundle);
393430

394431
mHubModule.onNewIntent(intent);
395432

0 commit comments

Comments
 (0)