使用 Firebase 和 OneSignal 发送推送通知

Send push notifications using Firebase and OneSignal

不确定这是否可行,但我有一个现有的 Ionic 3 应用程序,它使用 Firebase 作为后端。也许只有我一个人,我无法在同一个应用程序中集成 Google 登录、Facebook 登录和推送通知。已经试了几天了。

我能够安装 OneSignal 并将推送通知发送到 Android 设备,但我想使用为每个设备保存的令牌以编程方式发送它们,而不是从 OneSignal 仪表板。

这就是我在 Firebase Cloud Functions 中用来发送通知的方式。是否可以修改为将通知发送到OneSignal,然后发送到每个设备?

`function sendFcm(userID, eventSnapshot, eventID) {

  const getDeviceTokensPromise = admin.database().ref(`/fcmTokens/${userID}/`).once('value');

  return Promise.all([getDeviceTokensPromise]).then(result => {

    const tokensSnapshot = result[0];

    const payload = {
      "notification": {
        "title": "Your invitation has arrived",
        "body": eventSnapshot.name,
        "sound": "default",
        // "click_action": "FCM_PLUGIN_ACTIVITY",
        "icon": "fcm_push_icon"
      },
      "data": {
        "eventId": eventID,
        "uid": userID,
        "eventObj": JSON.stringify(eventSnapshot),
        "notificationType": "newEventNotification"
      }
    };

    const tokens = Object.keys(tokensSnapshot.val());

    console.log(tokens);

    // Send notifications to all tokens.
    return admin.messaging().sendToDevice(tokens, payload).then(response => {
      // For each message check if there was an error.
      const tokensToRemove = [];
      response.results.forEach((result, index) => {
        console.log(tokens[index]);
        const error = result.error;
        if (error) {
          console.error('Failure sending notification to', tokens[index], error);
          // Cleanup the tokens which are not registered anymore.
          if (error.code === 'messaging/invalid-registration-token' ||
            error.code === 'messaging/registration-token-not-registered') {
            tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
          }
        }
      });
      return Promise.all(tokensToRemove);
    });
  })
}`

经过一番搜索,我找到了 OneSignal API。似乎我只需要保存玩家 ID 并将其或多个数组发送到 onesignal.com/api/v1/notifications。此处有更多详细信息:https://documentation.onesignal.com/reference#section-send-based-on-onesignal-playerids-create-notification