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

loading...


0

我使用react-native-push-notification库进行远程通知 . 在我的情况下App崩溃(Android版本8.1)时收到远程通知 .

它运行较低的Android版本,但在Android版本超过7它没有工作

这是我在app中尝试过的

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'
        }
        
    }
}

应用程序/ 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>

loading...

1回答

  • 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"/>
    

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

评论

暂时没有评论!