Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Neto#push notifications #139

Merged
merged 12 commits into from
May 28, 2019
Merged
5 changes: 5 additions & 0 deletions .firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "unichat-35f13"
}
}
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ dependencies {
implementation "com.google.firebase:firebase-firestore:18.2.0"
implementation "com.google.firebase:firebase-storage:16.1.0"
implementation "com.google.firebase:firebase-database:16.1.0"
implementation "com.google.firebase:firebase-messaging:17.5.0"
}

// Run this once to be able to run the application with BUCK
Expand Down
10 changes: 9 additions & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
android:name=".MainApplication"
Expand All @@ -16,6 +18,12 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
<service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
Expand All @@ -26,7 +34,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<activity android:launchMode="singleTop" android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>

</manifest>
24 changes: 7 additions & 17 deletions android/app/src/main/java/com/unichat/MainApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.invertase.firebase.firestore.RNFirebaseFirestorePackage;
import io.invertase.firebase.storage.RNFirebaseStoragePackage;
import io.invertase.firebase.database.RNFirebaseDatabasePackage;
import io.invertase.firebase.messaging.RNFirebaseMessagingPackage;
import io.invertase.firebase.notifications.RNFirebaseNotificationsPackage;

import java.util.Arrays;
import java.util.List;
Expand All @@ -35,23 +37,11 @@ public boolean getUseDeveloperSupport() {

@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new NetInfoPackage(),
new AsyncStoragePackage(),
new ReactNativeContacts(),
new ImagePickerPackage(),
new RNTextInputMaskPackage(),
new LinearGradientPackage(),
new VectorIconsPackage(),
new RNFirebasePackage(),
new RNFirebaseAuthPackage(),
new RNFirebaseFirestorePackage(),
new SvgPackage(),
new RNGestureHandlerPackage(),
new RNFirebaseStoragePackage(),
new RNFirebaseDatabasePackage()
);
return Arrays.<ReactPackage>asList(new MainReactPackage(), new NetInfoPackage(), new AsyncStoragePackage(),
new ReactNativeContacts(), new ImagePickerPackage(), new RNTextInputMaskPackage(),
new LinearGradientPackage(), new VectorIconsPackage(), new RNFirebasePackage(), new RNFirebaseAuthPackage(),
new RNFirebaseFirestorePackage(), new SvgPackage(), new RNGestureHandlerPackage(),
new RNFirebaseStoragePackage(), new RNFirebaseDatabasePackage(), new RNFirebaseMessagingPackage(), new RNFirebaseNotificationsPackage());
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
],
"source": "functions"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
}
}
4 changes: 4 additions & 0 deletions firestore.indexes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"indexes": [],
"fieldOverrides": []
}
7 changes: 7 additions & 0 deletions firestore.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
123 changes: 123 additions & 0 deletions functions/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"parserOptions": {
// Required for certain syntax usages
"ecmaVersion": 2017
},
"plugins": [
"promise"
],
"extends": "eslint:recommended",
"rules": {
// Removed rule "disallow the use of console" from recommended eslint rules
"no-console": "off",

// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
"no-regex-spaces": "off",

// Removed rule "disallow the use of debugger" from recommended eslint rules
"no-debugger": "off",

// Removed rule "disallow unused variables" from recommended eslint rules
"no-unused-vars": "off",

// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
"no-mixed-spaces-and-tabs": "off",

// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
"no-undef": "off",

// Warn against template literal placeholder syntax in regular strings
"no-template-curly-in-string": 1,

// Warn if return statements do not either always or never specify values
"consistent-return": 1,

// Warn if no return statements in callbacks of array methods
"array-callback-return": 1,

// Require the use of === and !==
"eqeqeq": 2,

// Disallow the use of alert, confirm, and prompt
"no-alert": 2,

// Disallow the use of arguments.caller or arguments.callee
"no-caller": 2,

// Disallow null comparisons without type-checking operators
"no-eq-null": 2,

// Disallow the use of eval()
"no-eval": 2,

// Warn against extending native types
"no-extend-native": 1,

// Warn against unnecessary calls to .bind()
"no-extra-bind": 1,

// Warn against unnecessary labels
"no-extra-label": 1,

// Disallow leading or trailing decimal points in numeric literals
"no-floating-decimal": 2,

// Warn against shorthand type conversions
"no-implicit-coercion": 1,

// Warn against function declarations and expressions inside loop statements
"no-loop-func": 1,

// Disallow new operators with the Function object
"no-new-func": 2,

// Warn against new operators with the String, Number, and Boolean objects
"no-new-wrappers": 1,

// Disallow throwing literals as exceptions
"no-throw-literal": 2,

// Require using Error objects as Promise rejection reasons
"prefer-promise-reject-errors": 2,

// Enforce “for” loop update clause moving the counter in the right direction
"for-direction": 2,

// Enforce return statements in getters
"getter-return": 2,

// Disallow await inside of loops
"no-await-in-loop": 2,

// Disallow comparing against -0
"no-compare-neg-zero": 2,

// Warn against catch clause parameters from shadowing variables in the outer scope
"no-catch-shadow": 1,

// Disallow identifiers from shadowing restricted names
"no-shadow-restricted-names": 2,

// Enforce return statements in callbacks of array methods
"callback-return": 2,

// Require error handling in callbacks
"handle-callback-err": 2,

// Warn against string concatenation with __dirname and __filename
"no-path-concat": 1,

// Prefer using arrow functions for callbacks
"prefer-arrow-callback": 1,

// Return inside each then() to create readable and reusable Promise chains.
// Forces developers to return console logs and http calls in promises.
"promise/always-return": 2,

//Enforces the use of catch() on un-returned promises
"promise/catch-or-return": 2,

// Warn against nested then() or catch() statements
"promise/no-nesting": 1
}
}
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
52 changes: 52 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable */
const functions = require("firebase-functions")
const admin = require("firebase-admin")

admin.initializeApp(functions.config().firebase)

return (exports.sendPushNotification = functions.firestore
.document("users/{userId}/conversas/{conversaId}/messages/{messageId}")
// eslint-disable-next-line consistent-return
.onCreate(async (snap, context) => {
const { userId, conversaId } = context.params
const { contentTranslated, content, source } = snap.data()

const data = await admin
.firestore()
.collection("users")
.doc(userId)
.collection("conversas")
.doc(conversaId)
.get()
.then(doc => {
return doc.data()
})
.catch(error => console.log(error))

const { contactName, contactPhoto } = data
if (source === "2") {
const payload = {
data: {
conversaId
},
notification: {
title: contactName,
body: contentTranslated,
sound: "default",
android_channel_id: "main-channel",
collapseKey: "unichat",
group: "unichat"
}
}

return admin
.firestore()
.collection("users")
.doc(userId)
.get()
.then(doc => {
const { pushToken } = doc.data()
return admin.messaging().sendToDevice(pushToken, payload)
})
}
}))
Loading