react-native-android-notification-upload

## Getting started

Usage no npm install needed!

<script type="module">
  import reactNativeAndroidNotificationUpload from 'https://cdn.skypack.dev/react-native-android-notification-upload';
</script>

README

react-native-android-notification-upload

Getting started

$ npm install react-native-android-notification-upload --save

Mostly automatic installation

$ react-native link react-native-android-notification-upload

NOTE

Currently this package only works in Android.

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-android-notification-upload and add RNAndroidNotificationUpload.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNAndroidNotificationUpload.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  • Add import com.reactlibrary.RNAndroidNotificationUploadPackage; to the imports at the top of the file
  • Add new RNAndroidNotificationUploadPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:

    include ':react-native-android-notification-upload'
    project(':react-native-android-notification-upload').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-android-notification-upload/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:

      compile project(':react-native-android-notification-upload')
    
  3. we are using okhttp to upload files so please add these dependency to your android/app/build.gradle react native project:

    implementation "com.squareup.okhttp3:okhttp:3.0.1"
    implementation "com.squareup.okhttp3:okhttp-urlconnection:3.0.1"
    implementation 'com.google.code.gson:gson:2.8.5'
    
  4. put service and receiver tag in android/app/src/main/AndroidManifest.xml:

    1. add these permission inside the menifest tag:
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    
    1. Add thes inside the application tag
     <!--This Receiver Action CONNECTIVITY_CHANGE Work in API Level 24or Lower-->
        <receiver android:name="com.reactlibrary.broadcast.UploadBroadCast">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>
        </receiver>
    
        <!--This Service Work in API Level 20 or Lower-->
        <service android:name="com.reactlibrary.services.UploadService"
            android:process=":UploadService"
            android:enabled="true"/>
    
        <!--This Service Work in API Level 21 or Higher-->
        <service android:name="com.reactlibrary.services.UploadJobService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:enabled="true"/>
    
  5. for API 26 we need Notification Channel So add this code in and receiver tag in android/app/src/main/java/com/projectName/MainApplication.java: a) import these Classes at the Top

    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.os.Build;
    

    b) declare notification Channel String inside MainApplication.java(Make sure is will be same as the string you will use in javascript code).

    public static final String CHANNEL_ID = "upload_notification";
    

    c) create function at the bottom of MainApplication.java class.

        private void createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                CharSequence name = "Upload Notification";
                String description = "Uploading All Files to Server";
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
    
                NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
                notificationChannel.setDescription(description);
                notificationChannel.setSound(null, null);
    
                NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                notificationManager.createNotificationChannel(notificationChannel);
            }
        }
    

    d) call function from onCreate() function inside MyApplication.java

    @Override
    public void onCreate() {
        super.onCreate();
        ........
        ........
        this.createNotificationChannel();
    }
    

Windows

Read it! :D

  1. In Visual Studio add the RNAndroidNotificationUpload.sln in node_modules/react-native-android-notification-upload/windows/RNAndroidNotificationUpload.sln folder to their solution, reference from their app.
  2. Open up your MainPage.cs app
  • Add using Android.Notification.Upload.RNAndroidNotificationUpload; to the usings at the top of the file
  • Add new RNAndroidNotificationUploadPackage() to the List<IReactPackage> returned by the Packages method

Usage

import RNAndroidNotificationUpload from 'react-native-android-notification-upload';


// TODO: What to do with the module?
//NOTE:- bcoz you are accessing media files path from Gallery so you need to ask for run time Permission throught React Native code.
    const headers = new Array();
    const datas = new Array();                          
    for(let i=0; i < this.state.filePathArray.length; i++){
      let head = {'key' : 'test1234',
                   'orderid' : '1234567',
                   'filename' : this.state.fileNameArray[i] };
      headers[i] = head;
      datas[i] = {
          'image': this.state.filePathArray[i],// make sure key always be 'image'  
          'type' : 'image/jpeg' //Make sure key always be 'type'  };           
    }
    const filesData = {
      'url': 'http://www.example.com/api',
      'headers': headers,
      'datas': datas,
      };
      //"upload_notification" is a  channel id which you used in    MyApplication.java. Make sure both are same.
    RNAndroidNotificationUpload.startService('upload_notification',filesData);