Skip to content

Commit be96feb

Browse files
committed
feat(Android): Add action buttons for notifications
1 parent eb92dca commit be96feb

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

USAGE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const options = {
3838
},
3939
color: '#ff00ff',
4040
linkingURI: 'yourSchemeHere://chat/jane', // See Deep Linking for more info
41+
actions:[
42+
{ title: 'Jane', URI: 'yourSchemeHere://chat/jane' },
43+
{ title: 'John', URI: 'yourSchemeHere://chat/john' }
44+
]
4145
parameters: {
4246
delay: 1000,
4347
},
@@ -62,6 +66,7 @@ await BackgroundService.stop();
6266
| `taskIcon` | [`<taskIconOptions>`](#taskIconOptions) | **Android Required**. Notification icon. |
6367
| `color` | `<string>` | Notification color. **Default**: `"#ffffff"`. |
6468
| `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`. |
69+
| `actions` | [`[<actionItem>]`](#actionItem) | List of notification action items. |
6570
| `progressBar` | [`<taskProgressBarOptions>`](#taskProgressBarOptions) | Notification progress bar. |
6671
| `parameters` | `<any>` | Parameters to pass to the task. |
6772

@@ -77,6 +82,14 @@ Example:
7782

7883
![photo5837026843969041365](https://user-images.githubusercontent.com/44206249/72532521-de49e280-3873-11ea-8bf6-00618bcb82ab.jpg)
7984

85+
#### actionItem
86+
**Android only**
87+
| Property | Type | Description |
88+
| ----------- | ---------- | -------------------------------------------------------------- |
89+
| `title` | `<string>` | **Required**. Action title. |
90+
| `URI` | `<string>` | Link that will be called when the notification is clicked. Example: `"yourSchemeHere://chat/jane"`. See [Deep Linking](#deep-linking) for more info. |
91+
**It defaults to the app's package. It is higly recommended to leave like that.** |
92+
8093
#### taskProgressBarOptions
8194
**Android only**
8295
| Property | Type | Description |

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.facebook.react.bridge.Arguments;
1212
import com.facebook.react.bridge.ReactContext;
13+
import com.facebook.react.bridge.ReadableArray;
1314
import com.facebook.react.bridge.ReadableMap;
1415

1516
public final class BackgroundTaskOptions {
@@ -68,6 +69,23 @@ public BackgroundTaskOptions(@NonNull final ReactContext reactContext, @NonNull
6869
} catch (Exception e) {
6970
extras.putInt("color", Color.parseColor("#ffffff"));
7071
}
72+
73+
// Get actions
74+
try {
75+
final ReadableArray acts = options.getArray("actions");
76+
Bundle actions = new Bundle();
77+
for (int i = 0; i < acts.size(); i++) {
78+
ReadableMap map = acts.getMap(i);
79+
Bundle action = new Bundle();
80+
action.putString("title", map.getString("title"));
81+
action.putString("URI", map.getString("URI"));
82+
actions.putBundle(Integer.toString(i), action);
83+
}
84+
extras.putBundle("actions", actions);
85+
} catch (Exception e) {
86+
throw new IllegalArgumentException();
87+
}
88+
7189
}
7290

7391
public Bundle getExtras() {
@@ -97,6 +115,11 @@ public String getLinkingURI() {
97115
return extras.getString("linkingURI");
98116
}
99117

118+
@Nullable
119+
public Bundle getActions() {
120+
return extras.getBundle("actions");
121+
}
122+
100123
@Nullable
101124
public Bundle getProgressBar() {
102125
return extras.getBundle("progressBar");

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static Notification buildNotification(@NonNull final ReactContext context
3131
final int iconInt = bgOptions.getIconInt();
3232
final int color = bgOptions.getColor();
3333
final String linkingURI = bgOptions.getLinkingURI();
34+
final Bundle actions = bgOptions.getActions();
3435
Intent notificationIntent;
3536
if (linkingURI != null) {
3637
notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkingURI));
@@ -47,6 +48,24 @@ public static Notification buildNotification(@NonNull final ReactContext context
4748
.setPriority(NotificationCompat.PRIORITY_MIN)
4849
.setColor(color);
4950

51+
for (String key : actions.keySet()) {
52+
final Bundle action = actions.getBundle(key);
53+
final String title = action.getString("title");
54+
55+
if (title == null) break;
56+
57+
final String actionURI = action.getString("URI");
58+
Intent actionIntent;
59+
if (actionURI != null) {
60+
actionIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(actionURI));
61+
} else {
62+
actionIntent = new Intent(context, context.getCurrentActivity().getClass());
63+
}
64+
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
65+
66+
builder.addAction(iconInt, title, pendingIntent);
67+
}
68+
5069
final Bundle progressBarBundle = bgOptions.getProgressBar();
5170
if (progressBarBundle != null) {
5271
final int progressMax = (int) Math.floor(progressBarBundle.getDouble("max"));

lib/types/index.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export type BackgroundTaskOptions = {
1010
};
1111
color?: string | undefined;
1212
linkingURI?: string | undefined;
13+
actions?: [{
14+
title: string;
15+
URI: string;
16+
}] | undefined;
1317
progressBar?: {
1418
max: number;
1519
value: number;
@@ -24,6 +28,7 @@ declare const backgroundServer: BackgroundServer;
2428
* taskIcon: {name: string, type: string, package?: string},
2529
* color?: string
2630
* linkingURI?: string,
31+
* actions?: [{title: string, URI: string}],
2732
* progressBar?: {max: number, value: number, indeterminate?: boolean}
2833
* }} BackgroundTaskOptions
2934
* @extends EventEmitter<'expiration',any>
@@ -53,6 +58,7 @@ declare class BackgroundServer extends EventEmitter<"expiration", any> {
5358
* taskIcon?: {name: string, type: string, package?: string},
5459
* color?: string,
5560
* linkingURI?: string,
61+
* actions?: [{title: string, URI: string}],
5662
* progressBar?: {max: number, value: number, indeterminate?: boolean}}} taskData
5763
*/
5864
updateNotification(taskData: {
@@ -65,6 +71,10 @@ declare class BackgroundServer extends EventEmitter<"expiration", any> {
6571
};
6672
color?: string;
6773
linkingURI?: string;
74+
actions?: [{
75+
title: string;
76+
URI: string;
77+
}];
6878
progressBar?: {
6979
max: number;
7080
value: number;
@@ -97,6 +107,10 @@ declare class BackgroundServer extends EventEmitter<"expiration", any> {
97107
};
98108
color?: string | undefined;
99109
linkingURI?: string | undefined;
110+
actions?: [{
111+
title: string;
112+
URI: string;
113+
}] | undefined;
100114
progressBar?: {
101115
max: number;
102116
value: number;

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import EventEmitter from 'eventemitter3';
99
* taskIcon: {name: string, type: string, package?: string},
1010
* color?: string
1111
* linkingURI?: string,
12+
* actions?: [{title: string, URI: string}],
1213
* progressBar?: {max: number, value: number, indeterminate?: boolean}
1314
* }} BackgroundTaskOptions
1415
* @extends EventEmitter<'expiration',any>
@@ -46,6 +47,7 @@ class BackgroundServer extends EventEmitter {
4647
* taskIcon?: {name: string, type: string, package?: string},
4748
* color?: string,
4849
* linkingURI?: string,
50+
* actions?: [{title: string, URI: string}],
4951
* progressBar?: {max: number, value: number, indeterminate?: boolean}}} taskData
5052
*/
5153
async updateNotification(taskData) {
@@ -117,6 +119,7 @@ class BackgroundServer extends EventEmitter {
117119
taskIcon: { ...options.taskIcon },
118120
color: options.color || '#ffffff',
119121
linkingURI: options.linkingURI,
122+
actions: options.actions,
120123
progressBar: options.progressBar,
121124
};
122125
}

0 commit comments

Comments
 (0)