极光推送

极光推送基础配置可以直接查看官方文档,按照步骤走下来还是比较简单的,我这里重点是说一下别名通知。

前提是极光推送都已集成好了,并能接收到普通通知。

1.我们需要自定义一个广播,用来接收推送过来的消息。

/**
 * 极光推送的
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver,则:
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
   private static final String TAG = "JPush";

   @Override
   public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
      Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
      
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...
                        
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
           Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
//         processCustomMessage(context, bundle);
        
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
           
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
         String extra=bundle.getString(JPushInterface.EXTRA_EXTRA);
         Log.d(TAG, "下拉 点击通知栏后" + extra);


         try {
            JSONObject js=new JSONObject(extra.toString());
            String data = js.getString("message");
            JSONObject js2=new JSONObject(data.toString());
            Log.d(TAG, "xxxxxxxxxxxxx"+ data);
            String type = js2.optString("type");
            String content = js2.optString("content");
            String title = js2.optString("title");
            String extinfo = js2.getString("extinfo");
            Boolean share=false;
            Intent intent1;
	    //接下来是根据type的不同类型去做相应的操作
            if( "H5".equals(type)){
               if(!StringUtils.isEmptyString(content)){
                  if (!StringUtils.isEmptyString(extinfo)&&!"null".equals(extinfo)){
                     JSONObject js1=new JSONObject(extinfo);
                     share = js1.getBoolean("share");
                     if (share){//有分享的webview
                        intent1 = new Intent(context, IndexWebActivity.class);
                        intent1.putExtra("url",content);
                        intent1.putExtra("title", title);
                        intent1.putExtra("share", share);
                        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent1);
                     }else {
                        intent1 = new Intent(context, IndexWebActivity.class);
                        intent1.putExtra("url",content);
                        intent1.putExtra("title", title);
                        intent1.putExtra("share", share);
                        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent1);
                     }
                  }else{
                     intent1 = new Intent(context, IndexWebActivity.class);
                     intent1.putExtra("url",content);
                     intent1.putExtra("title", title);
                     intent1.putExtra("share", false);
                     intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     context.startActivity(intent1);
                  }
               }else{
                  intent1 = new Intent(context, IndexWebActivity.class);
                  intent1.putExtra("url",content);
                  intent1.putExtra("title", title);
                  intent1.putExtra("share", false);
                  intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(intent1);
               }


            }else if("APP".equals(type)){
               if (!StringUtils.isEmptyString(extinfo)&&!"null".equals(extinfo)){
                  JSONObject js1=new JSONObject(extinfo);
                  String type1 = js1.getString("type");
                  if ("BENEFIT".equals(type1)){//有分享的webview
                     intent1=new Intent(context, IndexWebActivity.class);
                     intent1= new Intent(context,FavorableAct.class);
                     intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     intent1.putExtra("frommine", "frommine");
                     context.startActivity(intent1);
                  }else if("PRIVATE_MSG".equals(type1)){
                     //跳到私信列表
                     intent1= new Intent(context,MessageAct.class);
                     intent1.putExtra("isLetterFragment",true);//true 代表切换为LetterFragment
                     intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     context.startActivity(intent1);
                  }

                  else

                  {
                     intent1= new Intent(context,MainActivity.class);
                     intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     context.startActivity(intent1);
                  }
               }else{
                  intent1= new Intent(context,MainActivity.class);
                  intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(intent1);
               }
            }else{
               intent1= new Intent(context,MainActivity.class);
               intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               context.startActivity(intent1);
            }

         } catch (JSONException e) {
            e.printStackTrace();
         }
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用户收到到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);
           Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
        } else {
           Log.d(TAG, "[MyReceiver] 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 if (key.equals(JPushInterface.EXTRA_EXTRA)) {
            if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
               Log.i(TAG, "This message has no Extra data");
               continue;
            }

            try {
               JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
               Iterator<String> it =  json.keys();

               while (it.hasNext()) {
                  String myKey = it.next().toString();
                  sb.append("\nkey:" + key + ", value: [" +
                        myKey + " - " +json.optString(myKey) + "]");
               }
            } catch (JSONException e) {
               Log.e(TAG, "Get message extra JSON error!");
            }

         } else {
            sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
         }
      }
      return sb.toString();
   }

}
2.在manifest中注册自定义的广播

<receiver
    android:name=".service.MyReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="cn.jpush.android.intent.REGISTRATION" />
        <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
        <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
        <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
        <category android:name="cn.doolii.usershopping" />
    </intent-filter>
</receiver>
3.在app第一个启动页初始化极光推送

JPushInterface.setDebugMode(true);
JPushInterface.init(this);

4.在任意activity中定义handler将别名发送给极光推送

private final Handler mHandler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
      super.handleMessage(msg);
      switch (msg.what) {
         case MSG_SET_ALIAS:
            //此处是将别名发送给极光推送,极光推送根据这个别名来进行相应的推送
            JPushInterface.setAliasAndTags(getApplicationContext(), (String) msg.obj, null, mAliasCallback);
            break;
      }
   }
};

mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, "保证此处是唯一的标识"));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值