Skip to content

Commit ae748ce

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

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import android.graphics.Color;
44
import android.os.Bundle;
5+
import android.os.Parcelable;
6+
import android.util.Log;
7+
import android.widget.Button;
58

69
import androidx.annotation.ColorInt;
710
import androidx.annotation.IdRes;
@@ -10,8 +13,12 @@
1013

1114
import com.facebook.react.bridge.Arguments;
1215
import com.facebook.react.bridge.ReactContext;
16+
import com.facebook.react.bridge.ReadableArray;
1317
import com.facebook.react.bridge.ReadableMap;
1418

19+
import java.io.Serializable;
20+
import java.util.ArrayList;
21+
1522
public final class BackgroundTaskOptions {
1623
private final Bundle extras;
1724

@@ -68,6 +75,25 @@ public BackgroundTaskOptions(@NonNull final ReactContext reactContext, @NonNull
6875
} catch (Exception e) {
6976
extras.putInt("color", Color.parseColor("#ffffff"));
7077
}
78+
79+
// Get actions
80+
try {
81+
final ReadableArray acts = options.getArray("actions");
82+
Bundle actions = new Bundle();
83+
if(acts != null) {
84+
for (int i = 0; i < acts.size(); i++) {
85+
ReadableMap map = acts.getMap(i);
86+
Bundle action = new Bundle();
87+
action.putString("title", map.getString("title"));
88+
action.putString("URI", map.getString("URI"));
89+
actions.putBundle(Integer.toString(i), action);
90+
}
91+
}
92+
extras.putBundle("actions", actions);
93+
} catch (Exception e) {
94+
throw new IllegalArgumentException();
95+
}
96+
7197
}
7298

7399
public Bundle getExtras() {
@@ -97,6 +123,11 @@ public String getLinkingURI() {
97123
return extras.getString("linkingURI");
98124
}
99125

126+
@Nullable
127+
public Bundle getActions() {
128+
return extras.getBundle("actions");
129+
}
130+
100131
@Nullable
101132
public Bundle getProgressBar() {
102133
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)