第一篇博客:极光推送以及自定义推送声音

极光注册就不说了,首先配置App的build.grandle

在defaultConfig里粘贴一下内容appkey为我在极光注册之后的Appkey

第一篇博客:极光推送以及自定义推送声音_第1张图片

然后配置AndroidManifest  把下面这些信息放在application里面


        
        
        
            
                
                
                
                
            
        

        
        

        
        
        
            
                
                
            
        

        
        
            
                

                
            
            
                
                
            
            
            
                
                

                
            
        

        
        
            
                

                
                
            
        
        
        
            
                
                
            
        

        
        

        
        

        
        
        
        
            
                

                
            
        

        
        
            

                
                
                
                
                
                
                
                
                
                

                
            
        

然后在项目的根目录创建一个jpush的包在里面创建五个类

第一篇博客:极光推送以及自定义推送声音_第2张图片

首先来说MyJpushMessageReceiver

/**
 * 自定义JPush message 接收器,包括操作tag/alias的结果返回(仅仅包含tag/alias新接口部分)
 * */
public class MyJPushMessageReceiver extends JPushMessageReceiver {

    @Override
    public void onTagOperatorResult(Context context,JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onTagOperatorResult(context,jPushMessage);
        super.onTagOperatorResult(context, jPushMessage);
    }
    @Override
    public void onCheckTagOperatorResult(Context context,JPushMessage jPushMessage){
        TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context,jPushMessage);
        super.onCheckTagOperatorResult(context, jPushMessage);
    }
    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context,jPushMessage);
        super.onAliasOperatorResult(context, jPushMessage);
    }
}
然后是自定义的接收器Myreciever
**
 * 自定义接收器
 * 

* 如果不定义这个 Receiver,则: * 1) 默认用户会打开主界面 * 2) 接收不到自定义消息 */ public class MyReceiver extends BroadcastReceiver { private static final String TAG = "tag"; @Override public void onReceive(Context context, Intent intent) { try { Bundle bundle = intent.getExtras(); Logger.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); Logger.e(TAG, "[MyReceiver] 接收Registration Id : " + regId); } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); //processCustomMessage(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知"); int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); //bundle.get(JPushInterface.EXTRA_ALERT);推送内容 Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId); //获取到的推送的通知类型 String str_Test = bundle.getString(JPushInterface.EXTRA_ALERT); Log.e("tag", "测试:" + str_Test); //在这里自定义通知声音 processCustomMessage(context,bundle); //这里通过EventBus来想我需要更新数据的界面发送更新通知 EventBus.getDefault().postSticky(new MessageEvent(str_Test)); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 用户点击打开了通知"); } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { Logger.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); Logger.w(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected); } else { Logger.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction()); } } catch (Exception e) { } } // 打印所有的 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 (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) { Logger.i(TAG, "This message has no Extra data"); continue; } try { JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA)); Iterator it = json.keys(); while (it.hasNext()) { String myKey = it.next(); sb.append("\nkey:" + key + ", value: [" + myKey + " - " + json.optString(myKey) + "]"); } } catch (JSONException e) { Logger.e(TAG, "Get message extra JSON error!"); } } else { sb.append("\nkey:" + key + ", value:" + bundle.getString(key)); } } return sb.toString(); } /** * 自定义推送的声音 * @param context * @param bundle */ private void processCustomMessage(Context context, Bundle bundle) { NotificationCompat.Builder notification = new NotificationCompat.Builder(context); //这一步必须要有而且setSmallIcon也必须要,没有就会设置自定义声音不成功 notification.setAutoCancel(true).setSmallIcon(R.mipmap.logo1); String alert = bundle.getString(JPushInterface.EXTRA_ALERT); if (alert!=null&&!alert.equals("")){ notification.setSound( Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.notification)); } //最后刷新notification是必须的 NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(1,notification.build()); }

另外三个ExampleUtil, Logger,TagAliasOperatorHelper为辅助类极光官方提供。
做完这些之后推送还是不能推送到你的手机,因为你没有把你的设备号给到后台,所以登陆的是要做处理
获取本地设备号然后请求极光提供的方法setJupshAlisa方法
 //获取手机设备号
TelephonyManager tm = (TelephonyManager) LoginActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
SetJuPshAlias(deviceId);
 /**
     * 向极光绑定别名(后台必须要根据注册在极光的别名进行个推)
     */
    /**
     * 设置、覆盖签名(可以自定逻辑判断(如果设置过了就不需要在设置)
     * 

* String Myalias:别名(和后台商定(例如使用Uid)) */ public static final int ACTION_SET = 2; public void SetJuPshAlias(String Myalias) { TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean(); tagAliasBean.isAliasAction = true; tagAliasBean.action = ACTION_SET; tagAliasBean.alias = Myalias; //TagAliasOperatorHelper.getInstance().handleAction(getActivity(),sequence,tagAliasBean); JPushInterface.setAlias(this, sequence, tagAliasBean.alias); regId = JPushInterface.getRegistrationID(this); Log.e("tag", "极光设备号:"+regId); if (regId!=null&&!regId.equals("")){ btnlogin.setOnClickListener(this); }else { showToast("设备号为空,请重新登录"); userName.setText(""); passward.setText(""); } }

得到极光返回的设备号之后就可以登录啦

你可能感兴趣的:(极光推送)