React Native-Android-FCM-显示群组通知,例如What's app也允许多个分组通知 [英] React Native - Android - FCM - Display group notification like What's app also allowing multiple grouped notifications

查看:44
本文介绍了React Native-Android-FCM-显示群组通知,例如What's app也允许多个分组通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个组通知,而不是像whatsapp那样显示多个通知.例如:一个带有消息的通知-"2个讨论1条评论",而不是总共三个通知.

I want to display a group notification instead of multiple notifications like whatsapp does. For eg: One notification with message - "2 discussions 1 comment" instead of total three notifications.

我使用了react-native-fcm库( https://github.com/evollu/react-native-fcm )我使用了&标记键,但无法达到以下代码

I used react-native-fcm library (https://github.com/evollu/react-native-fcm) I used group & tag keys but couldn't achieve the result as below code

FCM.presentLocalNotification({
  title: 'Title',
  body: 'Body',
  priority: "high",
  click_action: true,
  show_in_foreground: true,
  local: true,
  group: 'group1',
  tag: 'tag1'
});

是否可以在本机FCM中实现此功能?请让我知道.

Is it possible to achieve this functionality in react native FCM? Please let me know.

推荐答案

项目react-native-fcm已移至react-native-firebase下,并且

The project react-native-fcm is moved under react-native-firebase and there is a solution under this issue on the project.

主要思想:

诀窍是创建一个额外的通知,其中将包含该组的通知.

The trick is to create an additional notification that will contain the notifications for that group.

// ID for grouping notifications, always the same
const SUMMARY_ID = `${ALERTS_GROUP}.summary`

const sendIt = (notification: Firebase.notifications.Notification) => {
  return firebase.messaging().hasPermission().then((yes) => {
    if (yes) {
      try {
        return firebase.notifications().displayNotification(notification)
          .catch((err) => {
            Log.e(`[sendNotification] ERROR: ${err}`)
            return Promise.resolve()
          })
      } catch (err) {
        Log.e('[sendNotification] Error displaying notification: ' + err)
      }
    }
    return Promise.resolve()
  })
}

const sendSummary = (data: MessageData) => {
  const summary = new firebase.notifications.Notification()
    .setNotificationId(SUMMARY_ID)
    .setTitle(_T('notification.channels.alert.description'))
    .setData(data)
    .android.setAutoCancel(true)
    .android.setCategory(firebase.notifications.Android.Category.Message)
    .android.setChannelId(getChannelId(MsgType.Alert))
    .android.setColor(variables.scheme.primaryColor)
    .android.setSmallIcon(STATUS_ICON)
    .android.setGroup(ALERTS_GROUP)
    .android.setGroupSummary(true)
    .android.setGroupAlertBehaviour(firebase.notifications.Android.GroupAlert.Children)
  sendIt(summary)
}

/**
 * Called by `bgMessaging` or the `onMessage`  handler.
 */
export function sendNotification (message: Firebase.messaging.RemoteMessage) {
  const payload: MessagePayload = message.data as any || {}
  const notification = new firebase.notifications.Notification()
  // ... more code

  if (Platform.OS === 'android' && Platform.Version >= 24) {
    notification.android.setGroup(ALERTS_GROUP)
    sendSummary(notification.data)
  }
  Log.v('[sendSummary] sending notification.')
  return sendIt(notification)
}

这篇关于React Native-Android-FCM-显示群组通知,例如What's app也允许多个分组通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆