From cf34d4646f4f44ecec7050530ea62051fb4fec35 Mon Sep 17 00:00:00 2001 From: Sacha Froment Date: Wed, 16 Jan 2019 14:22:49 +0100 Subject: [PATCH] fix: fix merge conflict Signed-off-by: Sacha Froment --- .../chat/berty/core/NotificationManager.java | 51 +++++++++++++++++-- .../gomobile/core/notification.go | 8 +-- client/react-native/ios/Berty/AppDelegate.m | 6 --- .../ios/modules/core/Notification.swift | 14 ++++- core/node/event_handlers.go | 12 ++--- core/node/notification.go | 2 +- core/pkg/notification/notification.go | 6 +-- 7 files changed, 69 insertions(+), 30 deletions(-) diff --git a/client/react-native/android/app/src/main/java/chat/berty/core/NotificationManager.java b/client/react-native/android/app/src/main/java/chat/berty/core/NotificationManager.java index 5fe1984830..8a52c37218 100644 --- a/client/react-native/android/app/src/main/java/chat/berty/core/NotificationManager.java +++ b/client/react-native/android/app/src/main/java/chat/berty/core/NotificationManager.java @@ -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; @@ -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; @@ -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; @@ -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); } @@ -59,7 +68,7 @@ public void displayNotification(String title, String body, String icon, String s public void onMessageReceived(RemoteMessage remoteMessage) { Map map = remoteMessage.getData(); String data = new Gson().toJson(map); - this.notificationDriver.receiveNotification(data); + this.notificationDriver.receive(data); } /** @@ -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) { @@ -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. * diff --git a/client/react-native/gomobile/core/notification.go b/client/react-native/gomobile/core/notification.go index 5dac095a81..1554854070 100644 --- a/client/react-native/gomobile/core/notification.go +++ b/client/react-native/gomobile/core/notification.go @@ -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 @@ -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 { diff --git a/client/react-native/ios/Berty/AppDelegate.m b/client/react-native/ios/Berty/AppDelegate.m index f55d4c4176..64fdc87ea5 100644 --- a/client/react-native/ios/Berty/AppDelegate.m +++ b/client/react-native/ios/Berty/AppDelegate.m @@ -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]; } diff --git a/client/react-native/ios/modules/core/Notification.swift b/client/react-native/ios/modules/core/Notification.swift index ab36b5e699..4d349fb4dc 100644 --- a/client/react-native/ios/modules/core/Notification.swift +++ b/client/react-native/ios/modules/core/Notification.swift @@ -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 } @@ -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() @@ -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) } } diff --git a/core/node/event_handlers.go b/core/node/event_handlers.go index 79f7825bbf..85c4af3ec0 100644 --- a/core/node/event_handlers.go +++ b/core/node/event_handlers.go @@ -83,19 +83,13 @@ func (n *Node) handleContactRequestAccepted(ctx context.Context, input *p2p.Even return err } -<<<<<<< HEAD - n.DisplayNotification(¬ification.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(¬ification.Payload{ Title: i18n.T("ContactRequestAccpetedTitle", nil), Body: i18n.T("ContactRequestAccpetedBody", map[string]interface{}{ "Name": contact.DisplayName, @@ -158,7 +152,7 @@ func (n *Node) handleConversationInvite(ctx context.Context, input *p2p.Event) e return err } - n.DisplayNotification(notification.Payload{ + n.DisplayNotification(¬ification.Payload{ Title: i18n.T("ConversationInviteTitle", nil), Body: i18n.T("ConversationInviteBody", nil), DeepLink: "berty://conversation#id=" + url.PathEscape(attrs.Conversation.ID), @@ -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(¬ification.Payload{ Title: i18n.T("NewMessageTitle", nil), Body: i18n.T("NewMessageBody", nil), DeepLink: "berty://conversation#id=" + url.PathEscape(input.ConversationID), diff --git a/core/node/notification.go b/core/node/notification.go index a9adee6838..34818ceb7d 100644 --- a/core/node/notification.go +++ b/core/node/notification.go @@ -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)) } } diff --git a/core/pkg/notification/notification.go b/core/pkg/notification/notification.go index 2a119aadc0..6e19ddc3ca 100644 --- a/core/pkg/notification/notification.go +++ b/core/pkg/notification/notification.go @@ -15,7 +15,7 @@ import ( ) type Driver interface { - Display(*Payload) error + DisplayNotification(*Payload) error Register() error Unregister() error Subscribe() chan *Payload @@ -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), @@ -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"