如何将 message/notification 推送给 One Signal 的每个订阅者

how to push message/notification to every subscriber of One Signal

我可以使用其 ID(通知密钥)向特定用户推送通知

public class SendNotification {

    public SendNotification(String message, String heading, String notificationKey){

        try {
            JSONObject notificationContent = new JSONObject(
                    "{'contents':{'en':'" + message + "'},"+
                    "'include_player_ids':['" + notificationKey + "']," +
                    "'headings':{'en': '" + heading + "'}}");
            OneSignal.postNotification(notificationContent, null);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

如在一份信号文档中 https://documentation.onesignal.com/docs/sending-notifications

在那里我们可以向所有人(所有订阅者)推送通知

如何使用代码将通知推送给每个人,就像我可以发送给单个订阅者一样

使用 included_segments 键发送给所有 Active Users

更新:

似乎使用 include_player_ids 不需要 API 密钥来发送消息。但是,使用 included_segments,确实 - "Requires your OneSignal App's REST API Key"

已修改以下代码以包含 app_id 键。

public class SendNotification {

    public SendNotification(String message, String heading, String notificationKey){
            try {
            JSONObject notificationContent = new JSONObject(
                    "{'app_id':\"YOUR_APP_ID\"," +
                    "'contents':{'en':'" + message + "'},"+
                    "'included_segments':[\"Active Users\"]," +
                    "'headings':{'en': '" + heading + "'}}");
            OneSignal.postNotification(notificationContent, null);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

https://documentation.onesignal.com/reference#section-example-code-create-notification