当使用react-native-firebase在Android上收到通知时,如何唤醒设备?

时间:2018-06-05 02:36:15

标签: android react-native react-native-firebase

我正在使用react-native-firebase React native Android上的distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 开发通知功能。

大多数功能(例如前景和背景上的远程通知,显示本地通知)都得到了很好的实现,但是我还没有解决一个问题。

正如我上面所说,接收有关前景和背景的通知效果很好。 当设备的屏幕关闭(锁定模式或打盹模式)时,通知会受到振动和声音的响应,但屏幕未打开。 我在模拟器(带谷歌播放的Nexus 5X),LG G Flex2,三星galaxy S8上进行了测试。

期望

在Android上收到通知时唤醒设备(启用屏幕)

现状

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

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

gradle-wrapper.properties

dependencies {
    classpath 'com.android.tools.build:gradle:3.1.2'
    classpath 'com.google.gms:google-services:3.2.1'
}

build.gradle(项目)

compileSdkVersion 26
minSdkVersion 16
targetSdkVersion 26

build.gradle(app)

"react": "^16.3.0-alpha.1",
"react-native": "0.54.3",

React Native Version

"react-native-firebase": "^4.2.0",

react-native-firebase版本

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);
        });
    });
}

客户端接收通知代码

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);
};

客户端背景无头任务处理程序

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);

index.js

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);

的nodejs
# Definition for a binary tree node.
class TreeNode(object):
  def __init__(self, x):
    self.val = x
    self.left = None
    self.right = None

class Solution(object):
  def buildTree(self, preorder, inorder):
    """
    :type preorder: List[int]
    :type inorder: List[int]
    :rtype: TreeNode
    """
    preorder.reverse()
    return self.buildTreeHelper(preorder, inorder)

  def buildTreeHelper(self, preorder, inorder):
    if not preorder or not inorder:
      return None

    rootValue = preorder.pop()
    root = TreeNode(rootValue)

    inorderRootIndex = inorder.index(rootValue)

    root.left = self.buildTreeHelper(preorder, inorder[:inorderRootIndex])
    root.right = self.buildTreeHelper(preorder, inorder[inorderRootIndex+1:])
    return root

0 个答案:

没有答案