【问题标题】:Notifee Custom Sound works in foreground, not in background in React NativeNotifee Custom Sound 在前台工作,而不是在 React Native 的后台工作
【发布时间】:2022-12-15 14:56:23
【问题描述】:

我正在尝试使用 React Native 构建一个应用程序,我需要在其中发送带有一些自定义通知声音的通知。 Notifee 在前台显示通知和声音,但不在后台播放声音。 这是我的 App.js 的样子

import React, {useEffect} from 'react';
import messaging from '@react-native-firebase/messaging';
import {
  SafeAreaView,
  Button,

} from 'react-native';
import notifee from '@notifee/react-native';
import {
  getFCMToken,
  requestUserPermission,
} from './src/NotificationHandler';
import {onDisplayNotification} from './src/BackgroundNotification';

const App = () => {
  useEffect(() => {
    requestUserPermission();
    getFCMToken();
  }, []);

  useEffect(() => {
    const unsubscribe = messaging().onMessage(async remoteMessage => {
      onDisplayNotification();
    });

    return unsubscribe;
  }, []);
  return (
    <SafeAreaView>
      <Button title="Display Notification" onPress={onDisplayNotification} />
    </SafeAreaView>
  );
};

export default App;

我还在 Index.js 中注册了 Firebase 后台服务,它看起来像

import {AppRegistry} from 'react-native';
import messaging from '@react-native-firebase/messaging';
import App from './App';
import {name as appName} from './app.json';
import {onDisplayNotification} from './src/BackgroundNotification';
import notifee, {EventType} from '@notifee/react-native';

messaging().setBackgroundMessageHandler(async remoteMessage => {
  console.log('Message handled in the background!', remoteMessage);
  onDisplayNotification();
});

AppRegistry.registerComponent(appName, () => App);

onDisplayNotification 是处理 Notifee 通知的负责函数,它看起来像

import notifee from '@notifee/react-native';

export async function onDisplayNotification() {
  console.log('ondisplaynotification');
  // Request permissions (required for iOS)
  await notifee.requestPermission();

  // Create a channel (required for Android)
  const channelId = await notifee.createChannel({
    id: 'default',
    name: 'Default Channel',
    sound: 'customsound',
    vibration: true,
    vibrationPattern: [300, 500],
  });
  console.log('sound playing');
  // Display a notification
  await notifee.displayNotification({
    title: 'Notification Title',
    body: 'Main body content of the notification',
    android: {
      sound: 'customsound',
      vibration: true,
      vibrationPattern: [300, 500],

      channelId,
      //   smallIcon: 'name-of-a-small-icon', // optional, defaults to 'ic_launcher'.
      // pressAction is needed if you want the notification to open the app when pressed
      pressAction: {
        id: 'default',
      },
    },
  });
}

有趣的是我正在让日志正常工作。我可以在日志 Sound Playing 中看到,但声音并不总是播放。有时它在后台播放声音,有时它不播放声音,但会出现通知。有什么方法可以在设备锁定或屏幕关闭时始终在后台播放自定义通知声音?

【问题讨论】:

    标签: reactjs firebase react-native notifications firebase-cloud-messaging


    【解决方案1】:

    你找到任何解决方案了吗?如果您找到任何解决方案,请分享。

    谢谢

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    相关资源
    最近更新 更多