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

Commit f19d05d

Browse files
committed
Use SecureRandom and remove onNotification call for Scheduled notifications when there is no user interation.
1 parent f8b5710 commit f19d05d

File tree

3 files changed

+6
-55
lines changed

3 files changed

+6
-55
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Breaking changes
1111

12-
- Now local scheduled notifications trigger `onNotification` before display [#574](https://github.com/zo0r/react-native-push-notification/pull/574).
1312
- `RNPushNotificationRegistrationService` has been removed, old reference in AndroidManifest must be removed.
1413
- `Notifications.registerNotificationActions()` has been removed and is not required for `actions`.
1514
- `DeviceEventEmitter.addListener('notificationActionReceived', callback)` is replaced by `onAction`.

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

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
package com.dieam.reactnativepushnotification.modules;
22

3-
import android.app.ActivityManager;
4-
import android.app.ActivityManager.RunningAppProcessInfo;
53
import android.app.Application;
64
import android.content.BroadcastReceiver;
75
import android.content.Context;
86
import android.content.Intent;
97
import android.os.Bundle;
10-
import android.os.Handler;
11-
import android.os.Looper;
128
import android.util.Log;
139

14-
import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
15-
import com.facebook.react.ReactApplication;
16-
import com.facebook.react.ReactInstanceManager;
17-
import com.facebook.react.bridge.ReactApplicationContext;
18-
import com.facebook.react.bridge.ReactContext;
19-
20-
import org.json.JSONObject;
21-
2210
import java.util.List;
23-
import java.util.Random;
11+
import java.security.SecureRandom;
2412

2513
import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
2614

@@ -38,40 +26,14 @@ public void onReceive(final Context context, Intent intent) {
3826

3927
Log.v(LOG_TAG, "onMessageReceived: " + bundle);
4028

41-
// We need to run this on the main thread, as the React code assumes that is true.
42-
// Namely, DevServerHelper constructs a Handler() without a Looper, which triggers:
43-
// "Can't create handler inside thread that has not called Looper.prepare()"
44-
Handler handler = new Handler(Looper.getMainLooper());
45-
handler.post(new Runnable() {
46-
public void run() {
47-
// Construct and load our normal React JS code bundle
48-
final ReactInstanceManager mReactInstanceManager = ((ReactApplication) context.getApplicationContext()).getReactNativeHost().getReactInstanceManager();
49-
ReactContext context = mReactInstanceManager.getCurrentReactContext();
50-
// If it's constructed, send a notification
51-
if (context != null) {
52-
handleLocalNotification((ReactApplicationContext) context, bundle);
53-
} else {
54-
// Otherwise wait for construction, then send the notification
55-
mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
56-
public void onReactContextInitialized(ReactContext context) {
57-
handleLocalNotification((ReactApplicationContext) context, bundle);
58-
mReactInstanceManager.removeReactInstanceEventListener(this);
59-
}
60-
});
61-
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
62-
// Construct it in the background
63-
mReactInstanceManager.createReactContextInBackground();
64-
}
65-
}
66-
}
67-
});
29+
handleLocalNotification(context, bundle);
6830
}
6931

70-
private void handleLocalNotification(ReactApplicationContext context, Bundle bundle) {
32+
private void handleLocalNotification(Context context, Bundle bundle) {
7133

7234
// If notification ID is not provided by the user for push notification, generate one at random
7335
if (bundle.getString("id") == null) {
74-
Random randomNumberGenerator = new Random(System.currentTimeMillis());
36+
SecureRandom randomNumberGenerator = new SecureRandom();
7537
bundle.putString("id", String.valueOf(randomNumberGenerator.nextInt()));
7638
}
7739

@@ -80,16 +42,6 @@ private void handleLocalNotification(ReactApplicationContext context, Bundle bun
8042

8143
boolean isForeground = pushNotificationHelper.isApplicationInForeground(applicationContext);
8244

83-
RNPushNotificationJsDelivery jsDelivery = new RNPushNotificationJsDelivery(context);
84-
bundle.putBoolean("foreground", isForeground);
85-
bundle.putBoolean("userInteraction", false);
86-
jsDelivery.notifyNotification(bundle);
87-
88-
// If contentAvailable is set to true, then send out a remote fetch event
89-
if (bundle.getString("contentAvailable", "false").equalsIgnoreCase("true")) {
90-
jsDelivery.notifyRemoteFetch(bundle);
91-
}
92-
9345
Log.v(LOG_TAG, "sendNotification: " + bundle);
9446

9547
pushNotificationHelper.sendToNotificationCentre(bundle);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import java.util.Map;
2424
import java.util.List;
25-
import java.util.Random;
25+
import java.security.SecureRandom;
2626

2727
import static android.content.Context.ACTIVITY_SERVICE;
2828
import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
@@ -125,7 +125,7 @@ private void handleRemotePushNotification(ReactApplicationContext context, Bundl
125125

126126
// If notification ID is not provided by the user for push notification, generate one at random
127127
if (bundle.getString("id") == null) {
128-
Random randomNumberGenerator = new Random(System.currentTimeMillis());
128+
SecureRandom randomNumberGenerator = new SecureRandom();
129129
bundle.putString("id", String.valueOf(randomNumberGenerator.nextInt()));
130130
}
131131

0 commit comments

Comments
 (0)