如何向一个信号的每个订户推送消息/通知

时间:2019-09-10 20:35:05

标签: android onesignal

我可以使用其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

我们可以将通知推送给所有人(所有订户)

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

1 个答案:

答案 0 :(得分:1)

使用Active Users键发送给所有included_segments

更新

似乎使用include_player_ids不需要API密钥即可发送消息。但是,使用included_segments确实可以-“需要您的OneSignal应用的REST API密钥”

修改了下面的代码以包含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