当通知到达带有 react-native-firebase 的 Android 时,如何唤醒设备?

Hyu*_*Lee 7 android react-native react-native-firebase

我正在使用react-native-firebaseon React nativein开发通知功能Android

大部分功能(如前台后台远程通知、显示本地通知)都实现的很好,但有一个问题我没有解决。

正如我上面所说,在前台和后台接收通知运行良好。当设备的屏幕关闭(锁定模式或打瞌睡模式)时,通过振动和声音很好地接收通知,但屏幕不亮。我已经在模拟器(带有 Google play 的 Nexus 5X)、LG G Flex2、三星 Galaxy S8 上进行了测试。

期待

当通知到达 Android 时唤醒设备(打开屏幕)

现在的情况

收到通知时,声音和振动正常,但屏幕关闭。

我还没有在 iOS 上测试过。这是我的配置。

gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
Run Code Online (Sandbox Code Playgroud)

build.gradle(项目)

dependencies {
    classpath 'com.android.tools.build:gradle:3.1.2'
    classpath 'com.google.gms:google-services:3.2.1'
}
Run Code Online (Sandbox Code Playgroud)

build.gradle(应用程序)

compileSdkVersion 26
minSdkVersion 16
targetSdkVersion 26
Run Code Online (Sandbox Code Playgroud)

反应本机版本

"react": "^16.3.0-alpha.1",
"react-native": "0.54.3",
Run Code Online (Sandbox Code Playgroud)

react-native-firebase 版本

"react-native-firebase": "^4.2.0",
Run Code Online (Sandbox Code Playgroud)

客户端接收通知代码

async configureNotifications() {
    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
        this.listenNotification();
    } else {
        try {
            await firebase.messaging().requestPermission();
            this.listenNotification();
        } catch (e) {
            Alert.alert('', '?? ????? ?? ?? ??? ?? ? ? ????.');
        }
    }
}

listenNotification() {
    if (Platform.OS === 'android') {
        const channel = new firebase.notifications.Android.Channel(
            'alert',
            'alert',
            firebase.notifications.Android.Importance.Max,
        ).setDescription('My apps alert');
        firebase.notifications().android.createChannel(channel);
    }
    const subProm = [];
    const { account } = this.props;
    const { deviceInfo } = account;

    for (let i = 0; i < deviceInfo.length; i++) {
        subProm.push(firebase.messaging().subscribeToTopic(deviceInfo[i].deviceID));
    }

    Promise.all(subProm).then(() => {
        this.messageListner = firebase.messaging().onMessage((message) => {
            let newNotification;

            if (Platform.OS === 'android') {
                newNotification = new firebase.notifications.Notification()
                    .setNotificationId(message.messageId)
                    .setTitle(message.data.title)
                    .setBody(message.data.body)
                    .setSound('default')
                    .android.setPriority(firebase.notifications.Android.Priority.High)
                    .android.setChannelId('alert');
            }

            return firebase.notifications().displayNotification(newNotification);
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

客户端后台无头任务处理程序

export default async (message) => {
    console.log('on Background Message');
    console.log(message);
    let newNotification;

    if (Platform.OS === 'android') {
        newNotification = new firebase.notifications.Notification()
            .setNotificationId(message.messageId)
            .setTitle(message.data.title)
            .setBody(message.data.body)
            .setSound('default')
            .android.setPriority(firebase.notifications.Android.Priority.High)
            .android.setChannelId('alert');
    }

    return firebase.notifications().displayNotification(newNotification);
};
Run Code Online (Sandbox Code Playgroud)

索引.js

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);
Run Code Online (Sandbox Code Playgroud)

服务器端发送通知码(测试版)

nodejs 与 firebase-admin

var msg = {
    android: {
        ttl: 36000,
        data: {
            title: 'title',
            body: 'body' + topic,
        },
        priority: 'high',
    },
    topic,
};

admin
    .messaging()
    .send(msg)
    .then((res) => {
        console.log('Successfully sent message', res);
        process.exit();
    }, console.error);
Run Code Online (Sandbox Code Playgroud)