此处省略,配置依赖,和权限等问题(查看官网),只展示中间部分
清单文件
极光推送 <!-- Required SDK 核心功能--> <!-- 可配置android:process参数将PushService放在其他进程中 --> <service android:name="cn.jpush.android.service.PushService" android:enabled="true" android:exported="false" > <intent-filter> <action android:name="cn.jpush.android.intent.REGISTER" /> <action android:name="cn.jpush.android.intent.REPORT" /> <action android:name="cn.jpush.android.intent.PushService" /> <action android:name="cn.jpush.android.intent.PUSH_TIME" /> </intent-filter> </service> <!-- since 3.0.9 Required SDK 核心功能--> <provider android:authorities="你的包名.DataProvider" android:name="cn.jpush.android.service.DataProvider" android:exported="true" tools:replace="android:exported"//配置官方文档的时候这里容易报错,加一行这个 /> <!-- since 3.1.0 Required SDK 核心功能--> <provider android:authorities="你的包名.DownloadProvider" android:name="cn.jpush.android.service.DownloadProvider" android:exported="true" /> <!-- Required SDK核心功能--> <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" > <intent-filter android:priority="1000"> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" /> <category android:name="你的包名"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.USER_PRESENT" /> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> <!-- Optional --> <intent-filter> <action android:name="android.intent.action.PACKAGE_ADDED" /> <action android:name="android.intent.action.PACKAGE_REMOVED" /> <data android:scheme="package" /> </intent-filter> </receiver> <!-- Required SDK核心功能--> <activity android:name="cn.jpush.android.ui.PushActivity" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.NoTitleBar" android:exported="false" > <intent-filter> <action android:name="cn.jpush.android.ui.PushActivity" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="你的包名" /> </intent-filter> </activity> <!-- Required SDK核心功能--> <service android:name="cn.jpush.android.service.DownloadService" android:enabled="true" android:exported="false" > </service> <!-- Required SDK核心功能--> <receiver android:name="cn.jpush.android.service.AlarmReceiver" /> //自己定义的广播,用于接收推送下来的消息,并进行处理的 <receiver android:name=".other.MyReceiver" android:enabled="true" > <intent-filter> <!-- Required 用户注册SDK的intent --> <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 用户打开自定义通知栏的intent --> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <!-- 接收网络变化 连接/断开 since 1.6.3 --> <action android:name="cn.jpush.android.intent.CONNECTION" /> <category android:name="你的包名" /> </intent-filter> </receiver> <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/> <!-- Required. AppKey copied from Portal --> <meta-data android:name="JPUSH_APPKEY" android:value="申请的appKey"/>
自己定义的广播
public class MyReceiver extends BroadcastReceiver { private static final String TAG = "JPush"; private Intent i; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Log.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); Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId); //send the Registration Id to your server... } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE)); processCustomMessage(context, bundle); } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 接收到推送下来的通知"); int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId); } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { Log.d(TAG, "[MyReceiver] 用户点击打开了通知"); Log.d(TAG, "[MyReceiver] 用户点击打开了通知内容"+bundle.getString(JPushInterface.EXTRA_ALERT)); Log.d(TAG, "[MyReceiver] 用户点击打开了通知json"+bundle.getString(JPushInterface.EXTRA_EXTRA)); } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) { Log.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); Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected); } else { Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction()); } } // 打印所有的 intent extra 数据 private static String printBundle(Bundle bundle) { // ...//省略了 return null; } //send msg to MainActivity private void processCustomMessage(Context context, Bundle bundle) { // ...//省略了 } }最后运行一下,看一下广播里面的日志信息是否打印,如果有打印证明配置成功了