Rapsssito / react-native-background-actions

React Native background service library for running background tasks forever in Android & iOS.
MIT License
814 stars 116 forks source link

Working fine on every device apart from samsung devices with android 14. #223

Closed Muhammad-Bilal-Arshad closed 4 months ago

Muhammad-Bilal-Arshad commented 5 months ago

Even in Samsung Android 14 it does get registered as a background service but it doesnot do any thing in the background.

narayanlava commented 4 months ago

<service android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask" android:exported="false" android:foregroundServiceType="location" tools:replace="android:foregroundServiceType"/>

github-actions[bot] commented 4 months ago

:tada: This issue has been resolved in version 4.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

vishalyad16 commented 3 months ago

@Rapsssito It still crashes when the app goes into the background while sharing the screen.

"react-native-background-actions": "^4.0.1",

buildToolsVersion = "34.0.0" minSdkVersion = 23 compileSdkVersion = 34 targetSdkVersion = 34 ndkVersion = "26.2.11394342" kotlinVersion = "1.9.22"

`package com.asterinet.react.bgactions;

import android.annotation.SuppressLint; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle;

import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat;

import com.facebook.react.HeadlessJsTaskService; import com.facebook.react.bridge.Arguments; import com.facebook.react.jstasks.HeadlessJsTaskConfig;

final public class RNBackgroundActionsTask extends HeadlessJsTaskService {

public static final int SERVICE_NOTIFICATION_ID = 92901;
private static final String CHANNEL_ID = "RN_BACKGROUND_ACTIONS_CHANNEL";

@SuppressLint("UnspecifiedImmutableFlag")
@NonNull
public static Notification buildNotification(@NonNull Context context, @NonNull final BackgroundTaskOptions bgOptions) {
    // Get info
    final String taskTitle = bgOptions.getTaskTitle();
    final String taskDesc = bgOptions.getTaskDesc();
    final int iconInt = bgOptions.getIconInt();
    final int color = bgOptions.getColor();
    final String linkingURI = bgOptions.getLinkingURI();
    Intent notificationIntent;
    if (linkingURI != null) {
        notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkingURI));
    } else {
        //as RN works on single activity architecture - we don't need to find current activity on behalf of react context
        notificationIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
    }
    final PendingIntent contentIntent;
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
        contentIntent = PendingIntent.getActivity(context,0, notificationIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    } else {
        contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setContentTitle(taskTitle)
            .setContentText(taskDesc)
            .setSmallIcon(iconInt)
            .setContentIntent(contentIntent)
            .setOngoing(true)
            .setPriority(NotificationCompat.PRIORITY_MIN)
            .setColor(color);

    final Bundle progressBarBundle = bgOptions.getProgressBar();
    if (progressBarBundle != null) {
        final int progressMax = (int) Math.floor(progressBarBundle.getDouble("max"));
        final int progressCurrent = (int) Math.floor(progressBarBundle.getDouble("value"));
        final boolean progressIndeterminate = progressBarBundle.getBoolean("indeterminate");
        builder.setProgress(progressMax, progressCurrent, progressIndeterminate);
    }
    return builder.build();
}

@Override
protected @Nullable
HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    final Bundle extras = intent.getExtras();
    if (extras != null) {
        return new HeadlessJsTaskConfig(extras.getString("taskName"), Arguments.fromBundle(extras), 0, true);
    }
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    final Bundle extras = intent.getExtras();
    if (extras == null) {
        throw new IllegalArgumentException("Extras cannot be null");
    }
    final BackgroundTaskOptions bgOptions = new BackgroundTaskOptions(extras);
    createNotificationChannel(bgOptions.getTaskTitle(), bgOptions.getTaskDesc()); // Necessary creating channel for API 26+
    // Create the notification
    final Notification notification = buildNotification(this, bgOptions);

    startForeground(SERVICE_NOTIFICATION_ID, notification);
    return super.onStartCommand(intent, flags, startId);
}

private void createNotificationChannel(@NonNull final String taskTitle, @NonNull final String taskDesc) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        final int importance = NotificationManager.IMPORTANCE_LOW;
        final NotificationChannel channel = new NotificationChannel(CHANNEL_ID, taskTitle, importance);
        channel.setDescription(taskDesc);
        final NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

} ` @Rapsssito, I need to release the app on the Play Store with target SDK 34. Please mark this as urgent. Thanks

FATAL EXCEPTION: main Process: com.explorastory, PID: 16580 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { (has extras) }} to activity {/io.agora.rtc2.extensions.MediaProjectionMgr$LocalScreenCaptureAssistantActivity}: java.lang.SecurityException: Media projections require a foreground service of type ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION