成功的向android推送信息——android推技术!

   这个假期一直都在为齐鲁软件大赛做准备,其中一个很重要的技术就是推技术,请教了好多老师和高手,也看到了好多好多的协议,xmqq呀,还有好多好多协议,但都没有实现,后来只能用socket,想建立持久通道的方法去发送信息,虽然很成功的android端传送过去了,但服务器的负载太大了,也是一个bug吧,本来想这样过去的但还是四方打听技术,知道看到了一个帖子,关于真正的android,push技术,现粘过来了,亲测可用,不懂的留言。不过api时间太短了,毕竟交上作品时已经过期了,嘿嘿。

   

a) 在应用中添加 APNS 功能

  1. 下载 libaray: com_apns.jar
  2. 将com_apns.jar添加到工程

    在工程上右键打开“属性”,选择 “Java Build Path”, 在 Libraries 中选择 “Add External JARs”, 选择下载的 com_apns.jar.

  3. 接收 push notification
    使用BroadcastReceiver接收系统广播:
    
    public class MyBroadcastReceiver extends BroadcastReceiver {
    	@Override
    	public void onReceive(Context context, Intent intent) {
    	     if (intent.getAction().equals(APNService.ON_NOTIFICATION)) {
    			String str = intent.getStringExtra("data");
    			//todo, 处理收到的消息
    		 } 
    	}
    }
    
  4. 启动 Push Notification Service
    发送Intent 启动服务,将 chanel Id 以及 此设备的标识 (chanel中唯一表示此设备的字符串) 传递过去:
     
        Intent intent = new Intent(APNService.START);
        intent.putExtra("ch", chanel);
        intent.putExtra("devId", devId);
        startService(intent);
    
    Notes Chanel Id 在申请 API 后,登录开发者页面会看到. devId: chanel 内设备标识,要在chanel内保持唯一.
  5. 配置 AndroidManifest.xml
    ...
    <application android:icon="@drawable/icon" 
    	   ... 
    	 <service android:name="com.apns.APNSService" android:label="APNS">
             <intent-filter>
                 <action android:name="com.apns.APNService.START" />
                 <action android:name="com.apns.APNService.STOP" />
                 <category android:name="android.intent.category.DEFAULT"/>
             </intent-filter>
    	 </service>
    	 <receiver android:name="MyBroadcastReceiver">
    	      <intent-filter>
    	          <action android:name="com.apnsd.APNService.NOTIFICATION" />
    	      </intent-filter>
    	 </receiver>	
    </application>	
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    ...
    


b) 发送 Notification 到设备

通过 Rect 接口发送 Notification:
http://www.push-notification.mobi/apns_v1.php?ch=YourChannelId&devId=xxxxx&msg =”hello world”&random=0123&hash=HashCode

ch:Channel Id
devId:接收设备 Id
msg:消息
random:随机数
hash:md5(ch + devId + random + privateKey)
Notes 申请API后,可登录开发者页面使用测试控制台进行API测试.

你可能感兴趣的:(android,api,service,Random,action,NetWork)