Skip to content

Commit 24450fe

Browse files
authored
Merge pull request #24 from luigivitelli23/master
Enabling the instance id and token delete
2 parents 7a19f77 + 27e2703 commit 24450fe

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ Configure Firebase without configuration parameters.
4040

4141
\* By passing the `file` property, you can give a location to the Firebase plist file (usually named "GoogleService-Info.plist"), which contains all necessary properties for your Firebase project. This makes all other properties unnecessary. For Android: place the file in `/app/assets/android/` and pass just the filename.
4242

43+
##### `deleteInstanceId(callback)`
44+
45+
Delete the current `instanceId` (invalidating all tokens). See the [Firebase docs](https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId.html#deleteInstanceId()) for details.
46+
47+
The callback receives an object containing this fields:
48+
49+
| Key | Type |Description |
50+
| - | - | - |
51+
| `success` | Boolean | `true` if the deletion succeeded | *
52+
| `error` | String | The localized error message | *
53+
54+
##### `deleteToken(authorizedEntity, scope, callback)`
55+
56+
Delete the token of the provided `authorizedEntity` and `scope`. See the [Firebase docs](https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId#deleteToken(java.lang.String,%20java.lang.String)) for details.
57+
58+
The callback receives an object containing this fields:
59+
60+
| Key | Type | Description |
61+
| - | - | - |
62+
| `success` | Boolean | `true` if the deletion succeeded | *
63+
| `error` | String | The localized error message | *
64+
4365
## Examples
4466

4567
```js

android/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# this is your module manifest and used by Titanium
33
# during compilation, packaging, distribution, etc.
44
#
5-
version: 2.2.0
5+
version: 2.3.0
66
apiversion: 4
77
architectures: arm64-v8a armeabi-v7a x86
88
description: titanium-firebase-core

android/src/firebase/core/TitaniumFirebaseCoreModule.java

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
*/
99
package firebase.core;
1010

11+
import android.os.AsyncTask;
12+
1113
import org.appcelerator.kroll.KrollModule;
14+
import org.appcelerator.kroll.KrollFunction;
1215
import org.appcelerator.kroll.annotations.Kroll;
1316
import org.appcelerator.titanium.TiApplication;
1417
import org.appcelerator.kroll.common.Log;
@@ -18,11 +21,14 @@
1821

1922
import com.google.firebase.FirebaseApp;
2023
import com.google.firebase.FirebaseOptions;
24+
import com.google.firebase.iid.FirebaseInstanceId;
2125
import org.json.JSONArray;
2226
import org.json.JSONException;
2327
import org.json.JSONObject;
2428
import java.io.IOException;
2529
import java.io.InputStream;
30+
import java.lang.Void;
31+
import java.util.HashMap;
2632

2733
@Kroll.module(name = "TitaniumFirebaseCore", id = "firebase.core")
2834
public class TitaniumFirebaseCoreModule extends KrollModule
@@ -126,6 +132,58 @@ public void configure(@Kroll.argument(optional = true) KrollDict param)
126132
}
127133
}
128134

135+
@Kroll.method
136+
public void deleteInstanceId(final KrollFunction callback)
137+
{
138+
new AsyncTask<Void, Void, IOException>() {
139+
protected IOException doInBackground(Void ... v) {
140+
try {
141+
FirebaseInstanceId.getInstance().deleteInstanceId();
142+
return null;
143+
} catch (IOException e) {
144+
e.printStackTrace();
145+
return e;
146+
}
147+
}
148+
protected void onPostExecute(IOException error) {
149+
if (callback != null) {
150+
HashMap args = new HashMap<>();
151+
args.put("success", error == null);
152+
if (error != null) {
153+
args.put("error", error.getLocalizedMessage());
154+
}
155+
callback.call(getKrollObject(), args);
156+
}
157+
}
158+
}.execute();
159+
}
160+
161+
@Kroll.method
162+
public void deleteToken(final String authorizedEntity, final String scope, final KrollFunction callback)
163+
{
164+
new AsyncTask<Void, Void, IOException>() {
165+
protected IOException doInBackground(Void ... v) {
166+
try {
167+
FirebaseInstanceId.getInstance().deleteToken(authorizedEntity, scope);
168+
return null;
169+
} catch (IOException e) {
170+
e.printStackTrace();
171+
return e;
172+
}
173+
}
174+
protected void onPostExecute(IOException error) {
175+
if (callback != null) {
176+
HashMap args = new HashMap<>();
177+
args.put("success", error == null);
178+
if (error != null) {
179+
args.put("error", error.getLocalizedMessage());
180+
}
181+
callback.call(getKrollObject(), args);
182+
}
183+
}
184+
}.execute();
185+
}
186+
129187
public String loadJSONFromAsset(String filename)
130188
{
131189
String json = null;
@@ -140,7 +198,6 @@ public String loadJSONFromAsset(String filename)
140198
inStream.close();
141199
json = new String(buffer, "UTF-8");
142200
} catch (IOException ex) {
143-
Log.e(LCAT, "Error reading file");
144201
return "";
145202
}
146203
return json;

ios/Classes/FirebaseCoreModule.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212

1313
- (void)configure:(id)arguments;
1414

15+
- (void)deleteInstanceId:(id)callback;
16+
17+
- (void)deleteToken:(id)arguments;
18+
1519
@end

ios/Classes/FirebaseCoreModule.m

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#import <FirebaseCore/FirebaseCore.h>
9+
#import <FirebaseInstanceId/FIRInstanceID.h>
910

1011
#import "FirebaseCoreModule.h"
1112
#import "TiBase.h"
@@ -98,4 +99,45 @@ - (void)configure:(id)arguments
9899
[FIRApp configureWithOptions:options];
99100
}
100101

102+
- (void)deleteInstanceId:(id)callback
103+
{
104+
ENSURE_SINGLE_ARG_OR_NIL(callback, KrollCallback);
105+
106+
[[FIRInstanceID instanceID] deleteIDWithHandler:^(NSError *error) {
107+
if (callback != nil) {
108+
NSDictionary *dict = nil;
109+
if (error != nil) {
110+
dict = @{ @"success": @NO, @"error": [error localizedDescription] };
111+
} else {
112+
dict = @{ @"success": @YES };
113+
}
114+
[callback call:@[dict] thisObject:nil];
115+
}
116+
}];
117+
}
118+
119+
- (void)deleteToken:(id)arguments
120+
{
121+
NSString *authorizedEntity;
122+
ENSURE_ARG_AT_INDEX(authorizedEntity, arguments, 0, NSString);
123+
124+
NSString *scope;
125+
ENSURE_ARG_AT_INDEX(scope, arguments, 1, NSString);
126+
127+
KrollCallback *callback;
128+
ENSURE_ARG_OR_NIL_AT_INDEX(callback, arguments, 2, KrollCallback);
129+
130+
[[FIRInstanceID instanceID] deleteTokenWithAuthorizedEntity:authorizedEntity scope:scope handler:^(NSError *error) {
131+
if (callback != nil) {
132+
NSDictionary *dict = nil;
133+
if (error != nil) {
134+
dict = @{ @"success": @NO, @"error": [error localizedDescription] };
135+
} else {
136+
dict = @{ @"success": @YES };
137+
}
138+
[callback call:@[dict] thisObject:nil];
139+
}
140+
}];
141+
}
142+
101143
@end

ios/manifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# this is your module manifest and used by Titanium
33
# during compilation, packaging, distribution, etc.
44
#
5-
version: 1.2.1
5+
version: 1.3.0
66
apiversion: 2
77
architectures: armv7 arm64 i386 x86_64
88
description: titanium-firebase-core

0 commit comments

Comments
 (0)