简体   繁体   中英

How to send push notifications without OneSignal Dashboard?

I want to send push notifications to all devices where my apk is installed, I follow this tutorial :

https://rasupe.com/cara-membuat-push-notifications-di-android-dengan-onesignal-mudah/

and successful.

but I want to send push notifications without via OneSignal dashboard website. I don't know how to implement it to the codes.

could you please help me..

Make a https POST request using Url https://onesignal.com/api/v1/notifications

Add Headers "Content-Type":"application/json" and "Authorization":"Enter your REST API Key here"

In Body add

{
    "app_id": "Enter your app id here",
    "headings": {
        "en": "Title Here"
    },
    "contents": {
        "en": "Description Here"
    },
    "included_segments":["Active Users"]
}

You can send the notifications to particular segments and create filters for users and much more, refer this documentation for more info https://documentation.onesignal.com/reference

To send notification without the OneSignal dashboard, you can attach tags to users and send notifications to them by making a POST using the URL https://onesignal.com/api/v1/notifications

Segments can be created only by using the OneSignal dashboard, but tags can be created by using the SDK/API.

Add Headers "Content-Type":"application/json" and "Authorization":"Basic <REST API Key>"

In body add

        {
   "app_id":"<App Id>",
   "headings":{
      "en":"Title Here"
   },
   "contents":{
      "en":"Description Here"
   },
   "filters":[
      {
         "field":"tag",
         "key":"level",
         "relation":"=",
         "value":"10"
      },
      {
         "operator":"OR"
      },
      {
         "field":"amount_spent",
         "relation":">",
         "value":"0"
      }
   ]
}

Then make a JSON object request to complete the process.

You can refer the following code to see how to attach headers to a JSON object request & to attach tags to your users. (This request has been made by using android volley)

  String url = "https://onesignal.com/api/v1/notifications";

                JSONObject jsonBody;
                try {
                    jsonBody = new JSONObject(
                            "{'app_id':'app-id'," +
                            "'headings': {'en': 'title'}, " +
                            "'contents': {'en': 'message'}," +
                                    "'filters':[{'field':'tag','key':'"+id+"','relation':'=','value':'true'}]}"
                    );

                //request a json object response
                JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonBody, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        //now handle the response
                        Toast.makeText(Activity.this,  "Notification successfully sent", Toast.LENGTH_SHORT).show();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //handle the error
                        Toast.makeText(Activity.this, "An error occurred", Toast.LENGTH_SHORT).show();
                        error.printStackTrace();
                    }
                })
                {    //adding header to the request
                    @Override
                    public Map<String, String> getHeaders() {
                        Map<String, String> params = new HashMap<String, String>();
                        params.put("Authorization", "Basic <REST API KEY>");
                        params.put("Content-type", "application/json");
                        return params;
                    }
                };
                // Add the request to the queue
                Volley.newRequestQueue(Activity.this).add(jsonRequest);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

Attaching tags to a user

 JSONObject tags = new JSONObject();
            try {
                tags.put("key","value");
                //for the above JSON request I have used the following key value pair
                // tags.put(id,true);  
                // where id is a string containing the userId
                //true is a boolean value
            } catch (JSONException e) {
                e.printStackTrace();
            }
            OneSignal.sendTags(tags);

This should successfully complete your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM