jpush自定义推送声音

来源:互联网 发布:luastudio for mac 编辑:程序博客网 时间:2024/06/18 03:34
public class MyReceiver extends BroadcastReceiver {    public static final String TAG = "Jpush";    @Override    public void onReceive(Context context, Intent intent) {        Bundle bundle = intent.getExtras();//        Log.e(TAG, "收到的所有信息----->>>> " + intent.getAction() + ", extras: " + printBundle(bundle)+"<<<<<---------");        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);            Log.e(TAG, "[MyReceiver] 接收Registration Id : " + regId);            //保存ID到服务器        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {//自定义消息(需要自定义notifition,或者其他逻辑处理)        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {//收到了通知(自带notifition)            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);            Intent toOrderActivity = new Intent();            toOrderActivity.setAction(OrderActivity.BROADCAST_ACTION);            toOrderActivity.putExtra("msg", "isBroad");            context.sendBroadcast(toOrderActivity);            Log.e("guangbo",toOrderActivity.getStringExtra("msg"));//            Toast.makeText(CustomApplication.getContext(),"[MyReceiver] 接收到推送下来的通知:"+bundle.getString(JPushInterface.EXTRA_TITLE),Toast.LENGTH_SHORT);            Log.e(TAG, "[MyReceiver] 接收到推送下来的通知: " + bundle.getString(JPushInterface.EXTRA_TITLE));            Log.e(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);            processCustomMessage(context,bundle);  } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {//点击了通知消息            boolean isAlive = (boolean) SharedPreferencesUtils.getParam(CustomApplication.getContext(), "isAlive", false);            String extra = (String) bundle.get(JPushInterface.EXTRA_EXTRA);//            Log.e(TAG, "[MyReceiver] 用户点击打开了通知"+extra);            try {                JSONObject dataJson = new JSONObject(extra);                String pushFlag = dataJson.optString("push_flag");                if (!TextUtils.isEmpty(pushFlag)) {                    if (!pushFlag.equals("1")) {                        SharePreferecesUtils_MyWork.setParam(CustomApplication.getContext(), "qiehuan", 2);                    } else {                        SharePreferecesUtils_MyWork.setParam(CustomApplication.getContext(), "qiehuan", 1);                    }                } else {                    SharePreferecesUtils_MyWork.setParam(CustomApplication.getContext(), "qiehuan", 1);                }                if (isAlive) {                    Intent intent1 = new Intent(context, MainActivity.class);                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                    context.startActivity(intent1);                } else {                    Intent intent1 = new Intent(context, LoginActivity.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.e(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.e(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);        } else {            Log.e(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());        }    }    private void processCustomMessage(Context context,Bundle bundle) {        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);        NotificationCompat.Builder notification = new NotificationCompat.Builder(context);        notification.setAutoCancel(true)                .setContentText("自定义推送声音")                .setContentTitle("极光测试")                .setSmallIcon(R.mipmap.ic_launcher);        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
//获取音频文件路径,(在res文件夹下面创建一个raw文件夹,将需要的音频文件放进raw里面)        notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.test));        Intent mIntent = new Intent(context,OrderActivity.class);        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);        notification.setContentIntent(pendingIntent);        notificationManager.notify(2, notification.build()); }}
参考博客链接:http://www.jianshu.com/p/8e15ae0909d2
0 0