Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Android): Add action buttons for notifications #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -62,6 +66,7 @@ await BackgroundService.stop();
| `taskIcon` | [`<taskIconOptions>`](#taskIconOptions) | **Android Required**. Notification icon. |
| `color` | `<string>` | Notification color. **Default**: `"#ffffff"`. |
| `linkingURI` | `<string>` | 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>]`](#actionItem) | List of notification action items. |
| `progressBar` | [`<taskProgressBarOptions>`](#taskProgressBarOptions) | Notification progress bar. |
| `parameters` | `<any>` | Parameters to pass to the task. |

Expand All @@ -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` | `<string>` | **Required**. Action title. |
| `URI` | `<string>` | 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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Comment on lines +81 to +83
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to work, if I start the background task without an actions key in the options, the whole background task throws an Error.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG  Trying to start background service
LOG  Error [Error: null]

Here is an example of the log

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() {
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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"));
Expand Down
14 changes: 14 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export type BackgroundTaskOptions = {
};
color?: string | undefined;
linkingURI?: string | undefined;
actions?: [{
title: string;
URI: string;
}] | undefined;
progressBar?: {
max: number;
value: number;
Expand All @@ -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>
Expand Down Expand Up @@ -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: {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -117,6 +119,7 @@ class BackgroundServer extends EventEmitter {
taskIcon: { ...options.taskIcon },
color: options.color || '#ffffff',
linkingURI: options.linkingURI,
actions: options.actions,
progressBar: options.progressBar,
};
}
Expand Down