android极光推送

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的话,我们可以通过自定义布局来达到这个目的。极光这里提供了以下方法:

  1. // 自定义Notification样式  
  2.     CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(  
  3.             getApplicationContext(),  
  4.             R.layout.customer_notitfication_layout, R.id.icon, R.id.title,  
  5.             R.id.text);  
  6.     builder.layoutIconDrawable = R.drawable.ic_launcher;  
  7.     builder.developerArg0 = "developerArg2";  
  8.   
  9.     JPushInterface.setPushNotificationBuilder(2, builder);  
  10.     Toast.makeText(getApplicationContext(), "Custom Builder - 2",  
  11.             Toast.LENGTH_SHORT).show();  


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

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

代码示例:

  1. package com.infzm.daily.know.receiver;  
  2.   
  3. import org.json.JSONException;  
  4. import org.json.JSONObject;  
  5.   
  6. import android.content.BroadcastReceiver;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10. import cn.jpush.android.api.JPushInterface;  
  11.   
  12. import com.infzm.daily.know.ArticleDetailActivity;  
  13. import com.infzm.daily.know.MainActivity;  
  14. import com.infzm.daily.know.utils.LogUtils;  
  15.   
  16. public class PushReceiver extends BroadcastReceiver {  
  17.     private static final String TAG = "JPush";  
  18.     @Override  
  19.     public void onReceive(Context context, Intent intent) {  
  20.         Bundle bundle = intent.getExtras();  
  21.         LogUtils.logi(TAG, "[PushReceiver] onReceive - " + intent.getAction() + ", extras: "+ printBundle(bundle));   
  22.           
  23.         if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {  
  24.             String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);  
  25.             LogUtils.logi(TAG, "[PushReceiver] 接收Registeration Id : " + regId);  
  26.         } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {  
  27.             LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));  
  28.         }else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {  
  29.             LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的通知");  
  30.             int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);  
  31.             LogUtils.logi(TAG, "[PushReceiver] 接收到推送下来的通知的ID: " + notifactionId);  
  32.               
  33.         } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {  
  34.             LogUtils.logi(TAG, "[PushReceiver] 用户点击打开了通知");  
  35.               
  36.             String type = bundle.getString(JPushInterface.EXTRA_EXTRA);  
  37.             LogUtils.loge(TAG, "type:" + type);  
  38.             try {  
  39.                 JSONObject jsonObject = new JSONObject(type);  
  40.                 String str = jsonObject.getString("key");  
  41.                 if (str.equals("1")) {  
  42.                     //打开自定义的Activity  
  43.                     Intent i = new Intent(context, MainActivity.class);  
  44.                     bundle.putInt("index", 1);  
  45.                     i.putExtras(bundle);  
  46.                       
  47.                     //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  48.                     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );  
  49.                     context.startActivity(i);  
  50.                 }  
  51.             } catch (JSONException e) {  
  52.                 e.printStackTrace();  
  53.             }  
  54.               
  55.               
  56.               
  57.         } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {  
  58.             LogUtils.logi(TAG, "[PushReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));  
  59.             //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..  
  60.               
  61.         } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {  
  62.             boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);  
  63.             LogUtils.logi(TAG, "[PushReceiver]" + intent.getAction() +" connected state change to "+connected);  
  64.         } else {  
  65.             LogUtils.logi(TAG, "[PushReceiver] Unhandled intent - " + intent.getAction());  
  66.         }  
  67.     }  
  68.       
  69.     // 打印所有的 intent extra 数据  
  70.     private static String printBundle(Bundle bundle) {  
  71.         StringBuilder sb = new StringBuilder();  
  72.         for (String key : bundle.keySet()) {  
  73.             if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {  
  74.                 sb.append(" key:" + key + ", value:" + bundle.getInt(key));  
  75.             }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){  
  76.                 sb.append(" key:" + key + ", value:" + bundle.getBoolean(key));  
  77.             }   
  78.             else {  
  79.                 sb.append(" key:" + key + ", value:" + bundle.getString(key));  
  80.             }  
  81.         }  
  82.         return sb.toString();  
  83.     }  
  84.   
  85. }  
 
 
http://blog.csdn.net/wwj_748/article/details/41867467
原文地址:https://www.cnblogs.com/lihaibo-Leao/p/5123327.html