Skip to content

Commit

Permalink
fix: fix merge conflict
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 c0abe09 commit cf34d46
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 30 deletions.
@@ -1,10 +1,14 @@
package chat.berty.core;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.facebook.react.ReactApplication;
import com.facebook.react.bridge.Arguments;
Expand All @@ -14,6 +18,7 @@
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.google.gson.Gson;
Expand All @@ -22,9 +27,13 @@

import core.Core;
import core.MobileNotification;
import core.NativeNotification;
import core.NativeNotificationDriver;

public class NotificationManager extends FirebaseMessagingService implements ActivityEventListener, NativeNotificationDriver {
private Logger logger = new Logger("chat.berty.io");

public static int PERMISSION_CODE = 200;

public class NotificationManager extends FirebaseMessagingService implements ActivityEventListener, NativeNotification {
public String TAG = "NotificationManager";

private ReactApplicationContext reactContext;
Expand All @@ -42,7 +51,7 @@ public class NotificationManager extends FirebaseMessagingService implements Act
this.reactContext = reactContext;
this.context = reactContext.getApplicationContext();
this.notificationManager = (android.app.NotificationManager) this.context.getSystemService(Context.NOTIFICATION_SERVICE);
this.notificationDriver.setNativeNotification(this);
this.notificationDriver.setNative(this);

}

Expand All @@ -59,7 +68,7 @@ public void displayNotification(String title, String body, String icon, String s
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> map = remoteMessage.getData();
String data = new Gson().toJson(map);
this.notificationDriver.receiveNotification(data);
this.notificationDriver.receive(data);
}

/**
Expand All @@ -69,7 +78,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
*/
@Override
public void onNewToken(String token) {
this.notificationDriver.receivePushID(token, "fcm");
this.notificationDriver.receiveFCMToken(token.getBytes());
}

static WritableMap toNotificationOpenMap(Intent intent) {
Expand All @@ -89,6 +98,38 @@ static WritableMap toNotificationOpenMap(Intent intent) {
return notificationOpenMap;
}


public void refreshToken() throws Exception {
FirebaseInstanceId.getInstance().deleteInstanceId();
FirebaseInstanceId.getInstance().getInstanceId();
}

public void askPermissions() {
if (ContextCompat.checkSelfPermission(this.reactContext.getCurrentActivity(), Manifest.permission.ACCESS_NOTIFICATION_POLICY) == PackageManager.PERMISSION_GRANTED) {
this.logger.format(Level.DEBUG, "GRANTED", "GRANTED");

return;
}

this.logger.format(Level.DEBUG, "NOT_GRANTED", "NOT_GRANTED");

ActivityCompat.requestPermissions(
this.reactContext.getCurrentActivity(),
new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY},
PERMISSION_CODE);
}

public void register() throws Exception {
this.logger.format(Level.DEBUG, "REGISTER", "REGISTER");
this.askPermissions();
FirebaseInstanceId.getInstance().getInstanceId();
}

public void unregister() throws Exception {
FirebaseInstanceId.getInstance().deleteInstanceId();
this.notificationDriver.receiveFCMToken(null);
}

/**
* Called when host (activity/service) receives an {@link Activity#onActivityResult} call.
*
Expand Down
8 changes: 4 additions & 4 deletions client/react-native/gomobile/core/notification.go
Expand Up @@ -13,7 +13,7 @@ import (
var _ notification.Driver = (*MobileNotification)(nil)

type NativeNotificationDriver interface {
Display(title, body, icon, sound, badge string) error
DisplayNotification(title, body, icon, sound, url string) error
Register() error
Unregister() error
RefreshToken() error
Expand Down Expand Up @@ -114,15 +114,15 @@ func (n *MobileNotification) UnsubscribeToken(sub chan *notification.Token) {
// Native
//
func (n *MobileNotification) Display(p *notification.Payload) error {
return n.Native.Display(p.Title, p.Body, p.Icon, p.Sound, p.Badge)
return n.Native.DisplayNotification(p.Title, p.Body, p.Icon, p.Sound, p.DeepLink)
}

func (n *MobileNotification) Register() error {
return n.Native.Register()
}

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

func (n *MobileNotification) Unregister() error {
Expand Down
6 changes: 0 additions & 6 deletions client/react-native/ios/Berty/AppDelegate.m
Expand Up @@ -51,12 +51,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if (notification.userInfo[@"url"] != nil && [(NSString *)notification.userInfo[@"url"] length]) {
[self application:application openURL:[NSURL URLWithString:notification.userInfo[@"url"]] options:@{
UIApplicationOpenURLOptionsSourceApplicationKey: [[NSBundle mainBundle] bundleIdentifier],
UIApplicationOpenURLOptionsOpenInPlaceKey: @NO,
}];
}
[RCTPushNotificationManager didReceiveLocalNotification:notification];
}

Expand Down
14 changes: 12 additions & 2 deletions client/react-native/ios/modules/core/Notification.swift
Expand Up @@ -29,7 +29,7 @@ class Notification: NSObject, UNUserNotificationCenterDelegate, CoreNativeNotifi
completionHandler([.alert, .badge, .sound])
}

func display(_ title: String?, body: String?, icon: String?, sound: String?, url: String?) throws {
func displayNotification(_ title: String?, body: String?, icon: String?, sound: String?, url: String?) throws {
guard let utitle = title, let ubody = body else {
throw NotificationError.invalidArgument
}
Expand All @@ -40,7 +40,7 @@ class Notification: NSObject, UNUserNotificationCenterDelegate, CoreNativeNotifi
let content = UNMutableNotificationContent()
content.title = utitle
content.body = ubody
content.userInfo = [ "url" : url ]
content.userInfo = [ "url": url ]
content.categoryIdentifier = "berty.core.notification"
content.sound = UNNotificationSound.default()

Expand Down Expand Up @@ -141,6 +141,16 @@ extension AppDelegate {
}

override func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
if let data = notification.userInfo as? [String: String] {
if let url = data["url"] {
if url.count > 0 {
self.application(application, open: URL.init(string: url)!, options: [
UIApplicationOpenURLOptionsKey.sourceApplication: Bundle.main.bundleIdentifier!,
UIApplicationOpenURLOptionsKey.openInPlace: false
])
}
}
}
// RCTPushNotificationManager.didReceive(notification)
}
}
12 changes: 3 additions & 9 deletions core/node/event_handlers.go
Expand Up @@ -83,19 +83,13 @@ func (n *Node) handleContactRequestAccepted(ctx context.Context, input *p2p.Even
return err
}

<<<<<<< HEAD
n.DisplayNotification(&notification.Payload{
||||||| parent of 1025f3c... feat(ios): handle all local notif
n.DisplayNotification(notification.Payload{
=======
var deepLink string
conv, err := bsql.ConversationOneToOne(sql, n.config.Myself.ID, contact.ID)
if err == nil {
deepLink = "berty://conversation#id=" + url.PathEscape(conv.ID)
}

n.DisplayNotification(notification.Payload{
>>>>>>> 1025f3c... feat(ios): handle all local notif
n.DisplayNotification(&notification.Payload{
Title: i18n.T("ContactRequestAccpetedTitle", nil),
Body: i18n.T("ContactRequestAccpetedBody", map[string]interface{}{
"Name": contact.DisplayName,
Expand Down Expand Up @@ -158,7 +152,7 @@ func (n *Node) handleConversationInvite(ctx context.Context, input *p2p.Event) e
return err
}

n.DisplayNotification(notification.Payload{
n.DisplayNotification(&notification.Payload{
Title: i18n.T("ConversationInviteTitle", nil),
Body: i18n.T("ConversationInviteBody", nil),
DeepLink: "berty://conversation#id=" + url.PathEscape(attrs.Conversation.ID),
Expand All @@ -176,7 +170,7 @@ func (n *Node) handleConversationNewMessage(ctx context.Context, input *p2p.Even
// say that conversation has not been read
n.sql(ctx).Save(&entity.Conversation{ID: input.ConversationID, ReadAt: time.Time{}})

n.DisplayNotification(notification.Payload{
n.DisplayNotification(&notification.Payload{
Title: i18n.T("NewMessageTitle", nil),
Body: i18n.T("NewMessageBody", nil),
DeepLink: "berty://conversation#id=" + url.PathEscape(input.ConversationID),
Expand Down
2 changes: 1 addition & 1 deletion core/node/notification.go
Expand Up @@ -15,7 +15,7 @@ func WithNotificationDriver(driver notification.Driver) NewNodeOption {
}

func (n *Node) DisplayNotification(payload *notification.Payload) {
if err := n.notificationDriver.Display(payload); err != nil {
if err := n.notificationDriver.DisplayNotification(payload); err != nil {
logger().Error("Notification", zap.Error(err))
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/pkg/notification/notification.go
Expand Up @@ -15,7 +15,7 @@ import (
)

type Driver interface {
Display(*Payload) error
DisplayNotification(*Payload) error
Register() error
Unregister() error
Subscribe() chan *Payload
Expand Down Expand Up @@ -52,7 +52,7 @@ func NewNoopNotification() Driver {
return &NoopNotification{}
}

func (n *NoopNotification) Display(p *Payload) error {
func (n *NoopNotification) DisplayNotification(p *Payload) error {
// for debug puprpose
logger().Debug("Display",
zap.String("title", p.Title),
Expand Down Expand Up @@ -109,7 +109,7 @@ func NewDesktopNotification() Driver {

type DesktopNotification struct{}

func (n *DesktopNotification) Display(p *Payload) error {
func (n *DesktopNotification) DisplayNotification(p *Payload) error {
once.Do(func() {
_, filename, _, _ := runtime.Caller(0)
iconPath := path.Dir(filename) + "/../../../client/react-native/common/static/img/logo.png"
Expand Down

0 comments on commit cf34d46

Please sign in to comment.