Skip to content

Commit 2efefaa

Browse files
authored
fix: Fix Rapsssito#61 when React context was null
1 parent eb92dca commit 2efefaa

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.NotificationChannel;
55
import android.app.NotificationManager;
66
import android.app.PendingIntent;
7+
import android.content.Context;
78
import android.content.Intent;
89
import android.net.Uri;
910
import android.os.Build;
@@ -24,7 +25,7 @@ final public class RNBackgroundActionsTask extends HeadlessJsTaskService {
2425
private static final String CHANNEL_ID = "RN_BACKGROUND_ACTIONS_CHANNEL";
2526

2627
@NonNull
27-
public static Notification buildNotification(@NonNull final ReactContext context, @NonNull final BackgroundTaskOptions bgOptions) {
28+
public static Notification buildNotification(@NonNull Context context, @NonNull final BackgroundTaskOptions bgOptions) {
2829
// Get info
2930
final String taskTitle = bgOptions.getTaskTitle();
3031
final String taskDesc = bgOptions.getTaskDesc();
@@ -35,7 +36,8 @@ public static Notification buildNotification(@NonNull final ReactContext context
3536
if (linkingURI != null) {
3637
notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkingURI));
3738
} else {
38-
notificationIntent = new Intent(context, context.getCurrentActivity().getClass());
39+
//as RN works on single activity architecture - we don't need to find current activity on behalf of react context
40+
notificationIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
3941
}
4042
final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4143
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
@@ -54,7 +56,6 @@ public static Notification buildNotification(@NonNull final ReactContext context
5456
final boolean progressIndeterminate = progressBarBundle.getBoolean("indeterminate");
5557
builder.setProgress(progressMax, progressCurrent, progressIndeterminate);
5658
}
57-
5859
return builder.build();
5960
}
6061

@@ -77,7 +78,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
7778
final BackgroundTaskOptions bgOptions = new BackgroundTaskOptions(extras);
7879
createNotificationChannel(bgOptions.getTaskTitle(), bgOptions.getTaskDesc()); // Necessary creating channel for API 26+
7980
// Create the notification
80-
final Notification notification = buildNotification(getReactNativeHost().getReactInstanceManager().getCurrentReactContext(), bgOptions);
81+
final Notification notification = buildNotification(this, bgOptions);
8182
startForeground(SERVICE_NOTIFICATION_ID, notification);
8283
return super.onStartCommand(intent, flags, startId);
8384
}

0 commit comments

Comments
 (0)