如何在带有React-native-firebase的iOS设备上通过推送通知添加按钮?

问题描述 投票:1回答:1

我有一个疑问,我无法解决使用按钮在带有react-native-firebase的iOS推送通知中执行操作(接受或拒绝)的问题。对于Android,我可以做到,但对于iOS,我找不到注册动作的方法。接下来,我展示我的代码如何。

const notificationListener = () => {
  firebase.notifications().onNotification((notification) => {
    const localNotification = new firebase.notifications.Notification({
        sound: 'default',
        show_in_foreground: true,
      })
      .setSound('default')
      .setBody(notification.body)
      .setData(notification.data)
      .setTitle(notification.title)
      .setSubtitle(notification.subtitle)
      .setNotificationId(notification.notificationId);
    if (Platform.OS === 'android') {      
      localNotification
        .android.setBigText(notification.body)
        .android.setSmallIcon('ic_noti_logo_hnc')
        .android.setLargeIcon('ic_launcher')
        .android.setVisibility(firebase.notifications.Android.Visibility.Public)
        .android.setChannelId(CHANNEL_NOTIFICATIONS.CHANNEL_ID)
        .android.setPriority(firebase.notifications.Android.Priority.High);

      if (isEqual(localNotification.data.set_delay_time, "true")){
        // Build an action
        const acceptAction = new firebase.notifications.Android.Action('accept_action', 'default', 'Accept');
        const rejectAction = new firebase.notifications.Android.Action('reject_action', 'default', 'Reject');
        localNotification
          .android.addAction(acceptAction)
          .android.addAction(rejectAction);
      }

    } else if (Platform.OS === 'ios') {
      localNotification
        .ios.setBadge(notification.ios.badge);
    }
    firebase.notifications().displayNotification(localNotification)
      .catch(err => console.error(err));
  });
};

我打算做的事情与您在Android上发现的相似,但我发现最多的是IOSNotification.alertAction。

有人可以指导我吗?从已经非常感谢您!

Nico。

ios firebase react-native react-native-firebase
1个回答
0
投票

使用“ react-native-firebase”版本6。您可以从here获取迁移信息。

您可以使用Notifee显示动作按钮并处理所有动作单击事件。

定义动作,

import notifee from '@notifee/react-native';

async function setCategories() {
  await notifee.setNotificationCategories([
    {
      id: 'post',
      actions: [
        {
          id: 'like',
          title: 'Like Post',
        },
        {
          id: 'dislike',
          title: 'Dislike Post',
        },
      ],
    },
  ]);
}

分配类别以推送通知,

import notifee from '@notifee/react-native';

notifee.displayNotification({
  title: 'New post from John',
  body: 'Hey everyone! Check out my new blog post on my website.',
  ios: {
    categoryId: 'post',
  },
});
© www.soinside.com 2019 - 2024. All rights reserved.