ReactNative 极光推送,显示红点和桌面红点


import {
    AsyncStorage, FlatList, Image, StatusBar, TouchableOpacity, View, Dimensions,ToastAndroid,
    ActivityIndicator, Platform, ProgressBarAndroid, Text, PixelRatio,ImageBackground,BackHandler,
    NativeModules,Modal,PermissionsAndroid,
} from "react-native";
import React from "react";



export default class HomeScreen extends React.PureComponent {
componentDidMount() {
      
      if (Platform.OS === 'android') {
        JPushModule.initPush()
        JPushModule.getInfo(map => {
          this.setState({
            appkey: map.myAppKey,
            imei: map.myImei,
            package: map.myPackageName,
            deviceId: map.myDeviceId,
            version: map.myVersion
          })
        })
        JPushModule.notifyJSDidLoad(resultCode => {
          if (resultCode === 0) {
          }
        })
      } else {
        JPushModule.setupPush()
      }
  
      JPushModule.addReceiveCustomMsgListener(map => {
        this.setState({
          pushMsg: map.message
        })
        console.log('extras: ' + map.extras)
      })
  
      JPushModule.addReceiveNotificationListener(map => {
        console.log('alertContent: ' + map.alertContent)
        console.log('extras: ' + map.extras)
        // var extra = JSON.parse(map.extras);
        // console.log(extra.key + ": " + extra.value);
        AsyncStorage.setItem('receivedNotification','true');
        this.setState({receivedNotification:true});
        NativeModules.VersionModule.showBadgeCount(1);
      })
  
      JPushModule.addReceiveOpenNotificationListener(map => {
        console.log('Opening notification!')
        console.log('map.extra: ' + map.extras)
        console.log('map.alertContent: ' + map.alertContent)
        console.log('map.id', map.id)
        //this.jumpSecondActivity()
        // JPushModule.jumpToPushActivity("SecondActivity");
        //alert(JSON.stringify(map.extras));
        AsyncStorage.setItem('receivedNotification','false');
        this.setState({receivedNotification:false});
        NativeModules.VersionModule.removeBadgeCount();
        this._toMessageDetail(map,map.extras);
          
      })
  
      JPushModule.addGetRegistrationIdListener(registrationId => {
        console.log('Device register succeed, registrationId ' + registrationId)
      })

     
      this._findMessage();

    
      
    }

componentWillUnmount() {
        if (Platform.OS === 'android') {
          BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
        }
        JPushModule.removeReceiveCustomMsgListener(receiveCustomMsgEvent)
        JPushModule.removeReceiveNotificationListener(receiveNotificationEvent)
        JPushModule.removeReceiveOpenNotificationListener(openNotificationEvent)
        JPushModule.removeGetRegistrationIdListener(getRegistrationIdEvent)
        console.log('Will clear all notifications')
        JPushModule.clearAllNotifications()
    }
constructor(props) {
        super(props);
        this.state = {
            receivedNotification:false,
        }
    }

render() {
        return (
            
                
            

            
                               {this.state.receivedNotification?:null}
                  
                  {/*  */}

                  
                
                
)
}


async _findMessage(){
      AsyncStorage.setItem('receivedNotification','false');

      let value = await AsyncStorage.getItem('receivedNotification');
      //alert(value)
      if(value&&value==='true'){
       
        this.setState({receivedNotification:true});
//显示桌面红点
        NativeModules.VersionModule.showBadgeCount(1);
      }else{
        
        this.setState({receivedNotification:false});
//移除桌面红点
        NativeModules.VersionModule.removeBadgeCount();
    }

    }

async _handleMessage(){

      let value = await AsyncStorage.getItem('user');
      let user = JSON.parse(value);
      if(user===null||user===undefined){
        this.props.navigation.navigate('SignIn',{
          callback: (refresh) => {
            if(refresh){
              this._fetchData();
            }
          },
        })

      }else{
        AsyncStorage.setItem('receivedNotification','false');
        this.setState({receivedNotification:false});
        NativeModules.VersionModule.removeBadgeCount();
        this.props.navigation.navigate('MessageListScreen',{
          callback: (refresh) => {
            if(refresh){
              this._refreshProfile();
            }
          },
        });
      
    }

}

你可能感兴趣的:(ReactNative 极光推送,显示红点和桌面红点)