Skip to content

Commit

Permalink
fix(android): setup fcm sdk
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Jan 10, 2019
1 parent c8f3109 commit 256396e
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 34 deletions.
1 change: 1 addition & 0 deletions client/react-native/android/app/build.gradle
Expand Up @@ -185,6 +185,7 @@ dependencies {
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation 'com.facebook.react:react-native:+' // From node_modules
implementation 'com.google.firebase:firebase-messaging:17.3.4'
implementation 'com.google.code.gson:gson:2.8.2'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':ble')
implementation project(':core')
Expand Down
Expand Up @@ -8,17 +8,18 @@
import com.facebook.react.bridge.ReactMethod;
import chat.berty.ble.Manager;
import core.Core;
import core.MobileNotification;
import core.NativeNotification;

public class CoreModule extends ReactContextBaseJavaModule {
private Logger logger = new Logger("chat.berty.io");
private Notification notification;
private MobileNotification notificationDriver = Core.getNotificationDriver();
private String filesDir = "";
private ReactApplicationContext reactContext;

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

Object o = new Manager.ActivityGetter() {
Expand All @@ -28,6 +29,8 @@ public Activity getCurrentActivity() {
};

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

this.notificationDriver.setNativeNotification(new Notification(reactContext, this.notificationDriver));
}

public String getName() {
Expand Down Expand Up @@ -63,8 +66,7 @@ public void start(String nickname, Promise promise) {
core.MobileOptions coreOptions = new core.MobileOptions()
.withNickname(nickname)
.withDatastorePath(this.filesDir)
.withLoggerDriver(this.logger)
.withNotificationDriver(this.notification);
.withLoggerDriver(this.logger);

Core.start(coreOptions);
promise.resolve(null);
Expand Down
Expand Up @@ -13,23 +13,34 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactMethod;
import com.google.firebase.messaging.RemoteMessage;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.gson.Gson;

import java.util.Map;

import chat.berty.ble.Manager;
import chat.berty.main.R;
import core.Core;
import core.MobileNotification;
import core.NativeNotification;

public class Notification implements core.NativeNotification {
public class Notification extends FirebaseMessagingService implements NativeNotification {
private ReactApplicationContext reactContext;

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";

public Notification(final ReactApplicationContext reactContext) {
private static MobileNotification handler;

public Notification(final ReactApplicationContext reactContext, MobileNotification handler) {
this.reactContext = reactContext;
this.createNotificationChannel();
this.handler = handler;
}

@Override
public void displayNativeNotification(String title, String body, String icon, String sound) throws Exception {
public void displayNotification(String title, String body, String icon, String sound) throws Exception {
NotificationManager notificationManager = (NotificationManager)this.reactContext.getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(this.reactContext, Notification.class);
Expand Down Expand Up @@ -61,4 +72,27 @@ private void createNotificationChannel() {
notificationManager.createNotificationChannel(channel);
}
}


/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> map = remoteMessage.getData();
String data = new Gson().toJson(map);
this.handler.receiveNotification(data);
}

/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onNewToken(String token) {
this.handler.receivePushID(token, "fcm");
}
}
17 changes: 5 additions & 12 deletions client/react-native/gomobile/core/core.go
Expand Up @@ -15,17 +15,11 @@ import (
"go.uber.org/zap"
)

type MobileOptions struct {
logger NativeLogger
notification NativeNotification
datastorePath string
nickname string
}

var (
accountName = ""
appConfig *account.StateDB
rootContext = context.Background()
accountName = ""
appConfig *account.StateDB
rootContext = context.Background()
NotificationDriver = &MobileNotification{}
)

func logger() *zap.Logger {
Expand Down Expand Up @@ -217,13 +211,12 @@ func daemon(cfg *MobileOptions) error {
return err
}

notificationDriver := &nativeNotificationModule{cfg.notification}
accountOptions := account.Options{
account.WithJaegerAddrName("jaeger.berty.io:6831", cfg.nickname+":mobile"),
account.WithRing(logmanager.G().Ring()),
account.WithName(cfg.nickname),
account.WithPassphrase("secure"),
account.WithNotificationDriver(notificationDriver),
account.WithNotificationDriver(NotificationDriver),
account.WithDatabase(&account.DatabaseOptions{
Path: cfg.datastorePath,
Drop: false,
Expand Down
2 changes: 1 addition & 1 deletion client/react-native/gomobile/core/driver.go
Expand Up @@ -6,5 +6,5 @@ type NativeLogger interface {
}

type NativeNotification interface {
DisplayNativeNotification(title, body, icon, sound string) error
DisplayNotification(title, body, icon, sound string) error
}
21 changes: 16 additions & 5 deletions client/react-native/gomobile/core/notification.go
@@ -1,16 +1,27 @@
package core

import (
"fmt"

"berty.tech/core/pkg/notification"
)

// nativeNotificationModule is a notification.Driver
var _ notification.Driver = (*nativeNotificationModule)(nil)
// MobileNotification is a notification.Driver
var _ notification.Driver = (*MobileNotification)(nil)

type nativeNotificationModule struct {
type MobileNotification struct {
NativeNotification
}

func (n nativeNotificationModule) DisplayNotification(p notification.Payload) error {
return n.NativeNotification.DisplayNativeNotification(p.Title, p.Body, p.Icon, p.Sound)
func (n *MobileNotification) DisplayNotification(p notification.Payload) error {
return n.NativeNotification.DisplayNotification(p.Title, p.Body, p.Icon, p.Sound)
}

func (n *MobileNotification) ReceiveNotification(data string) {
logger().Debug(fmt.Sprintf("Receive notification: %+v", data))
}

// TODO: change pushIDType to enum and see if it build well with gomobile
func (n *MobileNotification) ReceivePushID(pushID, pushIDType string) {
logger().Debug(fmt.Sprintf("Receive push id from %v: %+v", pushIDType, pushID))
}
7 changes: 4 additions & 3 deletions client/react-native/gomobile/core/options.go
@@ -1,8 +1,9 @@
package core

func (cfg *MobileOptions) WithNotificationDriver(driver NativeNotification) *MobileOptions {
cfg.notification = driver
return cfg
type MobileOptions struct {
logger NativeLogger
datastorePath string
nickname string
}

func (cfg *MobileOptions) WithDatastorePath(path string) *MobileOptions {
Expand Down
4 changes: 0 additions & 4 deletions client/react-native/gomobile/go.mod
Expand Up @@ -2,14 +2,10 @@ module berty.tech/client/react-native/gomobile

require (
berty.tech/core v0.0.0
github.com/godbus/dbus v4.1.0+incompatible // indirect
github.com/gopherjs/gopherwasm v1.0.1 // indirect
github.com/libp2p/go-buffer-pool v0.1.2-0.20181009094743-058210c5a0d0 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/pkg/errors v0.8.0
go.uber.org/zap v1.9.1
golang.org/x/mobile v0.0.0-20181026062114-a27dd33d354d // indirect
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2 // indirect
)

replace berty.tech/core v0.0.0 => ../../../core
5 changes: 5 additions & 0 deletions client/react-native/gomobile/go.sum
Expand Up @@ -46,6 +46,7 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/gen2brain/beeep v0.0.0-20180718162406-4e430518395f h1:eyHMPp7tXlBMF8PZHdsL89G0ehuRNflu7zKUeoQjcJ0=
github.com/gen2brain/beeep v0.0.0-20180718162406-4e430518395f/go.mod h1:GprdPCZglWh5OMcIDpeKBxuUJI+fEDOTVUfxZeda4zo=
github.com/getlantern/context v0.0.0-20181106182922-539649cc3118/go.mod h1:L+mq6/vvYHKjCX2oez0CgEAJmbq1fbb/oNJIWQkBybY=
github.com/getlantern/deepcopy v0.0.0-20160317154340-7f45deb8130a/go.mod h1:AEugkNu3BjBxyz958nJ5holD9PRjta6iprcoUauDbU4=
github.com/getlantern/ema v0.0.0-20180718025023-42474605965c/go.mod h1:tzRwT19aDrWSr6yRDs8iOvaXXCau96EgWsgGT9wIpoQ=
github.com/getlantern/errors v0.0.0-20180829142810-e24b7f4ff7c7/go.mod h1:l+xpFBrCtDLpK9qNjxs+cHU6+BAdlBaxHqikB6Lku3A=
github.com/getlantern/golog v0.0.0-20170508214112-cca714f7feb5/go.mod h1:Vwx1Cg64gCdIalad44uvQsKZw6LsVczIKZrUBStEjVw=
Expand Down Expand Up @@ -239,6 +240,8 @@ github.com/gorilla/sessions v1.1.2/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE
github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gosimple/slug v1.4.2 h1:jDmprx3q/9Lfk4FkGZtvzDQ9Cj9eAmsjzeQGp24PeiQ=
github.com/gosimple/slug v1.4.2/go.mod h1:ER78kgg1Mv0NQGlXiDe57DpCyfbNywXXZ9mIorhxAf0=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:BWIsLfhgKhV5g/oF34aRjniBHLTZe5DNekSjbAjIS6c=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20181112102510-3304cc886352 h1:1GXyC+LmruB9uk340NwSS6UPwWNXM3xe6pbGpsXqAKc=
Expand Down Expand Up @@ -465,6 +468,8 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q=
github.com/rogpeppe/go-internal v1.0.0 h1:o4VLZ5jqHE+HahLT6drNtSGTrrUA3wPBmtpgqtdbClo=
github.com/rogpeppe/go-internal v1.0.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
Expand Down
18 changes: 17 additions & 1 deletion core/pkg/notification/notification.go
Expand Up @@ -9,6 +9,8 @@ import (

type Driver interface {
DisplayNotification(Payload) error
ReceiveNotification(string)
ReceivePushID(pushID string, pushIDType string)
}

type Payload struct {
Expand All @@ -30,7 +32,7 @@ func NewNoopNotification() Driver {

func (n *NoopNotification) DisplayNotification(p Payload) error {
// for debug puprpose
logger().Debug("NoopNotification",
logger().Debug("DisplayNotification",
zap.String("title", p.Title),
zap.String("body", p.Body),
zap.String("Icon", p.Icon),
Expand All @@ -41,6 +43,16 @@ func (n *NoopNotification) DisplayNotification(p Payload) error {
return nil
}

func (n *NoopNotification) ReceiveNotification(data string) {
logger().Debug("ReceiveNotification",
zap.String("data", data),
)
}

func (n *NoopNotification) ReceivePushID(pushID, pushIDType string) {
logger().Debug("ReceivePushID")
}

// NoopNotification is a Driver
var _ Driver = (*DesktopNotification)(nil)

Expand All @@ -53,3 +65,7 @@ type DesktopNotification struct{}
func (n *DesktopNotification) DisplayNotification(p Payload) error {
return beeep.Notify(p.Title, p.Body, p.Icon)
}

func (n *DesktopNotification) ReceiveNotification(data string) {}

func (n *DesktopNotification) ReceivePushID(pushID, pushIDType string) {}

0 comments on commit 256396e

Please sign in to comment.