Skip to content

Commit 4899349

Browse files
committed
Implement onNewToken for Firebase.
1 parent adac27b commit 4899349

File tree

5 files changed

+105
-31
lines changed

5 files changed

+105
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ PushNotification.configure({
217217

218218
// process the notification
219219

220-
// required on iOS only (see fetchCompletionHandler docs: https://github.com/react-native-community/react-native-push-notification-ios)
220+
// (required) Called when a remote is received or opened, or local notification is opened
221221
notification.finish(PushNotificationIOS.FetchResult.NoData);
222222
},
223223

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationListenerService.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
1616
import com.facebook.react.ReactApplication;
1717
import com.facebook.react.ReactInstanceManager;
18+
import com.facebook.react.bridge.Arguments;
1819
import com.facebook.react.bridge.ReactApplicationContext;
1920
import com.facebook.react.bridge.ReactContext;
21+
import com.facebook.react.bridge.WritableMap;
2022

2123
import org.json.JSONObject;
2224

@@ -27,6 +29,45 @@
2729

2830
public class RNPushNotificationListenerService extends FirebaseMessagingService {
2931

32+
@Override
33+
public void onNewToken(String token) {
34+
final String deviceToken = token;
35+
Log.d(LOG_TAG, "Refreshed token: " + deviceToken);
36+
37+
Handler handler = new Handler(Looper.getMainLooper());
38+
handler.post(new Runnable() {
39+
public void run() {
40+
// Construct and load our normal React JS code bundle
41+
ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
42+
ReactContext context = mReactInstanceManager.getCurrentReactContext();
43+
// If it's constructed, send a notificationre
44+
if (context != null) {
45+
handleNewToken((ReactApplicationContext) context, deviceToken);
46+
} else {
47+
// Otherwise wait for construction, then send the notification
48+
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
49+
public void onReactContextInitialized(ReactContext context) {
50+
handleNewToken((ReactApplicationContext) context, deviceToken);
51+
}
52+
});
53+
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
54+
// Construct it in the background
55+
mReactInstanceManager.createReactContextInBackground();
56+
}
57+
}
58+
}
59+
});
60+
}
61+
62+
private void handleNewToken(ReactApplicationContext context, String token) {
63+
RNPushNotificationJsDelivery jsDelivery = new RNPushNotificationJsDelivery(context);
64+
65+
WritableMap params = Arguments.createMap();
66+
params.putString("deviceToken", token);
67+
jsDelivery.sendEvent("remoteNotificationsRegistered", params);
68+
}
69+
70+
3071
@Override
3172
public void onMessageReceived(RemoteMessage message) {
3273
String from = message.getFrom();
@@ -87,7 +128,7 @@ public void run() {
87128
// Construct and load our normal React JS code bundle
88129
ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
89130
ReactContext context = mReactInstanceManager.getCurrentReactContext();
90-
// If it's constructed, send a notification
131+
// If it's constructed, send a notificationre
91132
if (context != null) {
92133
handleRemotePushNotification((ReactApplicationContext) context, bundle);
93134
} else {

example/NotifService.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,12 @@
11
import PushNotification from 'react-native-push-notification';
2+
import NotificationHandler from './NotificationHandler';
23

34
export default class NotifService {
45
constructor(onRegister, onNotification) {
5-
this.configure(onRegister, onNotification);
6-
76
this.lastId = 0;
8-
}
9-
10-
configure(onRegister, onNotification) {
11-
PushNotification.configure({
12-
// (optional) Called when Token is generated (iOS and Android)
13-
onRegister: onRegister, //this._onRegister.bind(this),
14-
15-
// (required) Called when a remote or local notification is opened or received
16-
onNotification: onNotification, //this._onNotification,
177

18-
// IOS ONLY (optional): default: all - Permissions to register.
19-
permissions: {
20-
alert: true,
21-
badge: true,
22-
sound: true,
23-
},
24-
25-
// Should the initial notification be popped automatically
26-
// default: true
27-
popInitialNotification: true,
28-
29-
/**
30-
* (optional) default: true
31-
* - Specified if permissions (ios) and token (android and ios) will requested or not,
32-
* - if not, you must call PushNotificationsHandler.requestPermissions() later
33-
*/
34-
requestPermissions: true,
35-
});
8+
NotificationHandler.onRegister(onRegister);
9+
NotificationHandler.attachNotification(onNotification);
3610
}
3711

3812
localNotif() {

example/NotificationHandler.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import PushNotification from 'react-native-push-notification';
2+
3+
class NotificationHandler {
4+
onNotification(notification) {
5+
console.log(notification);
6+
7+
if (typeof this._onNotification === 'function') {
8+
this._onNotification(notification);
9+
}
10+
}
11+
12+
onRegister(token) {
13+
console.log(token);
14+
15+
if (typeof this._onRegister === 'function') {
16+
this._onRegister(token);
17+
}
18+
}
19+
20+
attachRegister(handler) {
21+
this._onRegister = handler;
22+
}
23+
24+
attachNotification(handler) {
25+
this._onNotification = handler;
26+
}
27+
}
28+
29+
const handler = new NotificationHandler();
30+
31+
PushNotification.configure({
32+
// (optional) Called when Token is generated (iOS and Android)
33+
onRegister: handler.onRegister.bind(handler), //this._onRegister.bind(this),
34+
35+
// (required) Called when a remote or local notification is opened or received
36+
onNotification: handler.onNotification.bind(handler), //this._onNotification,
37+
38+
// IOS ONLY (optional): default: all - Permissions to register.
39+
permissions: {
40+
alert: true,
41+
badge: true,
42+
sound: true,
43+
},
44+
45+
// Should the initial notification be popped automatically
46+
// default: true
47+
popInitialNotification: true,
48+
49+
/**
50+
* (optional) default: true
51+
* - Specified if permissions (ios) and token (android and ios) will requested or not,
52+
* - if not, you must call PushNotificationsHandler.requestPermissions() later
53+
*/
54+
requestPermissions: true,
55+
});
56+
57+
export default handler;

example/android/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
</intent-filter>
4545
</service>
4646

47+
<meta-data android:name="com.dieam.reactnativepushnotification.notification_foreground"
48+
android:value="false"/>
4749
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name"
4850
android:value="Example-Channel"/>
4951
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description"

0 commit comments

Comments
 (0)