2

有没有人设法在后台和前台模式下修复 Android 上 react-native 的推送通知。

我正在使用 react-native 构建一个应用程序,该应用程序使用 ExpoKIt 与 Expo 分离。

我正在使用 RN 0.49 和这个库:https ://github.com/zo0r/react-native-push-notification来处理推送通知。

我的组件处理推送通知如下所示:

    import React from 'react';
var PushNotification = require('react-native-push-notification');

PushNotification.configure({

    // (optional) Called when Token is generated (iOS and Android)
    onRegister: function(token) {
        console.log( 'TOKEN RECEIVED:', token );
    },

    // (required) Called when a remote or local notification is opened or received
    onNotification: function(notification) {
        console.log( 'NOTIFICATION:', notification );

        PushNotification.localNotification({
            /* Android Only Properties */
            id: '0', // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID
            ticker: "My Notification Ticker", // (optional)
            autoCancel: true, // (optional) default: true
            largeIcon: "ic_launcher", // (optional) default: "ic_launcher"
            smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher"
            bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
            subText: "This is a subText", // (optional) default: none
            color: "red", // (optional) default: system default
            vibrate: true, // (optional) default: true
            vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
            tag: 'some_tag', // (optional) add tag to message
            group: "group", // (optional) add group to message
            ongoing: false, // (optional) set whether this is an "ongoing" notification
            title: "My Notification Title", // (optional, for iOS this is only used in apple watch, the title will be the app name on other iOS devices)
            message: "My Notification Message", // (required)
        });

        // process the notification

        // required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
        notification.finish(PushNotificationIOS.FetchResult.NoData);
    },

    // ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
    senderID: "23232323232323",

    // IOS ONLY (optional): default: all - Permissions to register.
    permissions: {
        alert: true,
        badge: true,
        sound: true
    },

    // Should the initial notification be popped automatically
    // default: true
    popInitialNotification: true,

    /**
     * (optional) default: true
     * - Specified if permissions (ios) and token (android and ios) will requested or not,
     * - if not, you must call PushNotificationsHandler.requestPermissions() later
     */
    requestPermissions: true,
});

export default class Geolocation extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            enabled: false,
            isMoving: false,
            isChangingPace: false,
            odometer: (0 / 1).toFixed(1),
            currentActivity: 'unknown',
            currentProvider: undefined,
            locationAllowed: false,
            coordinates: {
                coords: {
                    latitude: 60.161212,
                    longitude: 24.905779
                }
            },
            geofences: [],
            geofencesHit: [],
            geofencesHitEvents: [],
            message: 'Hello there',
            division: ''
        };
    }

    componentDidMount(){

    }

    componentWillMount() {

    }

    render() {
        return (
            null
        );
    }
}

MainApplication.java:

 package x.y.z;

import android.support.multidex.MultiDexApplication;

import com.facebook.react.ReactPackage;
import com.transistorsoft.rnbackgroundgeolocation.*;


import java.util.Arrays;
import java.util.List;


// Needed for `react-native link`
//import com.facebook.react.ReactApplication;
import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage;
//import com.transistorsoft.rnbackgroundgeolocation.RNBackgroundGeolocation;
//import io.invertase.firebase.RNFirebasePackage;
//import io.invertase.firebase.messaging.RNFirebaseMessagingPackage;
import com.oblador.vectoricons.VectorIconsPackage;

    public class MainApplication extends MultiDexApplication {
          public List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new ReactNativePushNotificationPackage(),

                    new RNBackgroundGeolocation(),
                    // Needed for `react-native link`

                    //new RNBackgroundGeolocation(),
                    //new RNFirebasePackage(),
                    new VectorIconsPackage()
                    //new RNFirebaseMessagingPackage()
            );
          }
        }

显现:

    <?xml version="1.0" encoding="utf-8"?>
<manifest package="x.y.z"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission android:name="x.y.z.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="x.y.z.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


    <uses-feature android:name="android.hardware.location.gps" />

    <application
        android:name=".MainApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true">
        <activity android:name=".MainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:theme="@style/Theme.Exponent.Light"
            android:windowSoftInputMode="adjustResize"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

            <intent-filter>
                <data android:scheme="exp6a747c4f7f604b89a93d9f3d281cbb77"/>

                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
            </intent-filter>
        </activity>

        <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="x.y.z" />
            </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>
        <!-- The Facebook SDK runs FacebookInitProvider on startup and crashes if there isn't an ID here -->
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>

        <!-- react-native-background-geolocation licence -->
        <meta-data android:name="com.transistorsoft.locationmanager.license" android:value="14ef777231a8412c8b2d109443999356a2c13e62c07e2868345f5e13f01b3c83" />


        <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@mipmap/ic_launcher"/>


    </application>

</manifest>

我可以看到从 onREgister 方法生成的 TOKEN,但是 onNOtification 似乎永远不会被调用,也不会出现任何日志或错误。我只是感觉缺少一些收听寄存器,我不确定是什么!

4

0 回答 0