我使用react-native-push-notification localNotification方法。但是在android和ios上都没有发生任何事情。我链接库。 在android logcat输出中,我看到以下输出:
11-25 18:46:38.373 618 735 E RNPushNotification: failed to send push notification
11-25 18:46:38.373 618 735 E RNPushNotification: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Intent.getComponent()' on a null object reference
11-25 18:46:38.373 618 735 E RNPushNotification: at com.dieam.reactnativepushnotification.modules.RNPushNotificationHelper.getMainActivityClass(RNPushNotificationHelper.java:56)
11-25 18:46:38.373 618 735 E RNPushNotification: at com.dieam.reactnativepushnotification.modules.RNPushNotificationHelper.sendToNotificationCentre(RNPushNotificationHelper.java:137)
11-25 18:46:38.373 618 735 E RNPushNotification: at com.dieam.reactnativepushnotification.modules.RNPushNotification.presentLocalNotification(RNPushNotification.java:156)
项目的gradle设置为:
buildToolsVersion = "27.0.3"
minSdkVersion = 19
compileSdkVersion = 27
targetSdkVersion = 27
supportLibVersion = "27.1.1"
这是我的代码。我在响应App组件之前在App.js中使用configure方法。
export const configure = ()=>{
PushNotification.configure({
onRegister: function(token) {
console.log( 'TOKEN:', token );
},
onNotification: function(notification) {
console.log( 'NOTIFICATION');
},
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: true,
requestPermissions: true,
});
}
export const localNotification = (title="",bigText="" )=>{
PushNotification.localNotification({
title:title,
message: bigText,
playSound: true,
soundName: 'default',
});
}
这是我的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mahan">
.....
.....
<uses-permission android:name="android.permission.GET_TASKS" /> <!-- <- Add this line -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- <- Add this line -->
<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">
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name"
android:value="xchannel"/>
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description"
android:value="YOUR NOTIFICATION CHANNEL DESCRIPTION"/>
<!-- Change the resource name to your App's accent color - or any other color you want -->
<meta-data android:name="com.dieam.reactnativepushnotification.notification_color"
android:resource="@android:color/white"/>
<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.DOWNLOAD_COMPLETE"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!-- enable deep linking -->
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- for example pay://herfehplus !-->
<data android:scheme="pay"
android:host="payhost"
/>
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
</manifest>