diff --git a/USAGE.md b/USAGE.md index ac21f01..af953f8 100644 --- a/USAGE.md +++ b/USAGE.md @@ -38,6 +38,10 @@ const options = { }, color: '#ff00ff', linkingURI: 'yourSchemeHere://chat/jane', // See Deep Linking for more info + actions:[ + { title: 'Jane', URI: 'yourSchemeHere://chat/jane' }, + { title: 'John', URI: 'yourSchemeHere://chat/john' } + ] parameters: { delay: 1000, }, @@ -62,6 +66,7 @@ await BackgroundService.stop(); | `taskIcon` | [``](#taskIconOptions) | **Android Required**. Notification icon. | | `color` | `` | Notification color. **Default**: `"#ffffff"`. | | `linkingURI` | `` | Link that will be called when the notification is clicked. Example: `"yourSchemeHere://chat/jane"`. See [Deep Linking](#deep-linking) for more info. **Default**: `undefined`. | +| `actions` | [`[]`](#actionItem) | List of notification action items. | | `progressBar` | [``](#taskProgressBarOptions) | Notification progress bar. | | `parameters` | `` | Parameters to pass to the task. | @@ -77,6 +82,14 @@ Example: ![photo5837026843969041365](https://user-images.githubusercontent.com/44206249/72532521-de49e280-3873-11ea-8bf6-00618bcb82ab.jpg) +#### actionItem +**Android only** +| Property | Type | Description | +| ----------- | ---------- | -------------------------------------------------------------- | +| `title` | `` | **Required**. Action title. | +| `URI` | `` | Link that will be called when the notification is clicked. Example: `"yourSchemeHere://chat/jane"`. See [Deep Linking](#deep-linking) for more info. | +**It defaults to the app's package. It is higly recommended to leave like that.** | + #### taskProgressBarOptions **Android only** | Property | Type | Description | diff --git a/android/src/main/java/com/asterinet/react/bgactions/BackgroundTaskOptions.java b/android/src/main/java/com/asterinet/react/bgactions/BackgroundTaskOptions.java index 9951e02..89682bb 100644 --- a/android/src/main/java/com/asterinet/react/bgactions/BackgroundTaskOptions.java +++ b/android/src/main/java/com/asterinet/react/bgactions/BackgroundTaskOptions.java @@ -2,6 +2,9 @@ import android.graphics.Color; import android.os.Bundle; +import android.os.Parcelable; +import android.util.Log; +import android.widget.Button; import androidx.annotation.ColorInt; import androidx.annotation.IdRes; @@ -10,8 +13,12 @@ import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; +import java.io.Serializable; +import java.util.ArrayList; + public final class BackgroundTaskOptions { private final Bundle extras; @@ -68,6 +75,25 @@ public BackgroundTaskOptions(@NonNull final ReactContext reactContext, @NonNull } catch (Exception e) { extras.putInt("color", Color.parseColor("#ffffff")); } + + // Get actions + try { + final ReadableArray acts = options.getArray("actions"); + Bundle actions = new Bundle(); + if(acts != null) { + for (int i = 0; i < acts.size(); i++) { + ReadableMap map = acts.getMap(i); + Bundle action = new Bundle(); + action.putString("title", map.getString("title")); + action.putString("URI", map.getString("URI")); + actions.putBundle(Integer.toString(i), action); + } + } + extras.putBundle("actions", actions); + } catch (Exception e) { + throw new IllegalArgumentException(); + } + } public Bundle getExtras() { @@ -97,6 +123,11 @@ public String getLinkingURI() { return extras.getString("linkingURI"); } + @Nullable + public Bundle getActions() { + return extras.getBundle("actions"); + } + @Nullable public Bundle getProgressBar() { return extras.getBundle("progressBar"); diff --git a/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java b/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java index 428b4d4..d648dda 100644 --- a/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java +++ b/android/src/main/java/com/asterinet/react/bgactions/RNBackgroundActionsTask.java @@ -31,6 +31,7 @@ public static Notification buildNotification(@NonNull final ReactContext context final int iconInt = bgOptions.getIconInt(); final int color = bgOptions.getColor(); final String linkingURI = bgOptions.getLinkingURI(); + final Bundle actions = bgOptions.getActions(); Intent notificationIntent; if (linkingURI != null) { notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkingURI)); @@ -47,6 +48,24 @@ public static Notification buildNotification(@NonNull final ReactContext context .setPriority(NotificationCompat.PRIORITY_MIN) .setColor(color); + for (String key : actions.keySet()) { + final Bundle action = actions.getBundle(key); + final String title = action.getString("title"); + + if (title == null) break; + + final String actionURI = action.getString("URI"); + Intent actionIntent; + if (actionURI != null) { + actionIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(actionURI)); + } else { + actionIntent = new Intent(context, context.getCurrentActivity().getClass()); + } + final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); + + builder.addAction(iconInt, title, pendingIntent); + } + final Bundle progressBarBundle = bgOptions.getProgressBar(); if (progressBarBundle != null) { final int progressMax = (int) Math.floor(progressBarBundle.getDouble("max")); diff --git a/lib/types/index.d.ts b/lib/types/index.d.ts index 9a9c02a..63f5179 100644 --- a/lib/types/index.d.ts +++ b/lib/types/index.d.ts @@ -10,6 +10,10 @@ export type BackgroundTaskOptions = { }; color?: string | undefined; linkingURI?: string | undefined; + actions?: [{ + title: string; + URI: string; + }] | undefined; progressBar?: { max: number; value: number; @@ -24,6 +28,7 @@ declare const backgroundServer: BackgroundServer; * taskIcon: {name: string, type: string, package?: string}, * color?: string * linkingURI?: string, + * actions?: [{title: string, URI: string}], * progressBar?: {max: number, value: number, indeterminate?: boolean} * }} BackgroundTaskOptions * @extends EventEmitter<'expiration',any> @@ -53,6 +58,7 @@ declare class BackgroundServer extends EventEmitter<"expiration", any> { * taskIcon?: {name: string, type: string, package?: string}, * color?: string, * linkingURI?: string, + * actions?: [{title: string, URI: string}], * progressBar?: {max: number, value: number, indeterminate?: boolean}}} taskData */ updateNotification(taskData: { @@ -65,6 +71,10 @@ declare class BackgroundServer extends EventEmitter<"expiration", any> { }; color?: string; linkingURI?: string; + actions?: [{ + title: string; + URI: string; + }]; progressBar?: { max: number; value: number; @@ -97,6 +107,10 @@ declare class BackgroundServer extends EventEmitter<"expiration", any> { }; color?: string | undefined; linkingURI?: string | undefined; + actions?: [{ + title: string; + URI: string; + }] | undefined; progressBar?: { max: number; value: number; diff --git a/src/index.js b/src/index.js index 3719399..ab38991 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,7 @@ import EventEmitter from 'eventemitter3'; * taskIcon: {name: string, type: string, package?: string}, * color?: string * linkingURI?: string, + * actions?: [{title: string, URI: string}], * progressBar?: {max: number, value: number, indeterminate?: boolean} * }} BackgroundTaskOptions * @extends EventEmitter<'expiration',any> @@ -46,6 +47,7 @@ class BackgroundServer extends EventEmitter { * taskIcon?: {name: string, type: string, package?: string}, * color?: string, * linkingURI?: string, + * actions?: [{title: string, URI: string}], * progressBar?: {max: number, value: number, indeterminate?: boolean}}} taskData */ async updateNotification(taskData) { @@ -117,6 +119,7 @@ class BackgroundServer extends EventEmitter { taskIcon: { ...options.taskIcon }, color: options.color || '#ffffff', linkingURI: options.linkingURI, + actions: options.actions, progressBar: options.progressBar, }; }