项目中用到微软的azure 作为给ios和安卓推送,因此看了下:
1.引入对应的maven依赖
<dependency> <groupId>com.windowsazure</groupId> <artifactId>NotificationHubs</artifactId> <version>0.0.5</version> </dependency>2.写个测试类
package com.message.push.azure;
import com.message.inbox.common.LogHelper;
import com.message.inbox.iospush.PushEntity;
import com.message.push.fcm.FCMUtil;
import com.windowsazure.messaging.Notification;
import com.windowsazure.messaging.NotificationHub;
import com.windowsazure.messaging.NotificationOutcome;
import java.util.Set;
public class AzureService {
private static String CONNECTION = "Endpoint=sb://iovsanotificationhubns.servicebus.windows.net/;SharedAccessKeyName=Default;SharedAccessKey=Fp9e9Uy0tjyJUJmlz8FIrcTDyDc=";
private static String HUBPATH="iovsanotificationhub";
private volatile static NotificationHub hub = null;
public static NotificationHub getNotificationHubInstance()
{
if(hub == null ) {
synchronized(NotificationHub.class) {
if(hub == null)
hub = new NotificationHub(CONNECTION, HUBPATH);
}
}
return hub;
}
public static void pushNotificationToAndroid(,Set<String> tags) throws Exception {
try {
NotificationHub hub = AzureService.getNotificationHubInstance();
String message ="{\"data\":{\"message\":\" 1122llll from Java!\"}}";
System.out.println("messageId:" + pushEntity.getMessageId());
Notification n = Notification.createGcmNotification(message );
NotificationOutcome o = hub.sendNotification(n,tags);
System.out.println("endAndroid:");
} catch (Exception e) {
e.printStackTrace();
}finally
{
}
}
public static void pushNotificationToIOS(Set<String> tags) throws Exception {
try {
NotificationHub hub = AzureService.getNotificationHubInstance();
String jsonRequest= "{\"aps\":{\"alert\":\"Hello from Java!\"}}";
Notification n = Notification.createAppleNotification(jsonRequest);
NotificationOutcome o = hub.sendNotification(n,tags);
System.out.println("endIOS:");
} catch (Exception e) {
e.printStackTrace();
}finally
{
}
}
public static void main(String[] args) {
Set<String> set =new HashSet<String>();
set.add("test");//对应要发送的手机端注册的tag
pushNotificationToIOS(set);
}