如何在没有 OneSignal 仪表板的情况下发送推送通知?

How to send push notifications without OneSignal Dashboard?

我想向安装了我的 apk 的所有设备发送推送通知,我遵循本教程:

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

并成功。

但我想不通过 OneSignal 仪表板网站发送推送通知。 我不知道如何将它实现到代码中。

你能帮帮我吗..

使用 Url https://onesignal.com/api/v1/notifications

发出 https POST 请求

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

在Body中添加

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

您可以将通知发送到特定细分并为用户创建过滤器等等,请参阅此文档以获取更多信息 https://documentation.onesignal.com/reference

要在没有 OneSignal 仪表板的情况下发送通知,您可以将标签附加到用户并通过 POST 使用 URL https://onesignal.com/api/v1/notifications

向他们发送通知

只能使用 OneSignal 仪表板创建细分,但可以使用 SDK/API.

创建标签

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

在body中添加

        {
   "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"
      }
   ]
}

然后发出 JSON object 请求以完成该过程。

您可以参考以下代码,了解如何将 headers 附加到 JSON object 请求以及如何将标签附加到您的用户。 (此请求已使用 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();
                }

为用户附加标签

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

这应该会成功完成您的查询。