为什么在React Native中收到来自Firebase的推送通知时应用崩溃?

时间:2018-10-01 10:14:47

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

我正在使用react-native-firebase进行远程推送通知。当应用打开或在后台运行时,我能够收到通知。但是,当我取消该应用并将其从内存中删除时,一旦我从Firestore应用发送消息,就会在移动设备中崩溃。我遵循了完整的文档,并根据文档隐含了这些内容。应用程序崩溃的原因是什么?如何解决?

我的App.js

module subALU(A, B, func, ALUout);
input [3:0] A;
input [3:0] B;
input [2:0] func;
output reg[7:0] ALUout;

//A+1
wire [3:0] addA1;
wire addA2;
rippleCarryAdder add1(.A(A), .B(4'b0001), .cin(0), .s(addA1), .cout(addA2));

//A+B
wire [3:0] ab1;
wire ab2;
rippleCarryAdder add2(.A(A), .B(B), .cin(0), .s(ab1), .cout(ab2));

//A+B using Verilog
wire [3:0] abv;
wire abvo;
fourBitAdd add3(.X(A), .Y(B), .C(abv), .overflow(abvo));

always @(∗)
begin
    case (func)
        //A + 1
        3'b000: ALUout[7:0] = {3'b000, addA2, addA1};
        //A + B (Using rippleCarryAdder)
        3'b001: ALUout = {3'b000, ab2, ab1};
        //A + B (Using Verilog arithmetic)
        3'b010: ALUout = {3'b000, abvo, abv};
        //A XOR B in lower 4 bits, A OR B in higher 4
        3'b011: ALUout = {A | B, A ^ B};
        //A and B reduction OR
        3'b100: ALUout = {7'b0000000, |(A|B)};
        //A in leftmost 4 bits, B in rightmost 4 bits 
        3'b101: ALUout = {A, B};
        //Display 0
        default: ALUout = 8'b00000000
    endcase
end

AndroidManifest.xml

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import firebase from 'react-native-firebase';
import type { Notification, NotificationOpen } from 'react-native-firebase';
const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});
export default class App extends Component {
    async componentDidMount() {
        const notificationOpen: NotificationOpen = await firebase.notifications().getInitialNotification();
        if (notificationOpen) {
            const action = notificationOpen.action;
            const notification: Notification = notificationOpen.notification;
            var seen = [];
            alert(JSON.stringify(notification.data, function(key, val) {
                if (val != null && typeof val == "object") {
                    if (seen.indexOf(val) >= 0) {
                        return;
                    }
                    seen.push(val);
                }
                return val;
            }));
        } 
        const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max)
                .setDescription('My apps test channel');
// Create the channel
        firebase.notifications().android.createChannel(channel);
        this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
            // Process your notification as required
            // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
        });
        this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
            // Process your notification as required
            notification
                .android.setChannelId('test-channel')
                .android.setSmallIcon('ic_launcher');
            firebase.notifications()
                .displayNotification(notification);
            
        });
        this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
            // Get the action triggered by the notification being opened
            const action = notificationOpen.action;
            // Get information about the notification that was opened
            const notification: Notification = notificationOpen.notification;
            var seen = [];
            alert(JSON.stringify(notification.data, function(key, val) {
                if (val != null && typeof val == "object") {
                    if (seen.indexOf(val) >= 0) {
                        return;
                    }
                    seen.push(val);
                }
                return val;
            }));
            firebase.notifications().removeDeliveredNotification(notification.notificationId);
            
        });
    }
    componentWillUnmount() {
      console.log("unmount called");
        this.notificationDisplayedListener();
        this.notificationListener();
        this.notificationOpenedListener();
    }

    componentWillMount(){
      if (!firebase.apps.length) {
        firebase.initializeApp({
          clientId: 'my client id-',
          appId: 'my app id',
          apiKey: "my api key",
          authDomain: "fcm-auth domain
          databaseURL: "database url
          projectId: "fcm-app-id,
          storageBucket: "fcm-app-BUCKET
          messagingSenderId: "sender id
        });
      }
      firebase.messaging().getToken()
        .then(fcmToken => {
          if (fcmToken) {
          console.log(fcmToken)
          } else {
            console.log("no token")
          } 
      });

      firebase.messaging().hasPermission()
        .then(enabled => {
        if (enabled) {
          // user has permissions
          console.log("user has permission")
        } else {
          // user doesn't have permission
          console.log("user don't has permission")
        } 
  });
    }
render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <Text style={styles.instructions}>{instructions}</Text>
      </View>
    );
  }
}
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,
  },
});

app / build.gradle

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <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" android:launchMode="singleTop">
        <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" />
        <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
        <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />
    </application>

</manifest>

0 个答案:

没有答案