ReactNative:使用react-native-push-notification库接收通知时,我的应用程序崩溃

时间:2018-12-10 10:04:50

标签: react-native

我正在使用react-native-push-notification库进行远程通知。 以我为例,当接收到远程通知时,App崩溃(Android版本8.1)。

它运行较低的android版本,但在android 7以上的版本中却不起作用

这是我在应用程序中尝试过的

 componentDidMount() {
PushNotification.configure({
    onRegister: async function(token) {
        console.log("DEVICE TOKEN:", token);
        await AsyncStorage.setItem("Token", JSON.stringify(token))
    },


    onNotification: function (notification) {
        console.log("NOTIFICATION:", notification);
        Storage.retrieve(STORAGE_KEY.NOTIFICATION, this.retrieveNotListCallback,)

    },


    permissions: {
        alert: true,
        badge: true,
        sound: true
    },


    popInitialNotification: true,


    requestPermissions: true
});

}

请任何人帮助我,这段代码我错了

这是我的代码

build.gradle

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        classpath 'com.google.gms:google-services:+'
    }
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            url "$rootDir/../node_modules/react-native/android"
        }
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        
    }
}

app / build.gradlew

dependencies {
    compile project(':react-native-android-location-services-dialog-box')
    compile project(':react-native-linear-gradient')
    compile project(':react-native-vector-icons')
    compile project(':react-native-image-picker')
    compile project(':react-native-fs')
    compile project(':react-native-maps')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile ("com.facebook.react:react-native:+") { force = true }
    compile project(':react-native-push-notification') 
    compile(project(':react-native-maps')){
    exclude group: 'com.google.android.gms', module: 'play-services-base'
    exclude group: 'com.google.android.gms', module: 'play-services-maps'
  }
  implementation('com.google.android.gms:play-services-base:+') {
      force = true
  }
  implementation('com.google.android.gms:play-services-maps:+') {
      force = true
  }
  compile("com.google.firebase:firebase-messaging:+") {
    force = true
}
compile("com.google.firebase:firebase-core:+") {
    force = true
}
}
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply plugin: 'com.google.gms.google-services'

AndroidMainfest

<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_EXTERNAL_STORAGE"/>
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission
        android:name="${applicationId}.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="27" />
    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <meta-data
     android:name="com.google.android.geo.API_KEY"
     android:value="AIzaSyCLVVs5iMqf4tnOLIADBkNka9y_hbNxquY"/>
      
        
        <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="${applicationId}" />
            </intent-filter>
        </receiver>

         <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <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>

1 个答案:

答案 0 :(得分:0)

必须从Android 8.1清单中缺少元数据,以添加通知通道。
来自Android documentation

  

如果您以Android 8.0(API级别26)为目标并且在未指定通知渠道的情况下发布通知,则该通知不会出现,并且系统会记录错误。   

来自 react-native-push-notification 库:分别对清单文件进行更改。例如

<meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
            android:value="YOUR NOTIFICATION CHANNEL NAME"/>
    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                android:value="YOUR NOTIFICATION CHANNEL DESCRIPTION"/>

查看库文档以获取更多详细信息。