Java GCM通知中的按钮
我正在开发一个应用程序,我正在尝试用按钮发送通知,但它不起作用,通知来了,但没有按钮 如何在通知中包含按钮? 我试过了,但没用:( 我在论坛上搜索过,但是什么都没有!有什么帮助吗 示例按钮:Java GCM通知中的按钮,java,android,push-notification,notifications,google-cloud-messaging,Java,Android,Push Notification,Notifications,Google Cloud Messaging,我正在开发一个应用程序,我正在尝试用按钮发送通知,但它不起作用,通知来了,但没有按钮 如何在通知中包含按钮? 我试过了,但没用:( 我在论坛上搜索过,但是什么都没有!有什么帮助吗 示例按钮: publicstaticvoidmain(字符串[]args){ 最后一个字符串msg=“Teste”; List gcms=new ArrayList(); gcms.添加(“c6h8rQ6v7iU:APA91BHZZZV8QUTKL3955BILFGBOQXNP-T3nP_Q3DR2Cry7TTTFL
publicstaticvoidmain(字符串[]args){
最后一个字符串msg=“Teste”;
List gcms=new ArrayList();
gcms.添加(“c6h8rQ6v7iU:APA91BHZZZV8QUTKL3955BILFGBOQXNP-T3nP_Q3DR2Cry7TTTFLWLLWL0WK_5CHGHUCL52RLXM_YJCchYsuq99mz-jHac25fAfrK5EjAESBFqj2n3zYtB-RVFk0LoF7nBly6htMtQjA2Au4VlP-9razIetPgiZuABnA”);
用于(字符串gcm:gcms){
sendPush(“MX Soluções”、msg、gcm、“go”和“);
}
}
公共静态void sendPush(最终字符串标题、最终字符串消息、最终字符串id、对象…参数){
如果(!Utils.isEmpty(id)){
ConfigUtils cfg=ConfigUtils.get();
字符串serverKey=cfg.get(“gcm.server.key”,“**********”;
if(serverKey==null){
LogUtils.info(“API密钥não encontrada,注册应用程序emhttps://developers.google.com/mobile/add?platform=android&cntapi=gcm");
返回;
}
Thread Thread=新线程(new Runnable(){
@凌驾
公开募捐{
试一试{
HttpParams myParams=新的BasicHttpParams();
HttpConnectionParams.setSoTimeout(myParams,10000);
HttpConnectionParams.setConnectionTimeout(myParams,10000);//超时
字符串url=GCM_url;
DefaultHttpClient客户端=新的DefaultHttpClient(myParams);
HttpPost=新的HttpPost(url);
BasicHttpContext localContext=新的BasicHttpContext();
JSONObject mainData=新的JSONObject();
试一试{
JSONObject数据=新的JSONObject();
data.putOpt(“文本”,消息);
数据。putOpt(“标题”,标题);
data.putOpt(“声音”、“默认”);
data.putOpt(“声音名称”、“默认值”);
JSONArray regIds=新的JSONArray();
regIds.put(id);
mainData.put(“注册ID”,regIds);
mainData.put(“通知”,数据);
mainData.put(“内容可用”,true);
mainData.put(“优先级”、“高”);
mainData.put(“声音”、“默认”);
mainData.put(“声音名称”、“默认值”);
如果(args!=null&&args.length>0){
Map toMap=Utils.toMap(args);
toMap.put(“coldstart”,真);
toMap.put(“前景”,true);
mainData.put(“数据”,toMap);
}
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
});
thread.start();
}
}
看看这个
如果您在使其工作时遇到问题,请在创建通知的位置共享您的代码
public static void main(String[] args) {
final String msg = "Teste";
List<String> gcms = new ArrayList<>();
gcms.add("c6h8rQ6v7iU:APA91bHzZV8qutkl3955bILfGbOQXnp-T3nP_q3Dr2cRy7TTTflwLL0WK_5C2HgHUCL52rlxm_YJCchYsuq99mz-jHac25fAfrK5EjAESBFqj2n3zYtB-RVFk0LoF7nBly6htMtQjA2Au4VlP-9razIetPgiZuABnA");
for (String gcm : gcms) {
sendPush("MX Soluções", msg, gcm, "go", "");
}
}
public static void sendPush(final String title, final String message, final String id, Object... args) {
if (!Utils.isEmpty(id)) {
ConfigUtils cfg = ConfigUtils.get();
String serverKey = cfg.get("gcm.server.key", "***********");
if (serverKey == null) {
LogUtils.info("API Key não encontrada, registrar app em https://developers.google.com/mobile/add?platform=android&cntapi=gcm");
return;
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(myParams, 10000);
HttpConnectionParams.setConnectionTimeout(myParams, 10000); // Timeout
String url = GCM_URL;
DefaultHttpClient client = new DefaultHttpClient(myParams);
HttpPost post = new HttpPost(url);
BasicHttpContext localContext = new BasicHttpContext();
JSONObject mainData = new JSONObject();
try {
JSONObject data = new JSONObject();
data.putOpt("text", message);
data.putOpt("title", title);
data.putOpt("sound", "default");
data.putOpt("soundname", "default");
JSONArray regIds = new JSONArray();
regIds.put(id);
mainData.put("registration_ids", regIds);
mainData.put("notification", data);
mainData.put("content_available", true);
mainData.put("priority", "high");
mainData.put("sound", "default");
mainData.put("soundname", "default");
if (args != null && args.length > 0) {
Map toMap = Utils.toMap(args);
toMap.put("coldstart", true);
toMap.put("foreground", true);
mainData.put("data", toMap);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
thread.start();
}
}