2016-12-23 52 views
1

我需要的是,当用户点击一个按钮,推送通知将被发送到所有使用这个应用程序的用户。 我尝试在setOnclickListener方法中使用此代码,但单击它后没有发送任何内容。发送解析推送通知按钮点击android

注意:从Parse Dashboard发送推送通知完美地工作 罚款。

 ParsePush parsePush = new ParsePush(); 
     ParseQuery query = ParseInstallation.getQuery(); 
     parsePush.setQuery(query); 
     parsePush.setMessage("A new file has been Updated. Check it out!"); 
     parsePush.sendInBackground(new SendCallback() { 

      @Override 
      public void done(ParseException arg0) { 
       // TODO Auto-generated method stub 
       Log.d(TAG, "SendCallback success:"); 
       if(arg0 == null) 
       { 
        Log.d(TAG, 
          "suceess push notification :"); 
       } 
       else 
       { 
        Log.d(TAG, 
          "failed push notification :" 
            + arg0.getMessage()); 
       } 
      } 
     }); 
    } 
}); 

- 编辑 - 响应布拉德利·威尔逊的答案 - 这仍然没有工作

ParsePush parsePush = new ParsePush(); 
ParseQuery<ParseInstallation> parseQueryInstallation = ParseQuery.getQuery(ParseInstallation.class); 
parsePush.setQuery(parseQueryInstallation); 
parsePush.setMessage("A new file has been Updated. Check it out!"); 
parsePush.sendInBackground(new SendCallback() { 

    @Override 
    public void done(ParseException arg0) { 
     // TODO Auto-generated method stub 
     Log.d(TAG, "SendCallback success:"); 
     if(arg0 == null) 
     { 
      Log.d(TAG, 
        "suceess push notification :"); 
     } 
     else 
     { 
      Log.d(TAG, 
        "failed push notification :" 
          + arg0.getMessage()); 
     } 
    } 

- EDIT 2 - 响应苏雷什·库马尔的答案 - 对于一些原因,云代码从未在我的项目中起作用。它只是不识别任何Cloud代码并将其保留为红色as shown in this image

+0

尝试ParseQuery parseQueryInstallation = ParseQuery.getQuery(ParseInstallation.class);而不是ParseInstallation.getQuery(); –

+0

@BradleyWilson谢谢你的回答^^我试过替换它(我在我的问题中编辑了它),但它仍然无效 –

回答

1

在Parse中发送推送通知的更好方法是通过云代码。使用ParseCloud.callFunctionInBackground()创建一个云端函数来发送推送通知并在android中调用该云端函数。

HashMap<String, Object> params = new HashMap<String, Object>(); 
params.put("recipientId", userObject.getObjectId()); 
params.put("message", message); 
ParseCloud.callFunctionInBackground("sendPushToUser", params, new FunctionCallback<String>() { 
    void done(String success, ParseException e) { 
     if (e == null) { 
      // Push sent successfully 
     } 
    } 
}); 

看看这个link

+0

谢谢你的回答^^我编辑了我的问题以回应你的回答 –