Jpush 自定义推送声音

来源:互联网 发布:外卖快餐软件 编辑:程序博客网 时间:2024/09/21 06:21

最近的项目需要自定义推送声音和图标(Jpush默认图标是app的logo,可能会因为你存放logo的时候没有是配好,所以推送的图标会裁剪),然而极光的文档,说得模凌两可的,弄了半天才弄好,因此记录一下。

极光推送包含有通知与自定义消息两种类型的推送。因此可以发送三种方式的推送:

  1. 通知
  2. 自定义消息
  3. 通知 + 自定义消息
要实现自定义推送声音,需要使用第二种方式。

代码实现

1.在配置文件里面注册自定义的广播

<receiver    android:name=".receiver.JpushReceiver"    android:enabled="true"    android:exported="false">    <intent-filter>        <action android:name="cn.jpush.android.intent.REGISTRATION"/>        <!-- Required  用户注册SDKintent -->        <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED"/>        <!-- Required  用户接收SDK消息的intent -->        <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED"/>        <!-- Required  用户接收SDK通知栏信息的intent -->        <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED"/>        <!-- Required  用户打开自定义通知栏的intent -->        <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK"/>        <!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->        <action android:name="cn.jpush.android.intent.CONNECTION"/>        <!-- 接收网络变化 连接/断开 since 1.6.3 -->        <category android:name="com.lanbaoapp.propertyuser"/>    </intent-filter></receiver>

2.代码

public class JpushReceiver extends BroadcastReceiver {    private static final String TAG = "JPush";    @Override    public void onReceive(Context context, Intent intent) {        Bundle bundle = intent.getExtras();        LogUtils.d(TAG, "[JPush] D级别log");        LogUtils.e("test", "intent.getAction(): " + intent.getAction());        LogUtils.e("test", "JPushInterface.ACTION_REGISTRATION_ID: " + JPushInterface.ACTION_REGISTRATION_ID);        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {            /*             *  SDK  JPush Server 注册所得到的注册 ID              *  一般来说,可不处理此广播信息。             *  要深入地集成极光推送,开发者想要自己保存App用户与JPush 用户关系时,则接受此广播             *  ,取得 Registration ID 并保存与App uid 的关系到开发者自己的应用服务器上。             */            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);            String registId = JPushInterface.getRegistrationID(context);            LogUtils.e(TAG, "[JPush] regId: " + regId);            LogUtils.e(TAG, "[JPush] registId: " + registId);            SPUtils.setSP(context, AppConstants.REGISTER_ID, registId);        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {            /*             * 收到了自定义消息 Push              * SDK 对自定义消息,只是传递,不会有任何界面上的展示。             */            LogUtils.e(TAG, "[JPush] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));            processCustomMessage(context,bundle);        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {            /*             * 收到了通知 Push             * 如果通知的内容为空,则在通知栏上不会展示通知。             * 但是,这个广播 Intent 还是会有。开发者可以取到通知内容外的其他信息。             * 这里可以做些统计,或者做些其他工作             */            LogUtils.e(TAG, "[JPush] 接收到推送下来的通知");            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);            String content = bundle.getString(JPushInterface.EXTRA_ALERT);            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);            int id = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);            String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);            String fileHtml = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_PATH);            LogUtils.e(TAG, "[JPush] 接收到推送下来的通知标题: " + title);            LogUtils.e(TAG, "[JPush] 接收到推送下来的通知内容: " + content);            LogUtils.e(TAG, "[JPush] 接收到推送下来的附加字段(Json字符串): " + extras);            LogUtils.e(TAG, "[JPush] 接收到推送下来的通知的ID: " + id);            LogUtils.e(TAG, "[JPush] 接收到推送下来的通知的内容类型: " + type);            LogUtils.e(TAG, "[JPush] 接收到推送下载的HTML的文件路径,用于展现WebView: " + fileHtml);        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {            /*             * 用户点击了通知。             */            LogUtils.e(TAG, "[JPush] 用户点击打开了通知");            // 上报通知栏被打开            JPushInterface.reportNotificationOpened(context, bundle.getString(JPushInterface.EXTRA_MSG_ID));            // 获取附加字段            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);            LogUtils.e("test", "extras :" + extras);            Intent toIntent = new Intent();            toIntent.setClass(context, MainActivity.class);            toIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            toIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);            context.startActivity(toIntent);        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {            /*             * 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码             * 比如打开新的Activity, 打开一个网页等..             */            LogUtils.e(TAG, "[JPush] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);            LogUtils.w(TAG, "[JPush]" + intent.getAction() + " connected state change to " + connected);        } else {            LogUtils.e(TAG, "[JPush] Unhandled intent - " + intent.getAction());        }    }    /**     * 实现自定义推送声音     * @param context     * @param bundle     */    private void processCustomMessage(Context context, Bundle bundle) {        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.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);        if (!TextUtils.isEmpty(extras)) {            try {                JSONObject extraJson = new JSONObject(extras);                if (null != extraJson && extraJson.length() > 0) {                    String sound = extraJson.getString("sound");                    if("test.mp3".equals(sound)){                        notification.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" +R.raw.test));                    }                }            } catch (JSONException e) {            } catch (JSONException e) {                e.printStackTrace();            }        }        Intent mIntent = new Intent(context,MainActivity.class);        mIntent.putExtras(bundle);        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);        notification.setContentIntent(pendingIntent);        notificationManager.notify(2, notification.build());    }}


0 0
原创粉丝点击