首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >世博会弹出通知(浮动通知)不能在android上使用。

世博会弹出通知(浮动通知)不能在android上使用。
EN

Stack Overflow用户
提问于 2022-07-25 08:26:54
回答 1查看 257关注 0票数 0

通知会出现,但只显示在状态栏中。

我使用“世博”:"~44.0.0“和”博览会-通知“:"~0.14.0”。

代码是从文档中完全复制的。

代码语言:javascript
运行
复制
import * as app from 'app.json';
import * as Device from 'expo-device';
import { Subscription } from 'expo-modules-core';
import * as Notifications from 'expo-notifications';
import React, { useCallback, useEffect, useRef, useState } from 'react';

import { ScreenView } from '@shared/components/screenView';


Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: false,
    shouldSetBadge: false,
  }),
});

export const Login: React.FC = () => {
  const notificationListener = useRef<unknown>();
  const responseListener = useRef<unknown>();

  const [token, setToken] = useState('');
  const [notification, setNotification] = useState<Notifications.Notification>();



  useEffect(() => {
    void registerForPushNotificationsAsync().then((token) => setToken(token ?? ''));
  }, []);


  useEffect(() => {
    notificationListener.current = Notifications.addNotificationReceivedListener((notification) => {
      setNotification(notification);
    });

    responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => {
      console.log(response);
    });

  return () => {
      Notifications.removeNotificationSubscription(notificationListener.current);
      Notifications.removeNotificationSubscription(responseListener.current);
    };
  }, []);



  return (
    <ScreenView alignItems="center">
    </ScreenView>
  );
};


async function registerForPushNotificationsAsync() {
  let token;

  if (Device.isDevice) {
    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;
    }
    token = (await Notifications.getExpoPushTokenAsync()).data;
  } else {
    alert('Must use physical device for Push Notifications');
  }

  if (Platform.OS === 'android') {
    await Notifications.setNotificationChannelAsync('default', {
      name: 'default',
      importance: Notifications.AndroidImportance.MAX,
    });
  }

  return token;
}

我还添加到app.json中。

代码语言:javascript
运行
复制
"useNextNotificationsApi": true

以前的“博览会”版本:"~40.0.0“和”博览会-通知“:"~0.8.2”,它起作用了。

状态栏

推送通知

EN

回答 1

Stack Overflow用户

发布于 2022-07-25 09:16:21

如果应用程序被打开,您需要实现应用程序中的通知。查看一下:https://docs.expo.dev/versions/latest/sdk/notifications/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73106105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档