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

i can push notification to a specific user using its id (notification key)

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();
        }
    }
}

as in one signal documentation https://documentation.onesignal.com/docs/sending-notifications

there we can push notification to everyone(all the subscribers)

how can i push notification to everyone using code as i can send to a single subscriber

Upvotes: 2

Views: 1186

Answers (1)

JakeB
JakeB

Reputation: 2113

Send to all Active Users with the included_segments key:

Update:

It seems like using include_player_ids does not require an API Key to send the message. However, using included_segments, does - "Requires your OneSignal App's REST API Key"

Have ammended the code below to include the app_id key.

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

Upvotes: 1

Related Questions