【问题标题】:Add PlayerId to Meteor.user using oneSignal使用 oneSignal 将 PlayerId 添加到 Meteor.user
【发布时间】:2016-12-10 17:19:46
【问题描述】:

我正在尝试使用one Signal 在我的用户下订单以及订单更改时向他们发送推送通知。如何将一个 Signal 的 Player ID 关联到 Meteor 用户?

那么我可以使用下面的 Meteor 方法吗?

Meteor.methods({
  finishOrder: function(id) {
    var data;
    var PlayerId = Meteor.users.findOne({_id:id}).playerId
    data = {
      contents: {
        en: 'We are reviewing the order'
      }
    };
    return OneSignal.Notifications.create([PlayerId], data);
  }
});

我认为这与它有关

window.plugins.OneSignal.getIds(function(ids) {
  console.log('getIds: ' + JSON.stringify(ids));
  alert("userId = " + ids.userId + ", pushToken = " + ids.pushToken);
});

var userVar = new ReactiveVar(null);


if (Meteor.isCordova) {
  document.addEventListener('deviceready', function () {
    window.plugins.OneSignal.setLogLevel({logLevel: 6, visualLevel: 4});
    window.plugins.OneSignal
      .startInit('------')
      .getIds(function(ids) {
        userVar.set(ids.userId)
      .endInit();
      }, false);
  });
  Accounts.onLogin(function() {
    return window.plugins.OneSignal.getIds(function(ids) {
      return Meteor.users.update({
        _id: Meteor.userId()
      }, {
        $set: {
          playerId: userVar.get()
        }
      });
    });
  });
}

但我无法让它工作。因为它会运行并且不会获得 Meteor 用户,因为它还没有登录。

【问题讨论】:

  • 确保在调用.endItit() 之前没有调用OneSignal.getIds。另请注意,OneSignal.getIds 不返回值,并且传递给它的函数不会对它的返回值做任何事情。不确定你是否依赖这个。
  • @jkasten 如果 OneSignal.getIds 没有返回值,那么我将如何将玩家 ID 分配给 Meteor.user?这样只有在订单更改时才通知用户? documentation.onesignal.com/docs/cordova-sdk#section--getids- 是做什么的?

标签: cordova meteor meteor-accounts onesignal


【解决方案1】:

终于得到了答案,很简单。 首先像cordova一样使用流星并使用反应变量将玩家ID分配给userVar(反应变量)

var userVar = new ReactiveVar(null);


if (Meteor.isCordova) {
  document.addEventListener('deviceready', function () {
    window.plugins.OneSignal.setLogLevel({logLevel: 5, visualLevel: 4});
    window.plugins.OneSignal.startInit('-------')
    window.plugins.OneSignal.getIds(function(ids) {
      userVar.set(ids.userId);
    });
    window.plugins.OneSignal.endInit();
      }, false);
  Accounts.onLogin(function() {
    Meteor.call('addPlayerId', userVar.get())
  });

}

然后使用在服务器中建立的 Meteor Method

Meteor.methods({
  addPlayerId: function(playerId) {
    return Meteor.users.update({
      _id: Meteor.userId()
    }, {
      $set: {
        playerId: playerId
      }
    });
  }
});

最后是最后一部分。

  Accounts.onLogin(function() {
    Meteor.call('addPlayerId', userVar.get())
  });

【讨论】:

    猜你喜欢
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    相关资源
    最近更新 更多