You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

react-native-notifications与@voximplant/react-native-foreground-service搭配使用时无法显示通知的问题求助

react-native-notifications与@voximplant/react-native-foreground-service搭配使用时无法显示通知的问题求助

大家好,我最近在React Native项目里碰到了个棘手的问题:我用@voximplant/react-native-foreground-service来维持WebSocket的后台连接,同时想用react-native-notifications把WebSocket收到的数据展示成本地通知。

目前WebSocket是完全正常的——控制台能清晰打印出收到的消息数据,但react-native-notifications就是不弹出通知,而且全程没有任何报错信息,我实在摸不着头绪。

下面是我的相关代码,麻烦各位帮忙看看哪里可能出问题了:

import { Notifications } from "react-native-notifications";
import VIForegroundService from "@voximplant/react-native-foreground-service";
import config from "./config";

export default async function Notif(){

  await VIForegroundService.getInstance().createNotificationChannel({
    id:"id",
    name:"app_name",
    description:"service",
    enableVibration:false
  });

  try {
    await VIForegroundService.getInstance().startService({
      channelId:"id",
      id:1,
      title:"app_title",
      text:"Listening...",
      icon:"ic_icon",
      button:"Ok"
    });
  } catch (e) {
    console.error(e);
  }

  const ws = new WebSocket(config.ws_url);

  ws.onopen = () => setInterval(() => ws.send("ping"), 60 * 1000);

  ws.onmessage = (e) => {
    if(e.data !== "pong"){
      var data = JSON.parse(e.data);
      console.log(data);
      Notifications.postLocalNotification({
        title:"app_name",
        body:`Received report type ${data.type === 1 ? "Post" : "Comment"} from ${data.from}`
      });
    }
  }
}

有没有朋友遇到过类似的情况?或者能帮我排查下可能的原因?比如是不是通知权限没配置好、通道设置有问题,还是两个库之间存在什么冲突?

备注:内容来源于stack exchange,提问作者Alvin Dimas

火山引擎 最新活动