您的位置:首页 > 移动开发 > Android开发

Android记录18-集成极光推送的一点说明

2014-12-12 14:38 633 查看
Android开发记录18-集成推送服务的一点说明
关于推送服务,国内有很多选择,笔者也对它们进行了一个详细的对比,一般我们产品选择推送服务主要考量以下几个要素:
1、是否收费,如何收费?
2、推送内容是是什么(是否包含通知、消息、富媒体等等)
3、稳定性、及时性如何?
4、集成难度是否简单
5、支持平台有哪些(主流Android、IOS)
6、服务端支持语言(Java、C#、PHP、Python等)

下面笔者例举国内主要的一些推送服务:

来自Devstore的统计,共收录了国内21家推送服务,分别是(按关注度排列):

1.个推(个信互动(北京)网络科技有限公司http://www.igetui.com/

2.百度云推送(百度http://developer.baidu.com/cloud/push

3.极光推送(深圳市和讯华谷信息技术有限公司https://www.jpush.cn/

4.友盟推送(友盟http://www.umeng.com/push

5.小米推送(小米http://dev.xiaomi.com/doc/?page_id=1670

6.腾讯信鸽推送(腾讯公司http://xg.qq.com/

7.Bmob推送(广州市比目网络科技有限公司http://www.bmob.cn/

8.云巴推送(深圳市微智云科技有限公司http://www.yunba.io/

9.华为推送(华为公司http://developer.huawei.com/push

10.智游推送(北京智游网安科技有限公司http://www.zypush.com/

11.***云推送(***网络http://www.grandcloud.cn/product/push

12.原子推送(原子技术有限公司http://www.atom14.com/

13.魔桥推送(魔桥http://www.mobbridge.com/

14.魔泊网推送(魔泊网http://helpdocs.sturgeon.mopaas.com/helpdocs/_push.html

15.有推推送(中国移动通信http://dev.10086.cn/aoi/index.jsp

16.WeCloud(WeCloud http://www.wecloud.io/

17.Learn Cloud推送(美味书签信息技术有限公司https://cn.avoscloud.com/

18.亚马逊推送(亚马逊公司http://aws.amazon.com/cn/sns/

19.魔方推送(魔方公司http://www.imofan.com/

20.语盒团推送(语盒团公司http://www.yuchteam.com/

21.移动消息推送

开发者可以针对产品的需求,来对比选择适合自己应用的推送服务。

笔者这里选择了“极光推送”,它是部分收费的,收费模式各位可以到官网查看;支持推送的内容有通知、消息、富媒体,稳定性好、能及时到达、提供服务API、支持Android、iOS平台,服务端支持Java、PHP、Python、C#、Ruby、Node.js。

集成极光推送笔者这里也不详细写,主要提几点:
1、使用Portal来进行测试
Portal是服务提供的传送门,我们可以使用控制台来进行推送测试,实际应用时一般是根据推送服务提供的服务端API来实现定制推送。
极光Portal如下:



2、通知与消息的区别
通知就是可以再通知栏显示提醒用户的信息,而消息不会在通知栏显示,业务逻辑可以完全有开发者来定。



3、推送对象
可以分为:



广播:会把通知无区别的推送到每个人身上。
设置标签:这一般用于群组推送。
设置别名:适用于单播,根据客户端设置的别名来推送。
设置注册ID:适用于单播推送,指定推送给某一个人,可以使注册过的用户ID,主要用来区分。

4、自定义通知栏样式、附加字段的作用
我们有时候可能不想直接用Android原生的通知栏样式,如果服务提供相应的API的话,我们可以通过自定义布局来达到这个目的。极光这里提供了以下方法:

// 自定义Notification样式
		CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
				getApplicationContext(),
				R.layout.customer_notitfication_layout, R.id.icon, R.id.title,
				R.id.text);
		builder.layoutIconDrawable = R.drawable.ic_launcher;
		builder.developerArg0 = "developerArg2";

		JPushInterface.setPushNotificationBuilder(2, builder);
		Toast.makeText(getApplicationContext(), "Custom Builder - 2",
				Toast.LENGTH_SHORT).show();




我们只需要指定通知栏编号,下次推送通知的时候就会以自定义的通知栏样式来显示。

这里还有一个附加字段,我们有时候可能需要根据推送的不同消息来实现跳转不同的页面,这时候就可能需要用到附加字段了,我们在Broadcast Receiver来接受推送下来的消息,解析附加字段内容,来达到我们的目的。
代码示例:

package com.infzm.daily.know.receiver;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import cn.jpush.android.api.JPushInterface;

import com.infzm.daily.know.ArticleDetailActivity;
import com.infzm.daily.know.MainActivity;
import com.infzm.daily.know.utils.LogUtils;

public class PushReceiver extends BroadcastReceiver {
	private static final String TAG = "JPush";
	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		LogUtils.logi(TAG, "[PushReceiver] onReceive - " + intent.getAction() + ", extras: "+ printBundle(bundle)); 
		
		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			LogUtils.logi(TAG, "[PushReceiver] 接收Registeration Id : " + regId);
		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
			LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
		}else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
			LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的通知的ID: " + notifactionId);
        	
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
        	LogUtils.logi(TAG, "[PushReceiver] 用户点击打开了通知");
        	
        	String type = bundle.getString(JPushInterface.EXTRA_EXTRA);
        	LogUtils.loge(TAG, "type:" + type);
        	try {
				JSONObject jsonObject = new JSONObject(type);
				String str = jsonObject.getString("key");
				if (str.equals("1")) {
					//打开自定义的Activity
		        	Intent i = new Intent(context, MainActivity.class);
		        	bundle.putInt("index", 1);
		        	i.putExtras(bundle);
		        	
		        	//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		        	i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
		        	context.startActivity(i);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
            
        	
        	
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
        	LogUtils.logi(TAG, "[PushReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
        	
        } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
        	boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
        	LogUtils.logi(TAG, "[PushReceiver]" + intent.getAction() +" connected state change to "+connected);
        } else {
        	LogUtils.logi(TAG, "[PushReceiver] Unhandled intent - " + intent.getAction());
        }
	}
	
	// 打印所有的 intent extra 数据
	private static String printBundle(Bundle bundle) {
		StringBuilder sb = new StringBuilder();
		for (String key : bundle.keySet()) {
			if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
			}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
				sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
			} 
			else {
				sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
			}
		}
		return sb.toString();
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: