Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 2254fd4

Browse files
committed
Move back onNewToken to Firebase service.
1 parent 419dfa4 commit 2254fd4

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,61 @@
33
import com.google.firebase.messaging.FirebaseMessagingService;
44
import com.google.firebase.messaging.RemoteMessage;
55

6+
import android.os.Handler;
7+
import android.os.Looper;
8+
import android.util.Log;
9+
import android.support.annotation.NonNull;
10+
611
import com.dieam.reactnativepushnotification.modules.RNReceivedMessageHandler;
12+
import com.facebook.react.ReactApplication;
13+
import com.facebook.react.ReactInstanceManager;
14+
import com.facebook.react.bridge.Arguments;
15+
import com.facebook.react.bridge.ReactApplicationContext;
16+
import com.facebook.react.bridge.ReactContext;
17+
import com.facebook.react.bridge.WritableMap;
18+
19+
import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
720

821
public class RNPushNotificationListenerService extends FirebaseMessagingService {
922

1023
private RNReceivedMessageHandler mMessageReceivedHandler = new RNReceivedMessageHandler(this);
1124

1225
@Override
1326
public void onNewToken(String token) {
14-
mMessageReceivedHandler.onNewToken(token);
27+
final String deviceToken = token;
28+
Log.d(LOG_TAG, "Refreshed token: " + deviceToken);
29+
30+
Handler handler = new Handler(Looper.getMainLooper());
31+
handler.post(new Runnable() {
32+
public void run() {
33+
// Construct and load our normal React JS code bundle
34+
ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
35+
ReactContext context = mReactInstanceManager.getCurrentReactContext();
36+
// If it's constructed, send a notification
37+
if (context != null) {
38+
handleNewToken((ReactApplicationContext) context, deviceToken);
39+
} else {
40+
// Otherwise wait for construction, then send the notification
41+
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
42+
public void onReactContextInitialized(ReactContext context) {
43+
handleNewToken((ReactApplicationContext) context, deviceToken);
44+
}
45+
});
46+
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
47+
// Construct it in the background
48+
mReactInstanceManager.createReactContextInBackground();
49+
}
50+
}
51+
}
52+
});
53+
}
54+
55+
private void handleNewToken(ReactApplicationContext context, String token) {
56+
RNPushNotificationJsDelivery jsDelivery = new RNPushNotificationJsDelivery(context);
57+
58+
WritableMap params = Arguments.createMap();
59+
params.putString("deviceToken", token);
60+
jsDelivery.sendEvent("remoteNotificationsRegistered", params);
1561
}
1662

1763
@Override

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

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.dieam.reactnativepushnotification.modules;
22

3-
import java.util.Map;
43
import com.google.firebase.messaging.FirebaseMessagingService;
54
import com.google.firebase.messaging.RemoteMessage;
65

@@ -23,58 +22,20 @@
2322

2423
import org.json.JSONObject;
2524

25+
import java.util.Map;
2626
import java.util.List;
2727
import java.util.Random;
2828

2929
import static android.content.Context.ACTIVITY_SERVICE;
3030
import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
3131

32-
33-
3432
public class RNReceivedMessageHandler {
3533
private FirebaseMessagingService mFirebaseMessagingService;
3634

3735
public RNReceivedMessageHandler(@NonNull FirebaseMessagingService service) {
3836
this.mFirebaseMessagingService = service;
3937
}
4038

41-
public void onNewToken(String token) {
42-
final String deviceToken = token;
43-
Log.d(LOG_TAG, "Refreshed token: " + deviceToken);
44-
45-
Handler handler = new Handler(Looper.getMainLooper());
46-
handler.post(new Runnable() {
47-
public void run() {
48-
// Construct and load our normal React JS code bundle
49-
ReactInstanceManager mReactInstanceManager = ((ReactApplication) mFirebaseMessagingService.getApplication()).getReactNativeHost().getReactInstanceManager();
50-
ReactContext context = mReactInstanceManager.getCurrentReactContext();
51-
// If it's constructed, send a notification
52-
if (context != null) {
53-
handleNewToken((ReactApplicationContext) context, deviceToken);
54-
} else {
55-
// Otherwise wait for construction, then send the notification
56-
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
57-
public void onReactContextInitialized(ReactContext context) {
58-
handleNewToken((ReactApplicationContext) context, deviceToken);
59-
}
60-
});
61-
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
62-
// Construct it in the background
63-
mReactInstanceManager.createReactContextInBackground();
64-
}
65-
}
66-
}
67-
});
68-
}
69-
70-
private void handleNewToken(ReactApplicationContext context, String token) {
71-
RNPushNotificationJsDelivery jsDelivery = new RNPushNotificationJsDelivery(context);
72-
73-
WritableMap params = Arguments.createMap();
74-
params.putString("deviceToken", token);
75-
jsDelivery.sendEvent("remoteNotificationsRegistered", params);
76-
}
77-
7839
public void handleReceivedMessage(RemoteMessage message) {
7940
String from = message.getFrom();
8041
RemoteMessage.Notification remoteNotification = message.getNotification();

0 commit comments

Comments
 (0)