Skip to content

Commit db59563

Browse files
committed
Fixed notifcation when resuming app. Updated docs.
1 parent 2cebacf commit db59563

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ public class ReactNativeFirebaseMessagingService extends FirebaseMessagingServic
1616
private static final String TAG = "ReactNativeFMS";
1717

1818
private static ReactNativeNotificationsHandler notificationHandler;
19-
private static Context appContext;
2019
private static String notificationChannelID;
2120

2221
public static void createNotificationHandler(Context context) {
23-
appContext = context;
24-
2522
if (notificationHandler == null) {
2623
notificationHandler = new ReactNativeNotificationsHandler();
2724
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
@@ -63,15 +60,14 @@ public void onNewToken(String token) {
6360

6461
@Override
6562
public void onMessageReceived(RemoteMessage remoteMessage) {
66-
Log.d(TAG, "From: " + remoteMessage.getFrom());
63+
Log.d(TAG, "Remote message from: " + remoteMessage.getFrom());
6764

6865
if (notificationHandler == null) {
69-
Log.e(TAG, "Notification handler hasn't been created");
70-
return;
66+
createNotificationHandler(this);
7167
}
7268

7369
Bundle bundle = remoteMessage.toIntent().getExtras();
74-
notificationHandler.sendNotification(appContext, bundle, notificationChannelID);
75-
notificationHandler.sendBroadcast(appContext, bundle, 0);
70+
notificationHandler.sendNotification(this, bundle, notificationChannelID);
71+
notificationHandler.sendBroadcast(this, bundle, 0);
7672
}
7773
}

docs/android-installation.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ On the [Azure Portal](https://portal.azure.com) page for your notification hub,
167167
The example below shows how you can register and unregister from Azure Notification Hub in your React component.
168168

169169
```js
170+
import React, { Component } from 'react';
171+
import { NativeEventEmitter } from 'react-native';
172+
import {
173+
StyleSheet,
174+
Text,
175+
TouchableOpacity,
176+
View,
177+
} from 'react-native';
178+
170179
const NotificationHub = require('react-native-azurenotificationhub');
171180
const PushNotificationEmitter = new NativeEventEmitter(NotificationHub);
172181

@@ -193,7 +202,7 @@ class myapp extends Component {
193202
PushNotificationEmitter.addListener(NOTIF_REGISTER_AZURE_HUB_EVENT, this._onAzureNotificationHubRegistered);
194203
PushNotificationEmitter.addListener(NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT, this._onAzureNotificationHubRegistrationError);
195204

196-
NotificationHub.register({
205+
NotificationHub.register({
197206
connectionString,
198207
hubName,
199208
senderID,
@@ -243,5 +252,25 @@ class myapp extends Component {
243252
_onRemoteNotification(notification) {
244253
// Note notification will be a JSON string for android
245254
console.warn('Notification received: ' + notification);
246-
}
255+
}
256+
}
257+
258+
const styles = StyleSheet.create({
259+
container: {
260+
flex: 1,
261+
justifyContent: 'center',
262+
alignItems: 'center',
263+
backgroundColor: '#F5FCFF',
264+
},
265+
welcome: {
266+
fontSize: 20,
267+
textAlign: 'center',
268+
margin: 10,
269+
},
270+
instructions: {
271+
textAlign: 'center',
272+
color: '#333333',
273+
marginBottom: 5,
274+
},
275+
});
247276
```

0 commit comments

Comments
 (0)