mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Improve push experience
This commit is contained in:
11
src/utils/slices/instances/push/androidDefaults.ts
Normal file
11
src/utils/slices/instances/push/androidDefaults.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import * as Notifications from 'expo-notifications'
|
||||
|
||||
const androidDefaults = {
|
||||
importance: Notifications.AndroidImportance.DEFAULT,
|
||||
bypassDnd: false,
|
||||
showBadge: true,
|
||||
enableLights: true,
|
||||
enableVibrate: true
|
||||
}
|
||||
|
||||
export default androidDefaults
|
@ -1,5 +1,6 @@
|
||||
import apiGeneral from '@api/general'
|
||||
import apiInstance from '@api/instance'
|
||||
import i18n from '@root/i18n/i18n'
|
||||
import { RootState } from '@root/store'
|
||||
import {
|
||||
getInstance,
|
||||
@ -8,6 +9,7 @@ import {
|
||||
} from '@utils/slices/instancesSlice'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { Platform } from 'react-native'
|
||||
import androidDefaults from './androidDefaults'
|
||||
|
||||
const register1 = async ({
|
||||
expoToken,
|
||||
@ -66,17 +68,6 @@ const pushRegister = async (
|
||||
return Promise.reject()
|
||||
}
|
||||
|
||||
const { status: existingStatus } = await Notifications.getPermissionsAsync()
|
||||
let finalStatus = existingStatus
|
||||
if (existingStatus !== 'granted') {
|
||||
const { status } = await Notifications.requestPermissionsAsync()
|
||||
finalStatus = status
|
||||
}
|
||||
if (finalStatus !== 'granted') {
|
||||
alert('Failed to get push token for push notification!')
|
||||
return Promise.reject()
|
||||
}
|
||||
|
||||
const accountId = instanceAccount.id
|
||||
const accountFull = `@${instanceAccount.acct}@${instanceUri}`
|
||||
const serverRes = await register1({
|
||||
@ -111,25 +102,45 @@ const pushRegister = async (
|
||||
})
|
||||
|
||||
if (Platform.OS === 'android') {
|
||||
Notifications.setNotificationChannelAsync('follow', {
|
||||
name: 'Follow',
|
||||
importance: Notifications.AndroidImportance.DEFAULT
|
||||
})
|
||||
Notifications.setNotificationChannelAsync('favourite', {
|
||||
name: 'Favourite',
|
||||
importance: Notifications.AndroidImportance.DEFAULT
|
||||
})
|
||||
Notifications.setNotificationChannelAsync('reblog', {
|
||||
name: 'Reblog',
|
||||
importance: Notifications.AndroidImportance.DEFAULT
|
||||
})
|
||||
Notifications.setNotificationChannelAsync('mention', {
|
||||
name: 'Mention',
|
||||
importance: Notifications.AndroidImportance.DEFAULT
|
||||
})
|
||||
Notifications.setNotificationChannelAsync('poll', {
|
||||
name: 'Poll',
|
||||
importance: Notifications.AndroidImportance.DEFAULT
|
||||
Notifications.setNotificationChannelGroupAsync(accountFull, {
|
||||
name: accountFull,
|
||||
...androidDefaults
|
||||
}).then(group => {
|
||||
if (group) {
|
||||
if (instancePush.decode.value === false) {
|
||||
Notifications.setNotificationChannelAsync(`${group.id}_default`, {
|
||||
groupId: group.id,
|
||||
name: i18n.t('meSettingsPush:content.default.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
} else {
|
||||
Notifications.setNotificationChannelAsync(`${group.id}_follow`, {
|
||||
groupId: group.id,
|
||||
name: i18n.t('meSettingsPush:content.follow.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
Notifications.setNotificationChannelAsync(`${group.id}_favourite`, {
|
||||
groupId: group.id,
|
||||
name: i18n.t('meSettingsPush:content.favourite.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
Notifications.setNotificationChannelAsync(`${group.id}_reblog`, {
|
||||
groupId: group.id,
|
||||
name: i18n.t('meSettingsPush:content.reblog.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
Notifications.setNotificationChannelAsync(`${group.id}_mention`, {
|
||||
groupId: group.id,
|
||||
name: i18n.t('meSettingsPush:content.mention.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
Notifications.setNotificationChannelAsync(`${group.id}_poll`, {
|
||||
groupId: group.id,
|
||||
name: i18n.t('meSettingsPush:content.poll.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -2,9 +2,13 @@ import apiGeneral from '@api/general'
|
||||
import apiInstance from '@api/instance'
|
||||
import { RootState } from '@root/store'
|
||||
import { getInstance, PUSH_SERVER } from '@utils/slices/instancesSlice'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { Platform } from 'react-native'
|
||||
|
||||
const pushUnregister = async (state: RootState, expoToken: string) => {
|
||||
const instance = getInstance(state)
|
||||
const instanceUri = instance?.uri
|
||||
const instanceAccount = instance?.account
|
||||
|
||||
if (!instance?.url || !instance.account.id) {
|
||||
return Promise.reject()
|
||||
@ -26,6 +30,11 @@ const pushUnregister = async (state: RootState, expoToken: string) => {
|
||||
}
|
||||
})
|
||||
|
||||
if (Platform.OS === 'android') {
|
||||
const accountFull = `@${instanceAccount?.acct}@${instanceUri}`
|
||||
Notifications.deleteNotificationChannelGroupAsync(accountFull)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,16 @@
|
||||
import apiGeneral from '@api/general'
|
||||
import { createAsyncThunk } from '@reduxjs/toolkit'
|
||||
import i18n from '@root/i18n/i18n'
|
||||
import { RootState } from '@root/store'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { Platform } from 'react-native'
|
||||
import { getInstance, Instance, PUSH_SERVER } from '../instancesSlice'
|
||||
import androidDefaults from './push/androidDefaults'
|
||||
|
||||
export const updateInstancePushDecode = createAsyncThunk(
|
||||
'instances/updatePushDecode',
|
||||
async (
|
||||
disalbe: boolean,
|
||||
disable: boolean,
|
||||
{ getState }
|
||||
): Promise<Instance['push']['decode']['value']> => {
|
||||
const state = getState() as RootState
|
||||
@ -30,10 +33,61 @@ export const updateInstancePushDecode = createAsyncThunk(
|
||||
expoToken,
|
||||
instanceUrl: instance.url,
|
||||
accountId: instance.account.id,
|
||||
...(disalbe && { keys: instance.push.keys })
|
||||
...(disable && { keys: instance.push.keys })
|
||||
}
|
||||
})
|
||||
|
||||
return Promise.resolve(disalbe)
|
||||
if (Platform.OS === 'android') {
|
||||
const accountFull = `@${instance.account.acct}@${instance.uri}`
|
||||
switch (disable) {
|
||||
case true:
|
||||
Notifications.deleteNotificationChannelAsync(`${accountFull}_default`)
|
||||
Notifications.setNotificationChannelAsync(`${accountFull}_follow`, {
|
||||
groupId: accountFull,
|
||||
name: i18n.t('meSettingsPush:content.follow.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
Notifications.setNotificationChannelAsync(
|
||||
`${accountFull}_favourite`,
|
||||
{
|
||||
groupId: accountFull,
|
||||
name: i18n.t('meSettingsPush:content.favourite.heading'),
|
||||
...androidDefaults
|
||||
}
|
||||
)
|
||||
Notifications.setNotificationChannelAsync(`${accountFull}_reblog`, {
|
||||
groupId: accountFull,
|
||||
name: i18n.t('meSettingsPush:content.reblog.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
Notifications.setNotificationChannelAsync(`${accountFull}_mention`, {
|
||||
groupId: accountFull,
|
||||
name: i18n.t('meSettingsPush:content.mention.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
Notifications.setNotificationChannelAsync(`${accountFull}_poll`, {
|
||||
groupId: accountFull,
|
||||
name: i18n.t('meSettingsPush:content.poll.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
break
|
||||
case false:
|
||||
Notifications.setNotificationChannelAsync(`${accountFull}_default`, {
|
||||
groupId: accountFull,
|
||||
name: i18n.t('meSettingsPush:content.default.heading'),
|
||||
...androidDefaults
|
||||
})
|
||||
Notifications.deleteNotificationChannelAsync(`${accountFull}_follow`)
|
||||
Notifications.deleteNotificationChannelAsync(
|
||||
`${accountFull}_favourite`
|
||||
)
|
||||
Notifications.deleteNotificationChannelAsync(`${accountFull}_reblog`)
|
||||
Notifications.deleteNotificationChannelAsync(`${accountFull}_mention`)
|
||||
Notifications.deleteNotificationChannelAsync(`${accountFull}_poll`)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.resolve(disable)
|
||||
}
|
||||
)
|
||||
|
@ -4,7 +4,6 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||
import { RootState } from '@root/store'
|
||||
import { ComposeStateDraft } from '@screens/Compose/utils/types'
|
||||
import { findIndex } from 'lodash'
|
||||
import { Appearance } from 'react-native'
|
||||
import addInstance from './instances/add'
|
||||
import { connectInstancesPush } from './instances/connectPush'
|
||||
import removeInstance from './instances/remove'
|
||||
|
@ -1,8 +1,12 @@
|
||||
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||
import { RootState } from '@root/store'
|
||||
import i18n from '@root/i18n/i18n'
|
||||
import { RootState, store } from '@root/store'
|
||||
import * as Analytics from 'expo-firebase-analytics'
|
||||
import * as Localization from 'expo-localization'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { pickBy } from 'lodash'
|
||||
import androidDefaults from './instances/push/androidDefaults'
|
||||
import { getInstances } from './instancesSlice'
|
||||
|
||||
enum availableLanguages {
|
||||
'zh-Hans',
|
||||
|
Reference in New Issue
Block a user