極光推送,怎麼使用用廣播接收
阿新 • • 發佈:2019-01-06
此處省略,配置依賴,和許可權等問題(檢視官網),只展示中間部分
清單檔案
極光推送 <!-- 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) { // ...//省略了 } }最後執行一下,看一下廣播裡面的日誌資訊是否列印,如果有列印證明配置成功了