Skip to content

Commit

Permalink
fix(android): permissions & retrieve token
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 17, 2019
1 parent 10cf29b commit a483869
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
Expand Up @@ -24,6 +24,7 @@

import chat.berty.core.Level;
import chat.berty.core.Logger;
import chat.berty.main.BuildConfig;
import chat.berty.main.R;
import core.Core;
import core.MobileNotification;
Expand All @@ -44,31 +45,34 @@ public void display(String title, String body, String icon, String sound, String
}

public void refreshToken() throws Exception {
FirebaseInstanceId.getInstance().deleteInstanceId();
FirebaseInstanceId.getInstance().getInstanceId();
FirebaseInstanceId.getInstance().deleteToken(BuildConfig.APPLICATION_ID, "GCM");
FirebaseInstanceId.getInstance().deleteInstanceId();
FirebaseInstanceId.getInstance().getInstanceId();
FirebaseInstanceId.getInstance().getToken(BuildConfig.APPLICATION_ID, "GCM");
}

public void askPermissions() {
ReactApplicationContext context = NotificationModule.getInstance().getReactApplicationContext();

if (ContextCompat.checkSelfPermission(context.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(
context.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();
// TODO: do not refresh token every time in prod just uncomment this following lines and remove refreshToken when all good
// FirebaseInstanceId.getInstance().getInstanceId();
// FirebaseInstanceId.getInstance().getToken(BuildConfig.APPLICATION_ID, "GCM");
this.refreshToken();
}

public void unregister() throws Exception {
FirebaseInstanceId.getInstance().deleteToken(BuildConfig.APPLICATION_ID, "GCM");
FirebaseInstanceId.getInstance().deleteInstanceId();
gomobile.receiveFCMToken(null);
}
Expand Down
3 changes: 1 addition & 2 deletions client/react-native/gomobile/core/notification.go
@@ -1,7 +1,6 @@
package core

import (
"encoding/hex"
"fmt"
"sync"

Expand Down Expand Up @@ -62,7 +61,7 @@ func (n *MobileNotification) ReceiveFCMToken(token []byte) {
func (n *MobileNotification) ReceiveToken(token *notification.Token) {
logger().Debug("receive token",
zap.String("type", token.Type.String()),
zap.String("token", hex.EncodeToString(token.Value)),
zap.String("hash", token.Hash()),
)
n.tokenSubscribersMutex.Lock()
for i := range n.subscribers {
Expand Down
3 changes: 2 additions & 1 deletion core/node/notification.go
Expand Up @@ -34,7 +34,8 @@ func (n *Node) UseNotificationDriver() {
)
case token := <-tokenChan:
logger().Debug("node receive notification token",
zap.String("token", fmt.Sprintf("%+v", token.String())),
zap.String("type", token.Type.String()),
zap.String("hash", token.Hash()),
)
case <-n.shutdown:
logger().Debug("node notification driver shutdown")
Expand Down
12 changes: 9 additions & 3 deletions core/pkg/notification/notification.go
Expand Up @@ -4,7 +4,6 @@ package notification

import (
"encoding/hex"
"fmt"
"path"
"runtime"
"sync"
Expand All @@ -30,8 +29,15 @@ type Token struct {
Type p2p.DevicePushType
}

func (t *Token) String() string {
return fmt.Sprintf("hash: %+v, type: %+v", hex.EncodeToString(t.Value), t.Type)
func (t *Token) Hash() string {
switch t.Type {
default:
return ""
case p2p.DevicePushType_FCM:
return string(t.Value)
case p2p.DevicePushType_APNS:
return hex.EncodeToString(t.Value)
}
}

type Payload struct {
Expand Down

0 comments on commit a483869

Please sign in to comment.