diff --git a/firebase.android.js b/firebase.android.js index 027e97cd..6ac02aaa 100755 --- a/firebase.android.js +++ b/firebase.android.js @@ -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(); @@ -1778,4 +1779,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; \ No newline at end of file diff --git a/firebase.d.ts b/firebase.d.ts index 5bfa231f..9ab129a3 100644 --- a/firebase.d.ts +++ b/firebase.d.ts @@ -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; // Database @@ -673,3 +710,8 @@ export function unsubscribeFromTopic(topicName): Promise; // crash logging export function sendCrashLog(options: SendCrashLogOptions): Promise; + +// invites +export function sendInvitation(options: SendInvitationOptions): Promise; + +export function getInvitation(): Promise \ No newline at end of file diff --git a/platforms/android/include.gradle b/platforms/android/include.gradle index 22b98268..3bc57a4a 100644 --- a/platforms/android/include.gradle +++ b/platforms/android/include.gradle @@ -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" diff --git a/scripts/installer.js b/scripts/installer.js index 0f5c326a..dee6c665 100755 --- a/scripts/installer.js +++ b/scripts/installer.js @@ -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); @@ -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" @@ -408,4 +413,4 @@ module.exports = function() { */ function isSelected(value) { return value === true || (typeof value === "string" && value.toLowerCase() === 'y'); -} +} \ No newline at end of file