【问题标题】:How to add button in push-noitifications to iOS devices with react-native-firebase?如何使用 react-native-firebase 在 iOS 设备的推送通知中添加按钮?
【发布时间】:2019-11-07 21:16:57
【问题描述】:

我有一个疑问,我无法通过 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。

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

尼科。

【问题讨论】:

    标签: ios firebase react-native react-native-firebase


    【解决方案1】:

    使用“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',
      },
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 2017-12-07
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      相关资源
      最近更新 更多