通知响应本地聊天应用

时间:2018-07-19 09:25:52

标签: firebase react-native notifications firebase-notifications

我实际上在使用react-native的聊天应用程序上,并且我在firebase上有一个数据库。我想知道在应用上发布新消息时如何获得通知?那是我生成消息的地方:

import * as types from './actionTypes'
import firebaseService from '../../services/firebase'
import Component from './app/components/AuthScreen/AuthTabs/BasicForm/Component.js'

const FIREBASE_REF_MESSAGES = firebaseService.database().ref('/GeneralMessage')
const FIREBASE_REF_MESSAGES_LIMIT = 20

export const sendMessage = message => {
  return (dispatch) => {
    dispatch(chatMessageLoading())

    let currentUser = firebaseService.auth().currentUser
    let createdAt = new Date().getTime()
    let chatMessage = {
      text: message,
      createdAt: createdAt,
      user: {
        id: currentUser.uid,
        email: currentUser.email,
      }
    }

    FIREBASE_REF_MESSAGES.push().set(chatMessage, (error) => {
      if (error) {
        dispatch(chatMessageError(error.message))
      } else {
        dispatch(chatMessageSuccess())
      }
    })
  }
}

export const updateMessage = text => {
  return (dispatch) => {
    dispatch(chatUpdateMessage(text))
  }
}

export const loadMessages = () => {
  return (dispatch) => {
    FIREBASE_REF_MESSAGES.limitToLast(FIREBASE_REF_MESSAGES_LIMIT).on('value', (snapshot) => {
      dispatch(loadMessagesSuccess(snapshot.val()))
    }, (errorObject) => {
      dispatch(loadMessagesError(errorObject.message))
    })
  }
}

2 个答案:

答案 0 :(得分:2)

只要有新消息或数据库中发生任何更改,您都可以使用 Firebase Cloud Function 发送通知。

我在这里共享示例代码。

exports.sendNotification = functions.database.ref('/notificationRequests/{values}').onCreate((event) => {

// Grab the current value of what was written to the Realtime Database.
var itemVal = event.data.val()

// Notification details.
  const payload = {
    notification: {
    title: itemVal.title,
    body: itemVal.body,
    sound: 'default',

  },
  data: {
    otherData:"extra"
  },
};

// Set the message as high priority and have it expire after 24 hours.
const options = {
  contentAvailable: true,
  priority: 'high',
  timeToLive: 60 * 60 * 24,
};

if (itemVal.tokens) {
  // Send a message to the device corresponding to the provided
  // registration token with the provided options.
  return admin.messaging().sendToDevice(itemVal.tokens, payload, options)
    .then((response) => {


    })
    .catch((error) => {
      console.log('Error : ', error);

    });
} else {

}
});

参考链接:

Firebase Cloud Functions
react-native-push-notifications-with-firebase-cloud-functions
https://aaronczichon.de/2017/03/13/firebase-cloud-functions/
react-native-chat-app-serverless-firebase-push-notifications

答案 1 :(得分:0)

Microsoft App Center Push使您可以从App Center门户向应用程序的用户发送推送通知。 App Center门户和Push SDK已与Firebase Cloud Messaging集成。

相关问题