Skip to content

Commit

Permalink
adds support for getToken(string,string) and deleteToken(string,strin…
Browse files Browse the repository at this point in the history
…g) to iid
  • Loading branch information
ctaintor committed Jun 17, 2018
1 parent 88e2321 commit 3652a05
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.invertase.firebase.instanceid;


import java.io.IOException;

import android.util.Log;

import com.facebook.react.bridge.Promise;
Expand Down Expand Up @@ -42,4 +44,26 @@ public void get(Promise promise){
String id = FirebaseInstanceId.getInstance().getId();
promise.resolve(id);
}

@ReactMethod
public void getToken(String authorizedEntity, String scope, Promise promise) {
try {
String token = FirebaseInstanceId.getInstance().getToken(authorizedEntity, scope);
Log.d(TAG, "Firebase token for " + authorizedEntity + ": " + token);
promise.resolve(token);
} catch (IOException e) {
promise.reject("iid/request-failed", "getToken request failed", e);
}
}

@ReactMethod
public void deleteToken(String authorizedEntity, String scope, Promise promise) {
try {
FirebaseInstanceId.getInstance().deleteToken(authorizedEntity, scope);
Log.d(TAG, "Firebase token deleted for " + authorizedEntity);
promise.resolve(null);
} catch (IOException e) {
promise.reject("iid/request-failed", "deleteToken request failed", e);
}
}
}
31 changes: 31 additions & 0 deletions ios/RNFirebase/instanceid/RNFirebaseInstanceId.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "RNFirebaseInstanceId.h"

#if __has_include(<FirebaseInstanceID/FIRInstanceID.h>)
#import <FirebaseMessaging/FirebaseMessaging.h>
#import <FirebaseInstanceID/FIRInstanceID.h>

@implementation RNFirebaseInstanceId
Expand All @@ -26,6 +27,36 @@ @implementation RNFirebaseInstanceId
}];
}

RCT_EXPORT_METHOD(getToken:(NSString *)authorizedEntity
scope:(NSString *)scope
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSDictionary * options = nil;
if ([FIRMessaging messaging].APNSToken) {
options = @{@"apns_token": [FIRMessaging messaging].APNSToken};
}
[[FIRInstanceID instanceID] tokenWithAuthorizedEntity:authorizedEntity scope:scope options:options handler:^(NSString * _Nullable identity, NSError * _Nullable error) {
if (error) {
reject(@"instance_id_error", @"Failed to getToken", error);
} else {
resolve(identity);
}
}];
}

RCT_EXPORT_METHOD(deleteToken:(NSString *)authorizedEntity
scope:(NSString *)scope
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
[[FIRInstanceID instanceID] deleteTokenWithAuthorizedEntity:authorizedEntity scope:scope handler:^(NSError * _Nullable error) {
if (error) {
reject(@"instance_id_error", @"Failed to deleteToken", error);
} else {
resolve(nil);
}
}];
}

@end

#else
Expand Down
8 changes: 8 additions & 0 deletions lib/modules/iid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export default class InstanceId extends ModuleBase {
get(): Promise<string> {
return getNativeModule(this).get();
}

getToken(authorizedEntity: string, scope: string): Promise<string> {
return getNativeModule(this).getToken(authorizedEntity, scope);
}

deleteToken(authorizedEntity: string, scope: string): Promise<void> {
return getNativeModule(this).deleteToken(authorizedEntity, scope);
}
}

export const statics = {};
2 changes: 1 addition & 1 deletion tests/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ PODS:
- React/Core
- React/fishhook
- React/RCTBlob
- RNFirebase (4.1.0):
- RNFirebase (4.2.0):
- Firebase/Core
- React
- yoga (0.54.4.React)
Expand Down
35 changes: 35 additions & 0 deletions tests/src/tests/iid/iidTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import should from 'should';

function iidTests({ describe, it, firebase }) {
describe('iid', () => {
it('should delete the iid token', async () => {
await firebase.native.iid().delete();
});

it('it should return iid token from get', async () => {
const token = await firebase.native.iid().get();

token.should.be.a.String();
});

it('should return an FCM token from getToken with arguments', async () => {
await firebase.native.iid().delete();

const otherSenderIdToken = await firebase.native
.iid()
.getToken('305229645282', '*');

otherSenderIdToken.should.be.a.String();
});

it('should return nil from deleteToken', async () => {
const token = await firebase.native
.iid()
.deleteToken('305229645282', '*');

should.not.exist(token);
});
});
}

export default iidTests;
10 changes: 10 additions & 0 deletions tests/src/tests/iid/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import firebase from '../../firebase';
import TestSuite from '../../../lib/TestSuite';

import iidTests from './iidTests';

const suite = new TestSuite('Iid', 'firebase.id()', firebase);

suite.addTests(iidTests);

export default suite;
2 changes: 2 additions & 0 deletions tests/src/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import performance from './perf';
import admob from './admob';
import firestore from './firestore';
import links from './links/index';
import iid from './iid';

window.getCoverage = function getCoverage() {
return JSON.stringify(global.__coverage__);
Expand All @@ -31,6 +32,7 @@ const testSuiteInstances = [
performance,
storage,
links,
iid,
];

/*
Expand Down

0 comments on commit 3652a05

Please sign in to comment.