Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fabo/mobile push notifications #3209

Closed
wants to merge 9 commits into from
Closed
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ $ npx cap sync android
$ npx cap open android
```

#### Known issues

If you see something like "capacitor-cordova-android-plugins" not found. Follow this: https://github.com/ionic-team/capacitor/issues/349#issuecomment-550118242

#### iOS

Dependencies:
Expand Down
2 changes: 2 additions & 0 deletions android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/build/*
!/build/.npmkeep

google-services.json
1 change: 0 additions & 1 deletion changes/colw_fix-withdrawal-empty-message

This file was deleted.

1 change: 1 addition & 0 deletions changes/fabo_mobile-push-notifications
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Added] [#3203](https://github.com/cosmos/lunie/issues/3203) Adds push notification registration for mobile apps @faboweb
1 change: 0 additions & 1 deletion changes/fabo_terra

This file was deleted.

7 changes: 6 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ new Vue({
...App,
store,
apolloProvider,
mounted() {
async mounted() {
if (config.mobileApp) {
SplashScreen.hide()
StatusBar.show()

const { registerPushNotifications } = await import(
"scripts/push-notifications"
)
registerPushNotifications()
}
}
}).$mount("#app")
20 changes: 20 additions & 0 deletions src/scripts/push-notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Plugins } from "@capacitor/core"
import * as Sentry from "@sentry/browser"
const { PushNotifications } = Plugins

export const registerMobilePushNotifications = store => {
PushNotifications.register()

PushNotifications.addListener("registration", ({ value: token }) => {
store.commit("setPushIdToken", token)
})

PushNotifications.addListener("registrationError", error => {
Sentry.captureException(error)
})

// TODO if we want to react to notifications while the app is running enter the logic here
// PushNotifications.addListener("pushNotificationReceived", notification => {
// alert("Push received: " + JSON.stringify(notification))
// })
}
3 changes: 2 additions & 1 deletion src/vuex/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default opts => ({
ledger: require(`./ledger.js`).default(opts),
extension: require(`./extension.js`).default(opts),
signup: require(`./signup.js`).default(opts),
recover: require(`./recover.js`).default(opts)
recover: require(`./recover.js`).default(opts),
pushNotifications: require(`./push-notifications`).default(opts)
})
76 changes: 76 additions & 0 deletions src/vuex/modules/push-notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import config from "src/../config"

export default () => {
const emptyState = {
pushIdToken: undefined,
pendingRegistration: undefined
}
const state = {
...emptyState
}
const mutations = {
setPushIdToken(state, pushIdToken) {
state.pushIdToken = pushIdToken
},

setPendingPushNotificationRegistration(state, addresses) {
state.pendingRegistration = addresses
}
}

const actions = {
registerPushIdToken({ commit, dispatch }, pushIdToken) {
commit("setPushIdToken", pushIdToken)

if (state.pendingRegistration) {
dispatch("registerPushNotifications", state.pendingRegistration)
commit("setPendingPushNotificationRegistration", undefined)
}
},
// register this device with out API so it will receive notifications
async registerPushNotifications(
{ state: { pushIdToken }, commit },
addresses
) {
// if we have no push Id token yet, we wait until we have one
if (!pushIdToken) {
commit("setPendingPushNotificationRegistration", addresses)
return
}
fetch(`${config.graphqlHost}/push/default`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
pushId: pushIdToken,
addresses // [{address, networkId}]
})
}).then(() =>
console.log("Successfully registered for push notifications")
)
},
// register this device with out API so it will receive notifications
async unregisterPushNotifications({ state: { pushIdToken } }, addresses) {
fetch(`${config.graphqlHost}/push/unregister`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
pushId: pushIdToken,
addresses // [{address, networkId}] // currently not used as the whole id token will be unregistered
})
}).then(() =>
console.log("Successfully unregistered for push notifications")
)
}
}
return {
state,
mutations,
actions
}
}
9 changes: 9 additions & 0 deletions src/vuex/modules/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,22 @@ export default ({ apollo }) => {
addresses
})

if (state.mobile) {
dispatch(`registerPushNotifications`, [address])
}

state.externals.track(`event`, `session`, `sign-in`, sessionType)
},
signOut({ state, commit, dispatch }) {
const currentAddress = state.address
state.externals.track(`event`, `session`, `sign-out`)

dispatch(`resetSessionData`)
commit(`setSignIn`, false)

if (state.mobile) {
dispatch(`unregisterPushNotifications`, [currentAddress])
}
},
resetSessionData({ commit, state }) {
state.history = ["/"]
Expand Down