11package com .azure .reactnative .notificationhub ;
22
3- import android .app .AlarmManager ;
4- import android .app .Application ;
53import android .app .Notification ;
6- import android .app .NotificationChannel ;
74import android .app .NotificationManager ;
85import android .app .PendingIntent ;
96import android .content .Context ;
3229public class ReactNativeNotificationsHandler {
3330 public static final String TAG = "ReactNativeNotification" ;
3431
35- private static final long DEFAULT_VIBRATION = 300L ;
32+ public static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id" ;
3633
37- private static Context appContext ;
38- private static boolean channelCreated ;
39- private static NotificationChannel notificationChannel ;
34+ private static final long DEFAULT_VIBRATION = 300L ;
4035
41- public void sendBroadcast (final Bundle bundle , final long delay ) {
36+ public void sendBroadcast (final Context context , final Bundle bundle , final long delay ) {
4237 (new Thread () {
4338 public void run () {
4439 try {
@@ -55,17 +50,17 @@ public void run() {
5550 Intent event = new Intent (TAG );
5651 event .putExtra ("event" , ReactNativeNotificationHubModule .DEVICE_NOTIF_EVENT );
5752 event .putExtra ("data" , json .toString ());
58- LocalBroadcastManager localBroadcastManager = LocalBroadcastManager .getInstance (appContext );
53+ LocalBroadcastManager localBroadcastManager = LocalBroadcastManager .getInstance (context );
5954 localBroadcastManager .sendBroadcast (event );
6055 } catch (Exception e ) {
6156 }
6257 }
6358 }).start ();
6459 }
6560
66- public void sendNotification (Bundle bundle ) {
61+ public void sendNotification (Context context , Bundle bundle , String notificationChannelID ) {
6762 try {
68- Class intentClass = getMainActivityClass ();
63+ Class intentClass = getMainActivityClass (context );
6964 if (intentClass == null ) {
7065 Log .e (TAG , "No activity class found for the notification" );
7166 return ;
@@ -82,13 +77,13 @@ public void sendNotification(Bundle bundle) {
8277 return ;
8378 }
8479
85- Resources res = appContext .getResources ();
86- String packageName = appContext .getPackageName ();
80+ Resources res = context .getResources ();
81+ String packageName = context .getPackageName ();
8782
8883 String title = bundle .getString ("title" );
8984 if (title == null ) {
90- ApplicationInfo appInfo = appContext .getApplicationInfo ();
91- title = appContext .getPackageManager ().getApplicationLabel (appInfo ).toString ();
85+ ApplicationInfo appInfo = context .getApplicationInfo ();
86+ title = context .getPackageManager ().getApplicationLabel (appInfo ).toString ();
9287 }
9388
9489 int priority = NotificationCompat .PRIORITY_DEFAULT ;
@@ -113,7 +108,7 @@ public void sendNotification(Bundle bundle) {
113108 }
114109 }
115110
116- NotificationCompat .Builder notification = new NotificationCompat .Builder (appContext , notificationChannel . getId () )
111+ NotificationCompat .Builder notification = new NotificationCompat .Builder (context , notificationChannelID )
117112 .setContentTitle (title )
118113 .setTicker (bundle .getString ("ticker" ))
119114 .setVisibility (NotificationCompat .VISIBILITY_PRIVATE )
@@ -180,7 +175,7 @@ public void sendNotification(Bundle bundle) {
180175
181176 notification .setStyle (new NotificationCompat .BigTextStyle ().bigText (bigText ));
182177
183- Intent intent = new Intent (appContext , intentClass );
178+ Intent intent = new Intent (context , intentClass );
184179 intent .addFlags (Intent .FLAG_ACTIVITY_SINGLE_TOP );
185180 bundle .putBoolean ("userInteraction" , true );
186181 intent .putExtra ("notification" , bundle );
@@ -196,14 +191,14 @@ public void sendNotification(Bundle bundle) {
196191 // The reason is to make the iOS and android javascript interfaces compatible
197192
198193 int resId ;
199- if (appContext .getResources ().getIdentifier (soundName , "raw" , appContext .getPackageName ()) != 0 ) {
200- resId = appContext .getResources ().getIdentifier (soundName , "raw" , appContext .getPackageName ());
194+ if (context .getResources ().getIdentifier (soundName , "raw" , context .getPackageName ()) != 0 ) {
195+ resId = context .getResources ().getIdentifier (soundName , "raw" , context .getPackageName ());
201196 } else {
202197 soundName = soundName .substring (0 , soundName .lastIndexOf ('.' ));
203- resId = appContext .getResources ().getIdentifier (soundName , "raw" , appContext .getPackageName ());
198+ resId = context .getResources ().getIdentifier (soundName , "raw" , context .getPackageName ());
204199 }
205200
206- soundUri = Uri .parse ("android.resource://" + appContext .getPackageName () + "/" + resId );
201+ soundUri = Uri .parse ("android.resource://" + context .getPackageName () + "/" + resId );
207202 }
208203 }
209204 notification .setSound (soundUri );
@@ -224,7 +219,7 @@ public void sendNotification(Bundle bundle) {
224219
225220 int notificationID = notificationIdString .hashCode ();
226221
227- PendingIntent pendingIntent = PendingIntent .getActivity (appContext , notificationID , intent ,
222+ PendingIntent pendingIntent = PendingIntent .getActivity (context , notificationID , intent ,
228223 PendingIntent .FLAG_UPDATE_CURRENT );
229224
230225 notification .setContentIntent (pendingIntent );
@@ -258,18 +253,19 @@ public void sendNotification(Bundle bundle) {
258253 }
259254
260255 Intent actionIntent = new Intent ();
261- actionIntent .setAction (appContext .getPackageName () + "." + action );
256+ actionIntent .setAction (context .getPackageName () + "." + action );
262257 // Add "action" for later identifying which button gets pressed.
263258 bundle .putString ("action" , action );
264259 actionIntent .putExtra ("notification" , bundle );
265- PendingIntent pendingActionIntent = PendingIntent .getBroadcast (appContext , notificationID , actionIntent ,
260+ PendingIntent pendingActionIntent = PendingIntent .getBroadcast (context , notificationID , actionIntent ,
266261 PendingIntent .FLAG_UPDATE_CURRENT );
267262 notification .addAction (icon , action , pendingActionIntent );
268263 }
269264 }
270265
271266 Notification info = notification .build ();
272- NotificationManager notificationManager = appContext .getSystemService (NotificationManager .class );
267+ NotificationManager notificationManager = (NotificationManager ) context .getSystemService (
268+ Context .NOTIFICATION_SERVICE );
273269 if (bundle .containsKey ("tag" )) {
274270 String tag = bundle .getString ("tag" );
275271 notificationManager .notify (tag , notificationID , info );
@@ -281,19 +277,9 @@ public void sendNotification(Bundle bundle) {
281277 }
282278 }
283279
284- public void createChannelAndHandleNotifications (Context context , NotificationChannel channel ) {
285- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O && !channelCreated ) {
286- appContext = context ;
287- notificationChannel = channel ;
288- NotificationManager notificationManager = context .getSystemService (NotificationManager .class );
289- notificationManager .createNotificationChannel (channel );
290- channelCreated = true ;
291- }
292- }
293-
294- private Class getMainActivityClass () {
295- String packageName = appContext .getPackageName ();
296- Intent launchIntent = appContext .getPackageManager ().getLaunchIntentForPackage (packageName );
280+ private Class getMainActivityClass (Context context ) {
281+ String packageName = context .getPackageName ();
282+ Intent launchIntent = context .getPackageManager ().getLaunchIntentForPackage (packageName );
297283 String className = launchIntent .getComponent ().getClassName ();
298284 try {
299285 return Class .forName (className );
0 commit comments