我目前正在开发在服务器上的WebLogic上运行的Java web服务。这些web服务由另一个团队开发的移动应用程序调用。我需要向移动应用程序发送推送通知。我目前的开发不需要我做任何移动开发,因为我只做服务器端开发。我也没有移动开发的经验。如何开发对两个Android设备的推送通知?
谢谢。
发布于 2016-05-09 13:56:06
GCM应用程序的前提条件
进行通信
如果您对GCM有清晰的概念,请访问here。
您的托管服务器不需要任何Ip/主机名来发送消息,因为此消息将通过com.google.android.gcm.server.Sender类进行深思熟虑。这个类将在内部与GCM服务器通信。您正在使用以下方式配置此类:
Sender sender = new Sender(API_KEY);您可以使用GCM服务器发送消息。此代码将用于向设备发送推送通知。这可以从java服务器向任何Android/IOS应用程序发送通知。
Here是这个GCM服务器库的jar。
在这里,这个构造函数获取Message和deviceGcmId,其中必须发送推送消息。
public PushNotification(String id, String message) {
this.receiverID = id;
this.gcmMessage = message;
}以下是示例代码:
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
public class PushNotification {
private String receiverID;
private String gcmMessage;
String MESSAGE_KEY = "YOUR_MESSAGE_KEY";
String API_KEY = "YOUR_API_KEY";
Result result;
Sender sender;
Message message;
MulticastResult multicastResult;
public PushNotification() {
}
public PushNotification(String id, String message) {
this.receiverID = id;
this.gcmMessage = message;
}
public boolean sendSinglePushNotification() {
try {
sender = new Sender(API_KEY);
message = new Message.Builder().timeToLive(30).addData(MESSAGE_KEY, gcmMessage).build();
result = sender.send(message, receiverID, 1);
if (result != null && result.getErrorCodeName() == null) {
return true;
}
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
return false;
}
}您可以使用以下方式调用此类:
PushNotification notification=new PushNotification("DEVICE_GCM_ID","MESSAGE HAVE To SEND");
notification.sendSinglePushNotification();就是这样:)
发布于 2016-05-09 11:21:47
如果你想在多个平台上发送推送通知,使用Parse.com,但这里的问题是Parse.com将于2017年1月28日停止其服务。你有像urban airship这样的替代品,还有更多。我个人建议使用城市飞艇。
发布于 2016-05-09 11:38:55
您可以尝试使用来自Jersey的Sever Sent Event(SSE),看看是否满足您的要求。
https://stackoverflow.com/questions/37107088
复制相似问题