Skip to content

Commit

Permalink
RN 0.60 autolink support
Browse files Browse the repository at this point in the history
  • Loading branch information
YuliaGrigorieva committed Jul 16, 2019
1 parent 54298fd commit ac2636c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### 2.0.0
- RN 0.60 autolinking support

### 1.1.0
- Make channelId in NotificationConfig required only for android 8+

Expand Down
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,34 @@ See [the Android official documentation](https://developer.android.com/guide/com

`$ npm install @voximplant/react-native-foreground-service --save`

### Mostly automatic installation (Android only)
### Automatic installation (Android only)

`$ react-native link @voximplant/react-native-foreground-service`
- React Native 0.60+

Next follow steps #4 and #5 from the manual installation section.
CLI autolink feature links the module while building the app.

### Manual installation (Android only)
1. Add the FOREGROUND_SERVICE permission to the application's `AndroidManifest.xml`:
```
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
```
2. Add VIForegroundService as a service to the application's `AndroidManifest.xml`:
```
<service android:name="com.voximplant.foregroundservice.VIForegroundService"> </service>
- React Native <= 0.59
`$ react-native link @voximplant/react-native-foreground-service`
1. Add the FOREGROUND_SERVICE permission to the application's `AndroidManifest.xml`:
```
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
```
2. Add VIForegroundService as a service to the application's `AndroidManifest.xml`:
```
<service android:name="com.voximplant.foregroundservice.VIForegroundService"> </service>
```
### Manual installation (Android only, React Native <= 0.59)
1. Open up `android/app/src/main/java/[...]/MainActivity.java`
- Add `import com.voximplant.foregroundservice.VIForegroundServicePackage;` to the imports at the top of the file
- Add `new VIForegroundServicePackage()` to the list returned by the `getPackages()` method
Expand Down
5 changes: 2 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ def safeExtGet(prop, fallback) {
}

android {
compileSdkVersion 27
compileSdkVersion 28
buildToolsVersion '28.0.3'

defaultConfig {
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
Expand All @@ -26,6 +26,5 @@ repositories {

dependencies {
implementation 'com.facebook.react:react-native:+'
api "com.android.support:appcompat-v7:${safeExtGet('supportLibVersion', '27.1.1')}"
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.facebook.react.bridge.Promise;
Expand Down Expand Up @@ -84,20 +83,43 @@ Notification buildNotification(Context context, Bundle notificationConfig) {
Intent notificationIntent = new Intent(context, mainActivityClass);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

NotificationCompat.Builder notificationBuilder;
Notification.Builder notificationBuilder;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = notificationConfig.getString("channelId");
if (channelId == null) {
Log.e("NotificationHelper", "buildNotification: invalid channelId");
return null;
}
notificationBuilder = new NotificationCompat.Builder(context, channelId);
notificationBuilder = new Notification.Builder(context, channelId);
} else {
notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder = new Notification.Builder(context);
}

int priority = notificationConfig.containsKey("priority") ? notificationConfig.getInt("priority") : NotificationCompat.PRIORITY_HIGH;
int priorityInt = notificationConfig.containsKey("priority") ? notificationConfig.getInt("priority"): Notification.PRIORITY_HIGH;

int priority;
switch (priorityInt) {
case 0:
priority = Notification.PRIORITY_DEFAULT;
break;
case -1:
priority = Notification.PRIORITY_LOW;
break;
case -2:
priority = Notification.PRIORITY_MIN;
break;
case 1:
priority = Notification.PRIORITY_HIGH;
break;
case 2:
priority = Notification.PRIORITY_MAX;
break;
default:
priority = Notification.PRIORITY_HIGH;
break;

}

notificationBuilder.setContentTitle(notificationConfig.getString("title"))
.setContentText(notificationConfig.getString("text"))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@voximplant/react-native-foreground-service",
"version": "1.1.0",
"version": "2.0.0",
"description": "React native module to start foreground service on android",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 10 additions & 0 deletions react-native.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
dependency: {
platforms: {
android: {
packageImportPath: 'import com.voximplant.foregroundservice.VIForegroundServicePackage;',
packageInstance: 'new VIForegroundServicePackage()',
},
},
},
};

0 comments on commit ac2636c

Please sign in to comment.