Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
JL-Vidinoti committed Jun 25, 2018
0 parents commit 621d793
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Cordova plugin for Vidinoti Push integration with Android

This plugin can be used together with the [cordova-plugin-pixlive](https://github.com/vidinoti/cordova-plugin-PixLive).
It adds the support for the Android push notifications. Previously, it was directly integrated within the `cordova-plugin-pixlive` but has been decoupled.

This plugin requires the Vidinoti SDK version >= 6.4.0

## How to use

1. Install and follow the instructions for the [cordova-plugin-pixlive](https://github.com/vidinoti/cordova-plugin-PixLive).
2. Connect to the [Firebase Console](https://console.firebase.google.com/). Create a new Firebase project and download the `google-services.json` file.
3. Place the `google-services.json` file in the root folder of your cordova project.
4. Install the plugin

cordova plugin add cordova-plugin-vidinoti-push

## What does the plugin

1. It takes the file `google-services.json` from the root folder and copy it to `platforms/android/`
2. Add required dependencies in gradle files
3. Copy Java classes (Android services) `VidinotiInstanceIDListenerService` and `VidinotiFcmListenerService`
4. Add the Android services to the `AndroidManifest.xml` file
5. In the "after prepare" hook scripts, it takes the `google-services.json` file and extracts the Google app ID and key. It then writes them in the Android `strings.xml` file

## How to contribute and create a new release

* Edit the plugin as needed
* Update the version number in `plugin.xml` and `package.json`
* Commit and push the changes to GitHub
* Create a new release from GitHub interface (releases > Draft a new release)
* Publish the release to npm registry (if necessary `npm login`): `npm publish`

## Release note

### Version 0.1.0 - 25 June 2018

Initial release

28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "cordova-plugin-vidinoti-push",
"version": "0.1.0",
"description": "Cordova Plugin for Vidinoti V-Director push integration",
"cordova": {
"id": "cordova-plugin-vidinoti-push",
"platforms": [
"android"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/vidinoti/cordova-plugin-vidinoti-push.git"
},
"keywords": [
"cordova",
"augmented reality",
"vidinoti",
"ecosystem:cordova",
"cordova-android"
],
"author": "Vidinoti SA",
"license": "MIT",
"bugs": {
"url": "https://github.com/vidinoti/cordova-plugin-vidinoti-push/issues"
},
"homepage": "https://github.com/vidinoti/cordova-plugin-vidinoti-push#readme"
}
41 changes: 41 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-vidinoti-push" version="0.1.0">
<name>Vidinoti Push</name>
<description>Cordova Plugin for Vidinoti V-Director push integration</description>
<license>MIT</license>
<keywords>cordova, augmented reality, vidinoti</keywords>

<platform name="android">
<resource-file src="src/android/google-services.json" target="."/>
<source-file src="src/android/VidinotiInstanceIDListenerService.java" target-dir="src/com/vidinoti/cordova" />
<source-file src="src/android/VidinotiFcmListenerService.java" target-dir="src/com/vidinoti/cordova" />

<config-file parent="/resources" target="res/values/strings.xml">
<string name="google_app_id">@string/google_app_id</string>
</config-file>
<config-file parent="/resources" target="res/values/strings.xml">
<string name="google_api_key">@string/google_api_key</string>
</config-file>

<config-file target="app/src/main/AndroidManifest.xml" parent="/manifest/application">
<service android:name="com.vidinoti.cordova.VidinotiFcmListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name="com.vidinoti.cordova.VidinotiInstanceIDListenerService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</config-file>

<framework src="src/android/build.gradle" custom="true" type="gradleReference" />
<framework src="com.google.gms:google-services:+" />
<framework src="com.google.firebase:firebase-core:+" />
<framework src="com.google.firebase:firebase-messaging:+" />
</platform>

<hook src="scripts/after_prepare.js" type="after_prepare" />

</plugin>
56 changes: 56 additions & 0 deletions scripts/after_prepare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env node

'use strict';

var fs = require('fs');

fs.ensureDirSync = function (dir) {
if (!fs.existsSync(dir)) {
dir.split(path.sep).reduce(function (currentPath, folder) {
currentPath += folder + path.sep;
if (!fs.existsSync(currentPath)) {
fs.mkdirSync(currentPath);
}
return currentPath;
}, '');
}
};

function fileExists(path) {
try {
return fs.statSync(path).isFile();
} catch (e) {
return false;
}
}

function updateStringsXml(contents) {
var json = JSON.parse(contents);
var stringsXml = fileExists('platforms/android/app/src/main/res/values/strings.xml') ? 'platforms/android/app/src/main/res/values/strings.xml' : 'platforms/android/res/values/strings.xml';
var strings = fs.readFileSync(stringsXml).toString();

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', 'i'), '');

// strip non-default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^\@<]+?)</string>', 'i'), '');

// strip empty lines
strings = strings.replace(new RegExp('(\r\n|\n|\r)[ \t]*(\r\n|\n|\r)', 'gm'), '$1');

// replace the default value
strings = strings.replace(new RegExp('<string name="google_app_id">([^<]+?)</string>', 'i'), '<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>');

// replace the default value
strings = strings.replace(new RegExp('<string name="google_api_key">([^<]+?)</string>', 'i'), '<string name="google_api_key">' + json.client[0].api_key[0].current_key + '</string>');

fs.writeFileSync(stringsXml, strings);
}

module.exports = function (context) {
// Copy the file google-services.json from the project folder to the android platform folder
var contents = fs.readFileSync('google-services.json').toString();
fs.ensureDirSync('platforms/android');
fs.writeFileSync('platforms/android/google-services.json', contents);
updateStringsXml(contents);
};
90 changes: 90 additions & 0 deletions src/android/VidinotiFcmListenerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.vidinoti.cordova;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.media.RingtoneManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

/**
* Service that receives the push notifications.
* When a new notification is received, it creates a new notification in the status panel.
* When the notification is opened, it opens the start activity and some information needs to
* be forwarded to the Vidinoti SDK. See the documentation at https://vidinoti.github.io/ for
* more information.
*/
public class VidinotiFcmListenerService extends FirebaseMessagingService {

private static final String TAG = VidinotiFcmListenerService.class.getName();

private static final String CHANNEL_ID = "vidinoti_push_channel";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
Log.w(TAG, "NotificationManager is null");
return;
}

String nid = data.get("nid");
String message = data.get("message");
if (nid == null || nid.length() == 0 || message == null || message.length() == 0) {
Log.w(TAG, "Invalid push data");
return;
}

// Get the default intent of the app
Intent appIntent = getPackageManager().getLaunchIntentForPackage(getPackageName());
if (appIntent == null) {
Log.w(TAG, "Launch intent not found");
return;
}
appIntent.putExtra("nid", nid);
appIntent.putExtra("remote", true);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);

ApplicationInfo ai = getApplicationInfo();
createNotificationChannel(notificationManager);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(ai.icon!=0 ? ai.icon : android.R.drawable.star_big_off)
.setContentTitle(getString(ai.labelRes))
.setContentText(message)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

//To make sure notification ID is unique over time
int when = (int) ((System.currentTimeMillis() - 1419120000000L) / 1000L);

notificationManager.notify(when, mBuilder.build());
}

/**
* Creates a channel if necessary (required from Android API 26)
*/
private void createNotificationChannel(NotificationManager notificationManager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "App";
String description = "Notification about latest news";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
notificationManager.createNotificationChannel(channel);
}
}
}
23 changes: 23 additions & 0 deletions src/android/VidinotiInstanceIDListenerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.vidinoti.cordova;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import com.vidinoti.android.vdarsdk.VDARSDKController;

/**
* Service required for the push notifications.
* It receives the update of the Firebase token that needs to be forwarded to the Vidinoti SDK.
*/
public class VidinotiInstanceIDListenerService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
// Forward the token to the Vidinoti SDK
VDARSDKController controller = VDARSDKController.getInstance();
if (controller != null) {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
controller.updatePushNotificationToken(refreshedToken);
}
}

}
1 change: 1 addition & 0 deletions src/android/build-extras.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apply plugin: 'com.google.gms.google-services'
8 changes: 8 additions & 0 deletions src/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.gms:google-services:3.1.0'
}
}
Empty file.

0 comments on commit 621d793

Please sign in to comment.