【问题标题】:Can OneSignal send notification across app?OneSignal 可以跨应用发送通知吗?
【发布时间】:2016-05-30 07:47:53
【问题描述】:

我正在使用 cordova 开发 android 应用程序,并在我的应用程序中包含一个信号通知作为一项功能。

我有 2 个应用程序,第一个用于客户,第二个用于卖方。我的两个应用程序都使用不同的包名称(com.myapp.customer 和 com.myapp.seller)

我注册一个信号并测试推送通知。效果很好。

现在我将应用安装到移动设备并尝试从客户应用向卖家应用发送通知(两个应用都安装在同一设备上),但它不起作用。

我的问题是可以将通知从客户应用程序发送到卖方应用程序吗?如果有,该怎么做。

谢谢。

【问题讨论】:

    标签: angularjs cordova push-notification 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和授权密钥!

    【讨论】:

    • 谢谢!顺便问一下,你有 PHP 的例子吗?我使用codeigniter。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多