2016-05-30 87 views
0

我正在開發使用cordova的android應用程序,並且在我的應用程序中包含一個信號通知作爲功能。OneSignal可以通過應用程序發送通知嗎?

我有2個應用程序,第一個爲客戶,第二個爲賣家。我的兩個應用程序都使用不同的包名(com.myapp.customer和com.myapp.seller)

我註冊onesignal並測試以推送通知。它工作得很好。

現在我將應用程序安裝到移動設備,並嘗試從客戶應用程序發送通知給賣家應用程序(兩個應用程序都安裝在同一設備中),但它不起作用。

我的問題是可以發送通知從客戶應用程序到賣家應用程序?如果是這樣,該怎麼做。

謝謝。

回答

1

我認爲你應該使用一個信號REST API並從你的客戶應用程序發送請求到他們的服務器。 more info

這裏是Java代碼來發送推送通知:

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 NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj"); 
    con.setRequestMethod("POST"); 

    String strJsonBody = "{" 
         + "\"app_id\": \"5eb5a37e-b458-11e3-ac11-000c2940e62c\"," 
         + "\"included_segments\": [\"All\"]," 
         + "\"data\": {\"foo\": \"bar\"}," 
         + "\"contents\": {\"en\": \"English Message\"}" 
         + "}"; 


    System.out.println("strJsonBody:\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(); 
    } 
    System.out.println("jsonResponse:\n" + jsonResponse); 

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

PHP代碼:

<?PHP 
    function sendMessage(){ 
    $content = array(
     "en" => 'English Message' 
    ); 

    $fields = array(
     'app_id' => "5eb5a37e-b458-11e3-ac11-000c2940e62c", 
     'included_segments' => array('All'), 
     'data' => array("foo" => "bar"), 
     'contents' => $content 
    ); 

    $fields = json_encode($fields); 
    print("\nJSON sent:\n"); 
    print($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 NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj')); 
    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"); 
?> 

注:更換賣家應用程序ID和授權密鑰在上面的代碼!

+0

謝謝!順便說一句,你有一個PHP的例子嗎?我使用codeigniter。 – Giffary

+0

是的,我編輯了答案! –

相關問題