发布网友
共1个回答
热心网友
JPush SDK 收到推送,通过广播的方式,转发给开发者App,这样开发者就可以灵活地进行处理。这个动作不是必须的。用户有需要才定义 Receiver 类来处理 SDK过来的广播。如果不做这个动作,即不写自定义 Receiver,也不在 AndroidManifest.xml 里配置这个 Receiver,则默认的行为是:接收到推送的自定义消息,则没有被处理可以正常收到通知,用户点击打开应用主界面接受广播如果全部类型的广播都接收,则需要在 AndroidManifest.xml 里添加如下的配置信息:<receiver android:name="Your Receiver" 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="You package Name" /> </intent-filter></receiver>每个 Receiver action 详细解释如下。Action - cn.jpush.android.intent.REGISTRATIONSDK 向 JPush Server 注册所得到的注册 ID 。一般来说,可不处理此广播信息。要深入地集成极光推送,开发者想要自己保存App用户与JPush 用户关系时,则接受此广播,取得 Registration ID 并保存与App uid 的关系到开发者自己的应用服务器上。使用极光推送提供的别名与标签功能,是更加简单轻便的绑定App用户与JPush用户的方式,请参考文档:别名与标签使用教程。Intent 参数JPushInterface.EXTRA_REGISTRATION_IDSDK 向 JPush Server 注册所得到的注册 全局唯一的 ID ,可以通过此 ID 向对应的客户端发送消息和通知。Bundle bundle = intent.getExtras();String title = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);Action - cn.jpush.android.intent.MESSAGE_RECEIVED收到了自定义消息 Push 。SDK 对自定义消息,只是传递,不会有任何界面上的展示。如果开发者想推送自定义消息 Push,则需要在 AndroidManifest.xml 里配置此 Receiver action,并且在自己写的 BroadcastReceiver 里接收处理。Intent 参数JPushInterface.EXTRA_TITLE保存服务器推送下来的消息的标题。对应 API 消息内容的 title 字段。对应 Portal 推送消息界面上的“标题”字段(可选).Bundle bundle = intent.getExtras();String title = bundle.getString(JPushInterface.EXTRA_TITLE);JPushInterface.EXTRA_MESSAGE保存服务器推送下来的消息内容。对应 API 消息内容的 message 字段。对应 Portal 推送消息界面上的"消息内容”字段。 Bundle bundle = intent.getExtras();String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);JPushInterface.EXTRA_EXTRA保存服务器推送下来的附加字段。这是个 JSON 字符串。对应 API 消息内容的 extras 字段。对应 Portal 推送消息界面上的“自定义内容”。Bundle bundle = intent.getExtras();String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);JPushInterface.EXTRA_CONTENT_TYPE保存服务器推送下来的内容类型。对应 API 消息内容的 content_type 字段。Bundle bundle = intent.getExtras();String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);JPushInterface.EXTRA_RICHPUSH_FILE_PATHSDK 1.4.0 以上版本支持。富媒体通消息推送下载后的文件路径和文件名。Bundle bundle = intent.getExtras();String file = bundle.getString(JPushInterface.EXTRA_RICHPUSH_FILE_PATH);JPushInterface.EXTRA_MSG_IDSDK 1.6.1 以上版本支持。唯一标识消息的 ID, 可用于上报统计等。Bundle bundle = intent.getExtras();String file = bundle.getString(JPushInterface.EXTRA_MSG_ID);Action - cn.jpush.android.intent.NOTIFICATION_RECEIVED收到了通知 Push。如果通知的内容为空,则在通知栏上不会展示通知。但是,这个广播 Intent 还是会有。开发者可以取到通知内容外的其他信息。Intent 参数JPushInterface.EXTRA_NOTIFICATION_TITLE保存服务器推送下来的通知的标题。对应 API 通知内容的 n_title 字段。对应 Portal 推送通知界面上的“通知标题”字段。Bundle bundle = intent.getExtras();String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);JPushInterface.EXTRA_ALERT保存服务器推送下来的通知内容。对应 API 通知内容的 n_content 字段。对应 Portal 推送通知界面上的“通知内容”字段。Bundle bundle = intent.getExtras();String content = bundle.getString(JPushInterface.EXTRA_ALERT);JPushInterface.EXTRA_EXTRASDK 1.2.9 以上版本支持。保存服务器推送下来的附加字段。这是个 JSON 字符串。对应 API 通知内容的 n_extras 字段。对应 Portal 推送通知界面上的“自定义内容”字段。Bundle bundle = intent.getExtras();String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);JPushInterface.EXTRA_NOTIFICATION_IDSDK 1.3.5 以上版本支持。通知栏的Notification ID,可以用于清除NotificationBundle bundle = intent.getExtras();int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);JPushInterface.EXTRA_CONTENT_TYPE保存服务器推送下来的内容类型。对应 API 消息内容的 content_type 字段。 Portal 上暂时未提供输入字段。Bundle bundle = intent.getExtras();String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);JPushInterface.EXTRA_RICHPUSH_HTML_PATHSDK 1.4.0 以上版本支持。富媒体通知推送下载的HTML的文件路径,用于展现WebView。Bundle bundle = intent.getExtras();String fileHtml = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_PATH);JPushInterface.EXTRA_RICHPUSH_HTML_RESSDK 1.4.0 以上版本支持。富媒体通知推送下载的图片资源的文件名,多个文件名用 “,” 分开。 与 “JPushInterface.EXTRA_RICHPUSH_HTML_PATH” 位于同一个路径。Bundle bundle = intent.getExtras();String fileStr = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_RES);String[] fileNames = fileStr.spilt(",");JPushInterface.EXTRA_MSG_IDSDK 1.6.1 以上版本支持。 唯一标识通知消息的 ID, 可用于上报统计等。Bundle bundle = intent.getExtras();String file = bundle.getString(JPushInterface.EXTRA_MSG_ID);Action - cn.jpush.android.intent.NOTIFICATION_OPENED用户点击了通知。一般情况下,用户不需要配置此 receiver action。如果开发者在 AndroidManifest.xml 里未配置此 receiver action,那么,SDK 会默认打开应用程序的主 Activity,相当于用户点击桌面图标的效果。如果开发者在 AndroidManifest.xml 里配置了此 receiver action,那么,当用户点击通知时,SDK 不会做动作。开发者应该在自己写的 BroadcastReceiver 类里处理,比如打开某 Activity 。Intent 参数JPushInterface.EXTRA_NOTIFICATION_TITLE保存服务器推送下来的通知的标题。对应 API 通知内容的 n_title 字段。对应 Portal 推送通知界面上的“通知标题”字段。Bundle bundle = intent.getExtras();String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);JPushInterface.EXTRA_ALERT保存服务器推送下来的通知内容。对应 API 通知内容的n_content字段。对应 Portal 推送通知界面上的“通知内容”字段。Bundle bundle = intent.getExtras();String content = bundle.getString(JPushInterface.EXTRA_ALERT);JPushInterface.EXTRA_EXTRASDK 1.2.9 以上版本支持。保存服务器推送下来的附加字段。这是个 JSON 字符串。对应 API 消息内容的 n_extras 字段。对应 Portal 推送通知界面上的“自定义内容”字段。Bundle bundle = intent.getExtras();String type = bundle.getString(JPushInterface.EXTRA_EXTRA);JPushInterface.EXTRA_NOTIFICATION_IDSDK 1.3.5 以上版本支持。通知栏的Notification ID,可以用于清除NotificationBundle bundle = intent.getExtras();int notificationId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_IDJPushInterface.EXTRA_MSG_IDSDK 1.6.1 以上版本支持。唯一标识调整消息的 ID, 可用于上报统计等。Bundle bundle = intent.getExtras();String file = bundle.getString(JPushInterface.EXTRA_MSG_ID);代码示例public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Log.d(TAG, "onReceive - " + intent.getAction()); if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) { } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { System.out.println("收到了自定义消息。消息内容是:" + bundle.getString(JPushInterface.EXTRA_MESSAGE)); // 自定义消息不会展示在通知栏,完全要开发者写代码去处理 } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { System.out.println("收到了通知"); // 在这里可以做些统计,或者做些其他工作 } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { System.out.println("用户点击打开了通知"); // 在这里可以自己写代码去定义用户点击后的行为 Intent i = new Intent(context, TestActivity.class); //自定义打开的界面 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } else { Log.d(TAG, "Unhandled intent - " + intent.getAction()); } }