Onesignal 推送通知到特定类别

Onesignal Push Notification to specific category

我的服务器上有一个人员数据库,它包含一个 group number 属性。我想为每个组发送单独的通知。使用以下代码,它将向所有应用程序发送通知。我如何设置或订阅该应用程序,以便它知道某个手机属于某个组?每个成员都需要先登录才能访问应用程序。

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        console.log('Received Device Ready Event');
        console.log('calling setup push');
        // Set your iOS Settings
        var iosSettings = {};
        iosSettings["kOSSettingsKeyAutoPrompt"] = false;
        iosSettings["kOSSettingsKeyInAppLaunchURL"] = true;

        window.plugins.OneSignal
          .startInit("3074529d MY APP ID HERE d5eb61b000")
          .handleNotificationReceived(function(jsonData) {
            alert("Notification received: \n" + JSON.stringify(jsonData));
            console.log('Did I receive a notification: ' + JSON.stringify(jsonData));
          })
          .handleNotificationOpened(function(jsonData) {
            alert("Notification opened: \n" + JSON.stringify(jsonData));
            console.log('didOpenRemoteNotificationCallBack: ' + JSON.stringify(jsonData));
          })
          .inFocusDisplaying(window.plugins.OneSignal.OSInFocusDisplayOption.InAppAlert)
          .iOSSettings(iosSettings)
          .endInit();
    },
};

当用户订阅您的应用时,您可以获得设备的 OneSignal Player ID 并将其保存到您的数据库中。

在初始化调用后的任何时候使用 getPermissionSubscriptionState 方法获取玩家 ID。

然后您可以按照 OneSignal 的 Database Integration Guide 将播放器 ID 与您的用户和组号相关联。

另一种选择是使用 OneSignal Tagging Guide documentation 将设备与组相关联。