Microsoft azure Notification Hubs from Java

项目中用到微软的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);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值