Skip to content

Commit

Permalink
Merge pull request #24 from luigivitelli23/master
Browse files Browse the repository at this point in the history
Enabling the instance id and token delete
  • Loading branch information
hansemannn committed Oct 7, 2018
2 parents 7a19f77 + 27e2703 commit 24450fe
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 3 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ Configure Firebase without configuration parameters.

\* 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.

##### `deleteInstanceId(callback)`

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.

The callback receives an object containing this fields:

| Key | Type |Description |
| - | - | - |
| `success` | Boolean | `true` if the deletion succeeded | *
| `error` | String | The localized error message | *

##### `deleteToken(authorizedEntity, scope, callback)`

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.

The callback receives an object containing this fields:

| Key | Type | Description |
| - | - | - |
| `success` | Boolean | `true` if the deletion succeeded | *
| `error` | String | The localized error message | *

## Examples

```js
Expand Down
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 2.2.0
version: 2.3.0
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86
description: titanium-firebase-core
Expand Down
59 changes: 58 additions & 1 deletion android/src/firebase/core/TitaniumFirebaseCoreModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
*/
package firebase.core;

import android.os.AsyncTask;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
Expand All @@ -18,11 +21,14 @@

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.iid.FirebaseInstanceId;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Void;
import java.util.HashMap;

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

@Kroll.method
public void deleteInstanceId(final KrollFunction callback)
{
new AsyncTask<Void, Void, IOException>() {
protected IOException doInBackground(Void ... v) {
try {
FirebaseInstanceId.getInstance().deleteInstanceId();
return null;
} catch (IOException e) {
e.printStackTrace();
return e;
}
}
protected void onPostExecute(IOException error) {
if (callback != null) {
HashMap args = new HashMap<>();
args.put("success", error == null);
if (error != null) {
args.put("error", error.getLocalizedMessage());
}
callback.call(getKrollObject(), args);
}
}
}.execute();
}

@Kroll.method
public void deleteToken(final String authorizedEntity, final String scope, final KrollFunction callback)
{
new AsyncTask<Void, Void, IOException>() {
protected IOException doInBackground(Void ... v) {
try {
FirebaseInstanceId.getInstance().deleteToken(authorizedEntity, scope);
return null;
} catch (IOException e) {
e.printStackTrace();
return e;
}
}
protected void onPostExecute(IOException error) {
if (callback != null) {
HashMap args = new HashMap<>();
args.put("success", error == null);
if (error != null) {
args.put("error", error.getLocalizedMessage());
}
callback.call(getKrollObject(), args);
}
}
}.execute();
}

public String loadJSONFromAsset(String filename)
{
String json = null;
Expand All @@ -140,7 +198,6 @@ public String loadJSONFromAsset(String filename)
inStream.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
Log.e(LCAT, "Error reading file");
return "";
}
return json;
Expand Down
4 changes: 4 additions & 0 deletions ios/Classes/FirebaseCoreModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@

- (void)configure:(id)arguments;

- (void)deleteInstanceId:(id)callback;

- (void)deleteToken:(id)arguments;

@end
42 changes: 42 additions & 0 deletions ios/Classes/FirebaseCoreModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#import <FirebaseCore/FirebaseCore.h>
#import <FirebaseInstanceId/FIRInstanceID.h>

#import "FirebaseCoreModule.h"
#import "TiBase.h"
Expand Down Expand Up @@ -98,4 +99,45 @@ - (void)configure:(id)arguments
[FIRApp configureWithOptions:options];
}

- (void)deleteInstanceId:(id)callback
{
ENSURE_SINGLE_ARG_OR_NIL(callback, KrollCallback);

[[FIRInstanceID instanceID] deleteIDWithHandler:^(NSError *error) {
if (callback != nil) {
NSDictionary *dict = nil;
if (error != nil) {
dict = @{ @"success": @NO, @"error": [error localizedDescription] };
} else {
dict = @{ @"success": @YES };
}
[callback call:@[dict] thisObject:nil];
}
}];
}

- (void)deleteToken:(id)arguments
{
NSString *authorizedEntity;
ENSURE_ARG_AT_INDEX(authorizedEntity, arguments, 0, NSString);

NSString *scope;
ENSURE_ARG_AT_INDEX(scope, arguments, 1, NSString);

KrollCallback *callback;
ENSURE_ARG_OR_NIL_AT_INDEX(callback, arguments, 2, KrollCallback);

[[FIRInstanceID instanceID] deleteTokenWithAuthorizedEntity:authorizedEntity scope:scope handler:^(NSError *error) {
if (callback != nil) {
NSDictionary *dict = nil;
if (error != nil) {
dict = @{ @"success": @NO, @"error": [error localizedDescription] };
} else {
dict = @{ @"success": @YES };
}
[callback call:@[dict] thisObject:nil];
}
}];
}

@end
2 changes: 1 addition & 1 deletion ios/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 1.2.1
version: 1.3.0
apiversion: 2
architectures: armv7 arm64 i386 x86_64
description: titanium-firebase-core
Expand Down

0 comments on commit 24450fe

Please sign in to comment.