Skip to content

Commit

Permalink
feat(android): handle android deeplink notif
Browse files Browse the repository at this point in the history
Signed-off-by: Sacha Froment <sfroment42@gmail.com>
  • Loading branch information
sfroment committed Jan 16, 2019
1 parent 1bf10f9 commit c0abe09
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 187 deletions.
19 changes: 7 additions & 12 deletions client/react-native/android/app/src/main/AndroidManifest.xml
Expand Up @@ -17,25 +17,20 @@
android:allowBackup="false"
android:theme="@style/AppTheme">

<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
</activity>

<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:launchMode="singleTop"
android:theme="@style/SplashTheme"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down
Expand Up @@ -17,24 +17,22 @@ public class CoreModule extends ReactContextBaseJavaModule {
private ReactApplicationContext reactContext;
private MobileNotification notificationDriver = Core.getNotificationDriver();

public CoreModule(final ReactApplicationContext reactContext) {
public CoreModule(ReactApplicationContext reactContext) {
super(reactContext);
this.filesDir = reactContext.getFilesDir().getAbsolutePath();
this.reactContext = reactContext;

Object o = new Manager.ActivityGetter() {
public Activity getCurrentActivity() {
return reactContext.getCurrentActivity();
}
};
Object o = actGetter(reactContext);

Manager.getInstance().setmReactContext(o, reactContext);

this.notificationDriver.setNative(new Notification(reactContext));
}

public final ReactApplicationContext getContext() {
return getReactApplicationContext();
private Object actGetter(final ReactApplicationContext reactContext){
return new Manager.ActivityGetter() {
public Activity getCurrentActivity() {
return reactContext.getCurrentActivity();
}
};
}

public String getName() {
Expand Down
Expand Up @@ -6,14 +6,19 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CorePackage implements ReactPackage {

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Collections.<NativeModule>singletonList(new CoreModule(reactContext));
List<NativeModule> modules = new ArrayList<>();
modules.add(new CoreModule(reactContext));
modules.add(new NotificationModule(reactContext));

return modules;
}

// Deprecated in RN 0.47
Expand Down
@@ -0,0 +1,112 @@
package chat.berty.core;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;

import com.facebook.react.bridge.ReactApplicationContext;

import java.lang.ref.WeakReference;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import chat.berty.main.MainActivity;
import chat.berty.main.R;

public class DisplayNotification extends AsyncTask {
private static final String TAG = "DisplayNotification";
private static final String CHANNEL_ID = "berty-chat-android-notification-id";
private static final String CHANNEL_NAME = "berty.chat.android.core.notification.name";
private static final String CHANNEL_DESCRIPTION = "berty.chat.android.core.notification.description";

private final WeakReference<Context> contextWeakReference;
private final WeakReference<ReactApplicationContext> reactContextWeakReference;

private final android.app.NotificationManager notificationManager;

private final String title;

/**
* <p>Runs on the UI thread after {@link #doInBackground}. The
* specified result is the value returned by {@link #doInBackground}.</p>
*
* <p>This method won't be invoked if the task was cancelled.</p>
*
* @param o The result of the operation computed by {@link #doInBackground}.
* @see #onPreExecute
* @see #doInBackground
* @see #onCancelled(Object)
*/
@Override
protected void onPostExecute(Object o) {
contextWeakReference.clear();
reactContextWeakReference.clear();
super.onPostExecute(o);
}

private final String body;
private final String icon;
private final String url;
private final String sound;

DisplayNotification(Context context, ReactApplicationContext reactContext, android.app.NotificationManager notificationManager,
String title, String body, String icon, String sound, String url) {
this.contextWeakReference = new WeakReference<>(context);
this.reactContextWeakReference = new WeakReference<>(reactContext);
this.notificationManager = notificationManager;
this.title = title;
this.body = body;
this.icon = icon;
this.sound = sound;
this.url = url;
}


/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
* <p>
* This method can call {@link #publishProgress} to publish updates
* on the UI thread.
*
* @param objects The parameters of the task.
* @return A result, defined by the subclass of this task.
* @see #onPreExecute()
* @see #onPostExecute
* @see #publishProgress
*/
@Override
protected Object doInBackground(Object[] objects) {
Context context = contextWeakReference.get();
if (context == null) return null;


int m = new Random().nextInt(6) + 5;
String notificationID = Integer.toString(m);
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("url", url);

PendingIntent pendingIntent = PendingIntent.getActivity(
context,
notificationID.hashCode(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setAutoCancel(true)
.setVibrate(new long[]{0, 1000})
.setContentIntent(pendingIntent);

notificationManager.notify(m, mBuilder.build());
return null;
}
}

This file was deleted.

0 comments on commit c0abe09

Please sign in to comment.