Android 极光推送(JPush)


  1. package com.seven7.mypushdome;  
  2.   
  3.   
  4. import org.json.JSONException;  
  5. import org.json.JSONObject;  
  6.   
  7.   
  8. import cn.jpush.android.api.JPushInterface;  
  9. import android.app.Notification;  
  10. import android.app.NotificationManager;  
  11. import android.content.BroadcastReceiver;  
  12. import android.content.Context;  
  13. import android.content.Intent;  
  14. import android.os.Bundle;  
  15. import android.support.v4.app.NotificationCompat;  
  16. import android.util.Log;  
  17.   
  18.   
  19. public class MyPushReceiver extends BroadcastReceiver {  
  20.   
  21.   
  22.     private static final String TAG = "JPush";  
  23.   
  24.   
  25.     public MyPushReceiver() {  
  26.           
  27.     }  
  28.   
  29.   
  30.     /** 
  31.      * @param kdkajs 
  32.      * 
  33.      */  
  34.     @Override  
  35.     public void onReceive(Context context, Intent intent) {  
  36.         Bundle bundle = intent.getExtras();  
  37.         Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction()  
  38.                 + ", extras: " + printBundle(bundle));  
  39.         // 注册ID的广播这个比较重要,因为所有的推送服务都必须,注册才可以额接收消息  
  40.         // 注册是在后台自动完成的,如果不能注册成功,那么所有的推送方法都无法正常进行  
  41.         // 这个注册的消息,可以发送给自己的业务服务器上。也就是在用户登录的时候,给自己的服务器发送  
  42.         if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {  
  43.             String regId = bundle  
  44.                     .getString(JPushInterface.EXTRA_REGISTRATION_ID);  
  45.             Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);  
  46.             // send the Registration Id to your server...  
  47.   
  48.   
  49.         } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent  
  50.                 .getAction())) {  
  51.             // 所有自定义的消息才会进入这个方法里  
  52.             Log.d(TAG,  
  53.                     "[MyReceiver] 接收到推送下来的自定义消息: "  
  54.                             + bundle.getString(JPushInterface.EXTRA_MESSAGE));  
  55.             processCustomMessage(context, bundle);  
  56.   
  57.   
  58.         } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent  
  59.                 .getAction())) {  
  60.             // 所有的普通推送,都会进入到这个部分,并且Jpush自己会进行 Notification的显示  
  61.             // 我们只要把notificationId 存起来,或者保存到本地,用于列表的排序.  
  62.             Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");  
  63.             int notifactionId = bundle  
  64.                     .getInt(JPushInterface.EXTRA_NOTIFICATION_ID);  
  65.             Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);  
  66.   
  67.   
  68.         } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent  
  69.                 .getAction())) {  
  70.   
  71.   
  72.             // notification点击打开,主要针对是普通的推送消息  
  73.             Log.d(TAG, "[MyReceiver] 用户点击打开了通知");  
  74.   
  75.   
  76.             // 打开自定义的Activity  
  77.             Intent i = new Intent(context, MainActivity.class);  
  78.             i.putExtras(bundle);  
  79.             // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  80.             i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK  
  81.                     | Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  82.             context.startActivity(i);  
  83.   
  84.   
  85.         } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent  
  86.                 .getAction())) {  
  87.             Log.d(TAG,  
  88.                     "[MyReceiver] 用户收到到RICH PUSH CALLBACK: "  
  89.                             + bundle.getString(JPushInterface.EXTRA_EXTRA));  
  90.             // 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity,  
  91.             // 打开一个网页等..  
  92.   
  93.   
  94.         } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent  
  95.                 .getAction())) {  
  96.             boolean connected = intent.getBooleanExtra(  
  97.                     JPushInterface.EXTRA_CONNECTION_CHANGE, false);  
  98.             Log.e(TAG, "[MyReceiver]" + intent.getAction()  
  99.                     + " connected state change to " + connected);  
  100.         } else {  
  101.             Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());  
  102.         }  
  103.     }  
  104.   
  105.   
  106.     // 打印所有的 intent extra 数据  
  107.     private static String printBundle(Bundle bundle) {  
  108.         StringBuilder sb = new StringBuilder();  
  109.         for (String key : bundle.keySet()) {  
  110.             if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {  
  111.                 sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));  
  112.             } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {  
  113.                 sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));  
  114.             } else {  
  115.                 sb.append("\nkey:" + key + ", value:" + bundle.getString(key));  
  116.             }  
  117.         }  
  118.         return sb.toString();  
  119.     }  
  120.   
  121.   
  122.     // send msg to MainActivity  
  123.     /** 
  124.      * 可能经常用到的一点,获取附加的自定义的字段 
  125.      *  
  126.      * @param context 
  127.      * @param bundle 
  128.      */  
  129.     private void processCustomMessage(Context context, Bundle bundle) {  
  130.         // if (MainActivity.isForeground) {//检查当前软件是否在前台  
  131.         // 利用JPushInterface.EXTRA_MESSAGE 机械能推送消息的获取  
  132.         String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);  
  133.   
  134.   
  135.         // 可能经常用到的一点,获取附加的自定义的字段、  
  136.         // 这个字符串就是Json的格式,用于自己的服务器给特定的客户端传递一些特定的属性和配置,  
  137.         // 例如显示一些数字、特定的事件,或者是访问特定的网址的时候,使用extras  
  138.         // 例如显示订单信息、特定的商品列表,特定的咨询网址  
  139.         String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);  
  140.   
  141.   
  142.         // 使用广播或者通知进行内容的显示  
  143.         NotificationCompat.Builder builder = new NotificationCompat.Builder(  
  144.                 context);  
  145.         builder.setContentText(message).setSmallIcon(R.drawable.ic_launcher);  
  146.         builder.setContentTitle("Message");  
  147.         builder.setDefaults(Notification.DEFAULT_SOUND);  
  148.   
  149.   
  150.         Log.i("Jpush", extras + "~~");  
  151.           
  152.         int drawResId=R.drawable.ic_launcher;  
  153.         int num = 0;  
  154.         String title="hello";  
  155.         int iconType=0;  
  156.           
  157.         /** 
  158.          * 自定义信息: 获取 
  159.          * */  
  160.         if (extras != null) {  
  161.             try {  
  162.                 JSONObject object = new JSONObject(extras);  
  163.                 num = object.optInt("num");  
  164.                 title=object.optString("title","hello");  
  165.                 iconType=object.optInt("iconType");  
  166.             } catch (JSONException e) {  
  167.                 // TODO Auto-generated catch block  
  168.                 e.printStackTrace();  
  169.             }  
  170.   
  171.   
  172.         }  
  173.           
  174.         builder.setContentText(title);  
  175.           
  176.         // 不同的节日不同的推送  图标  
  177.         switch (iconType) {  
  178.         case 0://推送图标1  
  179.             drawResId=R.drawable.ic_launcher;  
  180.             break;  
  181.         case 1://推送图标2  
  182.             drawResId=R.drawable.ic_launcher;  
  183.             break;  
  184.   
  185.   
  186.         default:  
  187.             break;  
  188.         }  
  189.           
  190.         builder.setSmallIcon(drawResId);  
  191.           
  192.         if (num > 0) {  
  193.             builder.setNumber(num);  
  194.         }  
  195.   
  196.   
  197.         Notification notification = builder.build();  
  198.         NotificationManager manager = (NotificationManager) context  
  199.                 .getSystemService(Context.NOTIFICATION_SERVICE);  
  200.   
  201.   
  202.         manager.notify(1, notification);  
  203.         // 使用广播或通知进行内容的显示  
  204.   
  205.   
  206.         // Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);  
  207.         // msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);  
  208.         // if (!ExampleUtil.isEmpty(extras)) {  
  209.         // try {  
  210.         // JSONObject extraJson = new JSONObject(extras);  
  211.         // if (null != extraJson && extraJson.length() > 0) {  
  212.         // msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);  
  213.         // }  
  214.         // } catch (JSONException e) {  
  215.         //  
  216.         // }  
  217.         //  
  218.         // }  
  219.         // context.sendBroadcast(msgIntent);  
  220.         // }  
  221.     }  
  222. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值