推送通知在React Native中不起作用

时间:2019-01-29 08:00:57

标签: reactjs react-native

我正在使用https://github.com/zo0r/react-native-push-notification。完成所有设置后,我的应用程序开始运行,但未显示通知。这是我的AndroidManifest.xml文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.movit_driver1">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission
    android:name="com.movit_driver1.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
    <uses-permission android:name="com.movit_driver1.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
      <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyAbbD4N_pPzGPSPY_PyLy"/>
       <receiver
      android:name="com.google.android.gms.gcm.GcmReceiver"
      android:exported="true"
      android:permission="com.google.android.c2dm.permission.SEND" >
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="com.movit_driver1" />
      </intent-filter>
    </receiver>

    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
    <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
    <service
      android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
      android:exported="false" >
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      </intent-filter>
    </service>
    </application>

</manifest>

这是PushNotificationController.js文件:

import React, { Component } from 'react';
import PushNotification from 'react-native-push-notification';

export default class PushNotificationController extends Component {
  componentDidMount() {
    PushNotification.configure({
      onNotification: function(notification) {
        console.log( 'NOTIFICATION:', notification );
      },
    });
  }

  render() {
    return null;
  }
}

最后是我的主屏幕:

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Picker, AppState } from 'react-native';
import PushNotificationController from './PushNotification/PushNotificationController'
import PushNotification from 'react-native-push-notification';
const styles = StyleSheet.create({
    container: {
      flex: 1,
      justifyContent: 'center',
      alignItems: 'center',
      backgroundColor: '#F5FCFF',
    },
    welcome: {
      fontSize: 20,
      textAlign: 'center',
      margin: 10,
    },
    instructions: {
      textAlign: 'center',
      color: '#333333',
      marginBottom: 5,
    },
    picker: {
      width: 100,
    },
  });

export default class HomeScreen extends Component {
    constructor(props) {
        super(props);
    
        this.handleAppStateChange = this.handleAppStateChange.bind(this);
        this.state = {
          seconds: 5,
        };
      }
    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.welcome}>
                Choose your notification time in seconds.
                </Text>
                <Picker
                style={styles.picker}
                selectedValue={this.state.seconds}
                onValueChange={(seconds) => this.setState({ seconds })}
                >
                <Picker.Item label="5" value={5} />
                <Picker.Item label="10" value={10} />
                <Picker.Item label="15" value={15} />
                </Picker>
                <PushNotificationController/>
            </View>
        );
    }
    componentDidMount() {
        AppState.addEventListener('change', this.handleAppStateChange);
      }
    
    componentWillUnmount() {
    AppState.removeEventListener('change', this.handleAppStateChange);
    }
    
    handleAppStateChange(appState) {
            if (appState === 'background') {
                let date = new Date(Date.now() + (this.state.seconds * 1000));
          
                if (Platform.OS === 'ios') {
                  date = date.toISOString();
                }
          
                PushNotification.localNotificationSchedule({
                  message: "My Notification Message",
                  date,
                  soundName: "rush"
                });
        }
    }
}

其他信息:

  • 本机版本:“ 0.57.5”
  • 平台操作系统:“ android”

该应用会生成,但不会显示任何通知。我在这里想念什么?任何帮助将不胜感激。谢谢。

0 个答案:

没有答案