11package com .dieam .reactnativepushnotification .modules ;
22
3- import android .app .ActivityManager ;
4- import android .app .ActivityManager .RunningAppProcessInfo ;
53import android .app .Application ;
64import android .content .BroadcastReceiver ;
75import android .content .Context ;
86import android .content .Intent ;
97import android .os .Bundle ;
10- import android .os .Handler ;
11- import android .os .Looper ;
128import 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-
2210import java .util .List ;
23- import java .util . Random ;
11+ import java .security . SecureRandom ;
2412
2513import 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 );
0 commit comments