feat: 优化通知权限申请
This commit is contained in:
parent
ad860171d6
commit
745667f554
@ -1,5 +1,6 @@
|
|||||||
import { HapticTab } from '@/components/HapticTab';
|
import { HapticTab } from '@/components/HapticTab';
|
||||||
import { TabBarIcon } from '@/components/navigation/TabBarIcon';
|
import { TabBarIcon } from '@/components/navigation/TabBarIcon';
|
||||||
|
import { requestNotificationPermission } from '@/components/owner/utils';
|
||||||
import TabBarBackground from '@/components/ui/TabBarBackground';
|
import TabBarBackground from '@/components/ui/TabBarBackground';
|
||||||
import { Colors } from '@/constants/Colors';
|
import { Colors } from '@/constants/Colors';
|
||||||
import { useColorScheme } from '@/hooks/useColorScheme';
|
import { useColorScheme } from '@/hooks/useColorScheme';
|
||||||
@ -27,9 +28,9 @@ export default function TabLayout() {
|
|||||||
const [token, setToken] = useState('');
|
const [token, setToken] = useState('');
|
||||||
const sendNotification = async (item: PollingData) => {
|
const sendNotification = async (item: PollingData) => {
|
||||||
// 请求通知权限
|
// 请求通知权限
|
||||||
const { status } = await Notifications.requestPermissionsAsync();
|
const granted = await requestNotificationPermission();
|
||||||
if (status !== 'granted') {
|
if (!granted) {
|
||||||
alert('请先允许通知权限');
|
console.log('用户拒绝了通知权限');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import * as Device from 'expo-device';
|
|||||||
import * as Notifications from 'expo-notifications';
|
import * as Notifications from 'expo-notifications';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Button, Platform, Text, View } from 'react-native';
|
import { Button, Platform, Text, View } from 'react-native';
|
||||||
|
import { requestNotificationPermission } from '../owner/utils';
|
||||||
|
|
||||||
Notifications.setNotificationHandler({
|
Notifications.setNotificationHandler({
|
||||||
handleNotification: async () => ({
|
handleNotification: async () => ({
|
||||||
@ -108,13 +109,13 @@ async function registerForPushNotificationsAsync() {
|
|||||||
|
|
||||||
// 4. 如果尚未授予权限,则请求权限
|
// 4. 如果尚未授予权限,则请求权限
|
||||||
if (existingStatus !== 'granted') {
|
if (existingStatus !== 'granted') {
|
||||||
const { status } = await Notifications.requestPermissionsAsync();
|
const granted = await requestNotificationPermission();
|
||||||
finalStatus = status;
|
finalStatus = granted ? Notifications.PermissionStatus.GRANTED : Notifications.PermissionStatus.DENIED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. 如果权限被拒绝,显示警告并返回
|
// 5. 如果权限被拒绝,显示警告并返回
|
||||||
if (finalStatus !== 'granted') {
|
if (finalStatus !== 'granted') {
|
||||||
alert('Failed to get push token for push notification!');
|
console.log('用户拒绝了通知权限');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user