Skip to content

Commit 0c2635f

Browse files
authored
Merge pull request #95 from Grait-Interaction/android-notification-features
Android notification features and Crash fixes
2 parents 8ad5dfb + 27d0c8f commit 0c2635f

File tree

3 files changed

+81
-31
lines changed

3 files changed

+81
-31
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,23 @@ Returns a promise with the string ID of the upload. Will reject if there is a c
153153
|`headers`|object|Optional||HTTP headers|`{ 'Accept': 'application/json' }`|
154154
|`field`|string|Required if `type: 'multipart'`||The form field name for the file. Only used when `type: 'multipart`|`uploaded-file`|
155155
|`parameters`|object|Optional||Additional form fields to include in the HTTP request. Only used when `type: 'multipart`||
156-
|`notification`|object with single `enabled` field|Optional||Android only. |`{ enabled: false }`|
156+
|`notification`|Notification object (see below)|Optional||Android only. |`{ enabled: true, onProgressTitle: "Uploading...", autoClear: true }`|
157+
158+
### Notification Object
159+
|Name|Type|Required|Description|Example|
160+
|---|---|---|---|---|
161+
|`enabled`|boolean|Optional|Enable or diasable notifications|`{ enabled: true }`|
162+
|`autoClear`|boolean|Optional|Autoclear notification on complete|`{ autoclear: true }`|
163+
|`notificationChannel`|string|Optional|Sets android notificaion channel|`{ notificationChannel: "My-Upload-Service" }`|
164+
|`onProgressTitle`|string|Optional|Sets notification progress title|`{ onProgressTitle: "Uploading" }`|
165+
|`onProgressMessage`|string|Optional|Sets notification progress message|`{ onProgressMessage: "Uploading new video" }`|
166+
|`onCompleteTitle`|string|Optional|Sets notification complete title|`{ onCompleteTitle: "Upload finished" }`|
167+
|`onCompleteMessage`|string|Optional|Sets notification complete message|`{ onCompleteMessage: "Your video has been uploaded" }`|
168+
|`onErrorTitle`|string|Optional|Sets notification error title|`{ onErrorTitle: "Upload error" }`|
169+
|`onErrorMessage`|string|Optional|Sets notification error message|`{ onErrorMessage: "An error occured while uploading a video" }`|
170+
|`onCancelledTitle`|string|Optional|Sets notification cancelled title|`{ onCancelledTitle: "Upload cancelled" }`|
171+
|`onCancelledMessage`|string|Optional|Sets notification cancelled message|`{ onCancelledMessage: "Video upload was cancelled" }`|
172+
157173

158174
### getFileInfo(path)
159175

@@ -289,19 +305,6 @@ Ensure `compileSdkVersion` and `targetSdkVersion` are 25.
289305

290306
Done!
291307

292-
## Known issues
293-
294-
Android APK 27 and above require notifications to provide their own `NotificationChannel`. `react-native-background-upload` does not yet meet this requirement and thus [will cause crashes in Android 8.1 and above](https://github.com/Vydia/react-native-background-upload/issues/59#issuecomment-362476703). This issue can be avoided by electing not to use native notifications.
295-
296-
```js
297-
const options = {
298-
// ...
299-
notification: {
300-
enabled: false,
301-
},
302-
};
303-
```
304-
305308

306309
## Gratitude
307310

android/build.gradle

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ buildscript {
33
jcenter()
44
}
55
dependencies {
6-
classpath 'com.android.tools.build:gradle:2.3.0'
6+
classpath 'com.android.tools.build:gradle:2.3.3'
77
}
88
}
99

1010
apply plugin: 'com.android.library'
1111

12-
def DEFAULT_COMPILE_SDK_VERSION = 25
13-
def DEFAULT_BUILD_TOOLS_VERSION = "25.0.2"
14-
def DEFAULT_TARGET_SDK_VERSION = 25
12+
def DEFAULT_COMPILE_SDK_VERSION = 27
13+
def DEFAULT_BUILD_TOOLS_VERSION = "27.0.3"
14+
def DEFAULT_TARGET_SDK_VERSION = 27
1515

1616
android {
1717
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
@@ -25,7 +25,7 @@ android {
2525
ndk {
2626
abiFilters "armeabi-v7a", "x86"
2727
}
28-
}
28+
}
2929
lintOptions {
3030
abortOnError false
3131
}
@@ -35,14 +35,6 @@ repositories {
3535
mavenCentral()
3636
}
3737
dependencies {
38-
/**
39-
* Using okhttp is better because it fixes a possible outofmemory exception.
40-
* However the okhttp library can have a funky version resolution and cause compilation errors with RN
41-
* There is a PR to fix that
42-
* https://github.com/facebook/react-native/pull/11835
43-
* Until that is merged and RN is upgraded, you need to add this line to your android/app/build.gradle, inside 'android' block:
44-
* configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' }
45-
*/
4638
compile 'com.facebook.react:react-native:+'
47-
compile 'net.gotev:uploadservice-okhttp:3.2.3'
39+
compile 'net.gotev:uploadservice-okhttp:3.4.2'
4840
}

android/src/main/java/com/vydia/UploaderModule.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,21 @@ public void onProgress(Context context, UploadInfo uploadInfo) {
150150
}
151151

152152
@Override
153-
public void onError(Context context, UploadInfo uploadInfo, Exception exception) {
153+
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) {
154154
WritableMap params = Arguments.createMap();
155155
params.putString("id", customUploadId != null ? customUploadId : uploadInfo.getUploadId());
156-
params.putString("error", exception.getMessage());
156+
if (serverResponse != null) {
157+
params.putInt("responseCode", serverResponse.getHttpCode());
158+
params.putString("responseBody", serverResponse.getBodyAsString());
159+
}
160+
161+
// Make sure we do not try to call getMessage() on a null object
162+
if (exception != null){
163+
params.putString("error", exception.getMessage());
164+
} else {
165+
params.putString("error", "Unknown exception");
166+
}
167+
157168
sendEvent("error", params);
158169
}
159170

@@ -200,7 +211,51 @@ public void onCancelled(Context context, UploadInfo uploadInfo) {
200211
.setDelegate(statusDelegate);
201212

202213
if (notification.getBoolean("enabled")) {
203-
request.setNotificationConfig(new UploadNotificationConfig());
214+
215+
UploadNotificationConfig notificationConfig = new UploadNotificationConfig();
216+
217+
if (notification.hasKey("notificationChannel")){
218+
notificationConfig.setNotificationChannelId(notification.getString("notificationChannel"));
219+
}
220+
221+
if (notification.hasKey("autoClear") && notification.getBoolean("autoClear")){
222+
notificationConfig.getCompleted().autoClear = true;
223+
}
224+
225+
if (notification.hasKey("onCompleteTitle")) {
226+
notificationConfig.getCompleted().title = notification.getString("onCompleteTitle");
227+
}
228+
229+
if (notification.hasKey("onCompleteMessage")) {
230+
notificationConfig.getCompleted().message = notification.getString("onCompleteMessage");
231+
}
232+
233+
if (notification.hasKey("onErrorTitle")) {
234+
notificationConfig.getError().title = notification.getString("onErrorTitle");
235+
}
236+
237+
if (notification.hasKey("onErrorMessage")) {
238+
notificationConfig.getError().message = notification.getString("onErrorMessage");
239+
}
240+
241+
if (notification.hasKey("onProgressTitle")) {
242+
notificationConfig.getProgress().title = notification.getString("onProgressTitle");
243+
}
244+
245+
if (notification.hasKey("onProgressMessage")) {
246+
notificationConfig.getProgress().message = notification.getString("onProgressMessage");
247+
}
248+
249+
if (notification.hasKey("onCancelledTitle")) {
250+
notificationConfig.getCancelled().title = notification.getString("onCancelledTitle");
251+
}
252+
253+
if (notification.hasKey("onCancelledMessage")) {
254+
notificationConfig.getCancelled().message = notification.getString("onCancelledMessage");
255+
}
256+
257+
request.setNotificationConfig(notificationConfig);
258+
204259
}
205260

206261
if (options.hasKey("parameters")) {

0 commit comments

Comments
 (0)