2017-08-16 90 views
0

你好我试图发送推送通知之间的两个用户A & B我的Android应用程序。使用OneSignal网站是一种手动方式,我想通过应用程序本身发送通知,例如用户A按下一个按钮并发送通知给用户B. 任何帮助将不胜感激。使用OneSignal发送通知黑白设备

回答

1

要发送使用OneSignal的自定义通知,您需要使用OneSignal URL的授权和通知结构可能与我共享我的代码。

https://onesignal.com/api/v1/notifications 

传递这些标题

Content-Type application/json; charset=UTF-8 
Authorization Basic <your-rest-client-key> 

低于设定JSON进入你的身体

{ 
    "app_id": "<your-app-id>", 
    "included_segments": ["All"], 
    "content_available":"true", 
    "data": {"foo": "bar"}, 
    "contents": {"en": "Test_Message_Body"}, 
    "headings": {"en": "Test_Message_Title"} 
} 
+0

感谢它的工作,现在我只需要弄清楚如何针对特定的用户。 –

+0

如果答案是有效的,所以请接受,因为它对另一个有帮助。 –

0

这是OneSignal官方博客的代码通过使用过滤器,以针对特定的用户。这帮助我解决了我的问题。

try { 
     String jsonResponse; 

     URL url = new URL("https://onesignal.com/api/v1/notifications"); 
     HttpURLConnection con = (HttpURLConnection)url.openConnection(); 
     con.setUseCaches(false); 
     con.setDoOutput(true); 
     con.setDoInput(true); 

     con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
     con.setRequestProperty("Authorization", "Basic ZWY0YTU2YTItMjUzMC00NGY3LThiNTQtODFiY2U1NjQ5NmZj"); 
     con.setRequestMethod("POST"); 


     //////////////////////////////// --> Apply Search Criteria Filters Here <-- ///////////////////////// 
     String strJsonBody = "{" 
       + "\"app_id\": \"5eb5a37e-b458-11e3-ac11-000c2940e62c\"," 
       + "\"filters\": [{\"field\": \"tag\", \"key\": \"" + himID + "\", \"relation\": \"=\", \"value\": " + 
       "\"himID\"},{\"operator\": \"OR\"},{\"field\": \"amount_spent\", \"relation\": \">\",\"value\": \"0\"}]," 
       + "\"data\": {\"foo\": \"bar\"}," 
       + "\"contents\": {\"en\": \"One Signal Notification Test\"}" 
       + "}"; 
     /////////////////////////////////////////////////////////////////////////////////////////////////// 

     Log.d("Query Check->"," Query Check-> jsonResponse:\n" + strJsonBody); 

     byte[] sendBytes = strJsonBody.getBytes("UTF-8"); 
     con.setFixedLengthStreamingMode(sendBytes.length); 

     OutputStream outputStream = con.getOutputStream(); 
     outputStream.write(sendBytes); 

     int httpResponse = con.getResponseCode(); 
     System.out.println("httpResponse: " + httpResponse); 

     if ( httpResponse >= HttpURLConnection.HTTP_OK 
       && httpResponse < HttpURLConnection.HTTP_BAD_REQUEST) { 
      Scanner scanner = new Scanner(con.getInputStream(), "UTF-8"); 
      jsonResponse = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : ""; 
      scanner.close(); 
     } 
     else { 
      Scanner scanner = new Scanner(con.getErrorStream(), "UTF-8"); 
      jsonResponse = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : ""; 
      scanner.close(); 
     } 
     Log.d("Query Check->"," Query Check-> jsonResponse:\n" + jsonResponse); 

    } catch(Throwable t) { 
     t.printStackTrace(); 
    } 
0

使用Java代码: - 其中userid是reciever

try { 
         OneSignal.postNotification(new JSONObject("{'contents': {'en': '"+ msg_welcome +"'}, 'include_player_ids': ['" + userId + "']}"), 
           new OneSignal.PostNotificationResponseHandler() { 
            @Override 
            public void onSuccess(JSONObject response) { 
             Log.i("OneSignalExample", "postNotification Success: " + response.toString()); 

            } 

            @Override 
            public void onFailure(JSONObject response) { 
             Log.e("OneSignalExample", "postNotification Failure: " + response.toString()); 
            } 
           }); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

的唯一注册ID使用PHP: - 其中$ DEVICE_ID是reciever的唯一注册ID

<?PHP 
function sendMessage($device_id,$msg_title,$msg_body,$msg_img){ 
    $content = array(
     "en" => $msg_body 
    ); 
    $heading = array(
     "en" => $msg_title 
    ); 
// $device_id = "da2e72a0-6af7-4102-819e-4b7db5XXXXXX"; 
    $include_player_id = array(
     $device_id 
    ); 

    $fields = array(
     'app_id' => "YOUR_APP_ID", 
     'contents' => $content, 
     'headings' => $heading, 
     'data' => array("foo" => "bar"), 
     'small_icon'=> "ic_launcher", 
     'large_icon'=> "ic_launcher", 
     'big_picture'=> $msg_img, 
     'include_player_ids' => $include_player_id 

    ); 

    $fields = json_encode($fields); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 
          'Authorization: Basic YOUR_REST_API_KEY')); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_HEADER, FALSE); 
    curl_setopt($ch, CURLOPT_POST, TRUE); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

    $response = curl_exec($ch); 
    curl_close($ch); 

    return $response; 
    } 

    $response = sendMessage(); 
    $return["allresponses"] = $response; 
    $return = json_encode($return); 

    print("\n\nJSON received:\n"); 
    print($return); 
    print("\n"); 
?> 
相关问题