反应本机expo fcm:如何notification.origin控制? (如果应用在前台打开,我不想显示推送)

时间:2019-11-19 02:31:57

标签: react-native expo react-native-firebase react-native-push-notification react-native-fcm

☆我的源代码信息☆

  1. 前端: 我阅读了博览会的官方文档,并弄清楚了移动设备的令牌。 https://docs.expo.io/versions/latest/guides/push-notifications/
   let token = await Notifications.getExpoPushTokenAsync();
   console.log(token);
  1. 后端:并且我将令牌插入了后端服务器的源代码中。
   List<String> somePushTokens = Arrays.asList("ExponentPushToken[yyyyyyyyyyyyyyyyyyyyyy])";  // example
  1. 如何运行:如果我在Eclipse中运行Java服务器,则警报会传到我的手机上。

enter image description here


☆我的问题☆

如果应用程序在前台打开,我不想显示推送。 因此,我阅读了世博会官方文件的第3项。 https://docs.expo.io/versions/latest/guides/push-notifications/

3. Handle receiving and/or selecting the notification
Determining origin of the notification

根据官方文档,如果“来源已打开”且“应用已打开”,则“原始”值将被“接收”。

如果“接收到”原始值,则我不想显示警报。 我可以根据来源值控制警报消息的接收吗?

enter image description here

我使用了此源代码。 当前,警报将无条件发送到电话。 它会无条件显示在手机上。

我希望控制在看着屏幕时不显示警报。

import React from 'react';
import {
  Notifications,
} from 'expo';
import {
  Text,
  View,
} from 'react-native';

// This refers to the function defined earlier in this guide
import registerForPushNotificationsAsync from './registerForPushNotificationsAsync';

export default class AppContainer extends React.Component {
  state = {
    notification: {},
  };

  componentDidMount() {
    registerForPushNotificationsAsync();

    // Handle notifications that are received or selected while the app
    // is open. If the app was closed and then opened by tapping the
    // notification (rather than just tapping the app icon to open it),
    // this function will fire on the next tick after the app starts
    // with the notification data.
    this._notificationSubscription = Notifications.addListener(this._handleNotification);
  }

  _handleNotification = (notification) => {
    this.setState({notification: notification});
  };

  render() {
    return (
      <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
        <Text>Origin: {this.state.notification.origin}</Text>
        <Text>Data: {JSON.stringify(this.state.notification.data)}</Text>
      </View>
    );
  }
}

所以我用了Notifications.dismissAllNotificationsAsync(); 但是,如果我将其放在源代码中,则在启动应用程序时,警报只会消失一次。

我根本无法停止闹钟。

  componentDidMount() {
    Notifications.dismissAllNotificationsAsync();
  }

0 个答案:

没有答案