Jpush 自定义推送声音
来源:互联网 发布:外卖快餐软件 编辑:程序博客网 时间:2024/09/21 06:21
最近的项目需要自定义推送声音和图标(Jpush默认图标是app的logo,可能会因为你存放logo的时候没有是配好,所以推送的图标会裁剪),然而极光的文档,说得模凌两可的,弄了半天才弄好,因此记录一下。
极光推送包含有通知与自定义消息两种类型的推送。因此可以发送三种方式的推送:
- 通知
- 自定义消息
- 通知 + 自定义消息
代码实现
1.在配置文件里面注册自定义的广播
<receiver android:name=".receiver.JpushReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTRATION"/> <!-- Required 用户注册SDK的intent --> <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
- jpush自定义推送声音
- Jpush 自定义推送声音
- Jpush 自定义推送声音
- ios 自定义推送声音
- 设置推送自定义声音
- 极光推送JPush---自定义提示音
- jpush推送
- jpush 推送
- JPush推送
- 开发笔记 - 自定义推送声音
- 推送通知自定义提示声音
- iOS 本地推送以及自定义推送声音
- 极光推送(JPush)通知与自定义消息的区别
- iOS开发之极光推送自定义推送声音
- 极光推送JPush
- 试用JPush极光推送
- JPUSH andoid推送开发
- JPush 服务器消息推送
- 【Java基础】泛型
- No mapping found for HTTP request with URI [/ss1/] in DispatcherServlet with (SSM)
- \r\n,\n,\r的历史及在不同系统下的区别
- druid StatViewServlet配置 内置web页面
- 每日一题 No.30 简单的背包问题
- Jpush 自定义推送声音
- Mybatis入门程序
- 代码重构(三):数据重构规则
- spring源码解读:aware接口
- 因式分解结合最近邻:多层面的协同过滤模型
- Android中定时任务的实现方法
- 做项目是抢,做互联网是拼。 一个是跟对手抢,一个是自己跟自己比拼。
- Multiple methods named 'count:' found with mismatched result, parameter type or attributes
- tp5分页样式整理