Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Add Feature Firebase Invites #362

Merged
merged 1 commit into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions firebase.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ firebase._facebookAccessToken = null;

var fbCallbackManager = null;
var GOOGLE_SIGNIN_INTENT_ID = 123;
var REQUEST_INVITE_INTENT_ID = 48

var gson = typeof(com.google.gson) === "undefined" ? null : new com.google.gson.Gson();

Expand Down Expand Up @@ -1773,4 +1774,144 @@ firebase.sendCrashLog = function (arg) {
});
};

firebase.sendInvitation = function (arg) {
return new Promise(function (resolve, reject) {
try {

if (typeof(com.google.android.gms.appinvite) === "undefined") {
reject("Make sure firebase-invites is in the plugin's include.gradle");
return;
}

if (!arg.message || !arg.title) {
reject("The mandatory 'message' or 'title' argument is missing");
return;
}

var builder = new com.google.android.gms.appinvite.AppInviteInvitation.IntentBuilder(arg.title).setMessage(arg.message)

if (arg.deepLink) {
builder.setDeepLink(android.net.Uri.parse(arg.deepLink))
}

if (arg.callToActionText) {
builder.setCallToActionText(arg.callToActionText)
}

if (arg.customImage) {
builder.setCustomImage(android.net.Uri.parse(arg.customImage))
}

if (arg.iosClientID) {
builder.setOtherPlatformsTargetApplication(com.google.android.gms.appinvite.AppInviteInvitation.IntentBuilder.PlatformMode.PROJECT_PLATFORM_IOS, arg.iosClientID);
}

var firebaseInviteIntent = builder.build()

appModule.android.foregroundActivity.startActivityForResult(firebaseInviteIntent, REQUEST_INVITE_INTENT_ID);

appModule.android.onActivityResult = function (requestCode, resultCode, data) {

if (requestCode === REQUEST_INVITE_INTENT_ID) {

if (resultCode == android.app.Activity.RESULT_OK) {
// Get the invitation IDs of all sent messages
var ids = com.google.android.gms.appinvite.AppInviteInvitation.getInvitationIds(resultCode, data);

try {
var invitationIds = firebase.toJsObject(ids)
var result = {
count: ids.length,
invitationIds: invitationIds
}

resolve(result)
} catch (e) {
reject(e)
}

} else {
if (resultCode === 3) {
reject("Resultcode 3, see http://stackoverflow.com/questions/37883664/result-code-3-when-implementing-appinvites");
} else {
reject("Resultcode: " + resultCode);
}
}
}
};

} catch (ex) {
console.log("Error in firebase.sendInvitation: " + ex);
reject(ex);
}
});
};

firebase.getInvitation = function () {
return new Promise(function (resolve, reject) {
try {

if (typeof(com.google.android.gms.appinvite) === "undefined") {
reject("Make sure firebase-invites is in the plugin's include.gradle");
return;
}

var onConnectionFailedListener = new com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener({
onConnectionFailed: function (connectionResult) {
reject(connectionResult.getErrorMessage());
}
});

var autoLaunchDeepLink = false;
var activity = appModule.android.foregroundActivity;

firebase._mGoogleApiClient = new com.google.android.gms.common.api.GoogleApiClient.Builder(com.tns.NativeScriptApplication.getInstance())
.addOnConnectionFailedListener(onConnectionFailedListener)
.addApi(com.google.android.gms.appinvite.AppInvite.API)
.build()

firebase._mGoogleApiClient.connect()

var getInvitationCallback = new com.google.android.gms.common.api.ResultCallback({
onResult: function(result){

console.log("getInvitation:onResult:", result.getStatus().isSuccess())
if (result.getStatus().isSuccess()) {
// Extract information from the intent
var intent = result.getInvitationIntent();

try {

var deepLink = com.google.android.gms.appinvite.AppInviteReferral.getDeepLink(intent);
var invitationId = com.google.android.gms.appinvite.AppInviteReferral.getInvitationId(intent);

var result = {
deepLink: firebase.toJsObject(deepLink),
invitationId: firebase.toJsObject(invitationId)
}

resolve(result)

} catch (e) {
reject(e)
}

}
else {
reject("Not launched by invitation")
}
}
});

com.google.android.gms.appinvite.AppInvite.AppInviteApi.getInvitation(firebase._mGoogleApiClient, activity, autoLaunchDeepLink)
.setResultCallback(getInvitationCallback);


} catch (ex) {
console.log("Error in firebase.getInvitation: " + ex);
reject(ex);
}
});
};

module.exports = firebase;
42 changes: 42 additions & 0 deletions firebase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,43 @@ export interface SendCrashLogOptions {
showInConsole: boolean;
}

export interface SendInvitationOptions {
/**
* Invitation title you want to send
*/
title: string;

/**
* Sets the default message sent with invitations
*/
message: string;

/**
* Sets the link into your app that is sent with invitations.
*/
deepLink?: string;

/**
* Sets the call-to-action text of the button rendered in email invitations. Cannot exceed 32 characters.
*/
callToActionText?: string;

/**
* Sets the URL of a custom image to include in email invitations. The image must be square and around 600x600 pixels. The image can be no larger than 4000x4000 pixels.
*/
customImage?: string;

/**
* If you have an Android version of your app and you want to send an invitation that can be opened on Android in addition to iOS
*/
androidClientID?: string;

/**
* You can find your iOS app's client ID in the GoogleService-Info.plist file you downloaded from the Firebase console
*/
iosClientID?: string;
}

export function init(options?: InitOptions): Promise<any>;

// Database
Expand Down Expand Up @@ -673,3 +710,8 @@ export function unsubscribeFromTopic(topicName): Promise<any>;

// crash logging
export function sendCrashLog(options: SendCrashLogOptions): Promise<any>;

// invites
export function sendInvitation(options: SendInvitationOptions): Promise<any>;

export function getInvitation(): Promise<any>
3 changes: 3 additions & 0 deletions platforms/android/include.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ dependencies {
// Uncomment if you need Google Sign-In Authentication
// compile "com.google.android.gms:play-services-auth:$googlePlayServicesVersion"

// Uncomment if you want to use 'Invites'
// compile 'com.google.firebase:firebase-invites:10.2.+'

}

apply plugin: "com.google.gms.google-services"
9 changes: 7 additions & 2 deletions scripts/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ function promptQuestions() {
name: 'admob',
description: 'Are you using AdMob (y/n)',
default: 'n'
}, {
name: 'invites',
description: 'Are you using Firebase Invites (y/n)',
default: 'n'
}], function (err, result) {
if (err) {
return console.log(err);
Expand Down Expand Up @@ -301,7 +305,8 @@ dependencies {

// Uncomment if you need Google Sign-In Authentication
` + (isSelected(result.google_auth) ? `` : `//`) + ` compile "com.google.android.gms:play-services-auth:$googlePlayServicesVersion"

// Uncomment if you need Firebase Invites
` + (isSelected(result.invites) ? `` : `//`) + ` compile "com.google.firebase:firebase-invites:10.2.+"
}

apply plugin: "com.google.gms.google-services"
Expand Down Expand Up @@ -408,4 +413,4 @@ module.exports = function() {
*/
function isSelected(value) {
return value === true || (typeof value === "string" && value.toLowerCase() === 'y');
}
}