1,去JPush官網註冊一個帳號,建立你的app的應用,而且拿到你應用的AppKeyjava
2,在JPush官網下載對應的sdk,解壓出來,將libs文件下的全部的文件所有複製到你工程的libs文件中android
3,在清單文件中添加對應的權限和activity(更改對應的包名和對應的AppKey)api
AndroidManifest.xml權限配置: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="您應用的包名" android:versionCode="100" android:versionName="1.0.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <!-- Required --> <permission android:name="您應用的包名.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> <!-- Required --> <uses-permission android:name="You Package.permission.JPUSH_MESSAGE" /> <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <!-- Optional. Required for location feature --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 應用包名及appkey替換: <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name="Your Application"> <!-- Required SDK 核心功能--> <!-- option since 2.0.5 可配置PushService,DaemonService,PushReceiver,AlarmReceiver的android:process參數 將JPush相關組件設置爲一個獨立進程 --> <!-- 如:android:process=":remote" --> <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 1.8.0 option 可選項。用於同一設備中不一樣應用的JPush服務相互拉起的功能。 --> <!-- 若不啓用該功能可刪除該組件,將不拉起其餘應用也不能被其餘應用拉起 --> <service android:name="cn.jpush.android.service.DaemonService" android:enabled="true" android:exported="true"> <intent-filter > <action android:name="cn.jpush.android.intent.DaemonService" /> <category android:name="您應用的包名"/> </intent-filter> </service> <!-- Required --> <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: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" /> <!-- User defined. 用戶自定義的廣播接收器--> <receiver android:name="您本身定義的Receiver" android:enabled="true"> <intent-filter> <!--Required 用戶註冊SDK的intent--> <action android:name="cn.jpush.android.intent.REGISTRATION" /> <action android:name="cn.jpush.android.intent.UNREGISTRATION" /> <!--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" /> <!--Optional 用戶接受Rich Push Javascript 回調函數的intent--> <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <!-- 接收網絡變化 鏈接/斷開 since 1.6.3 --> <action android:name="cn.jpush.android.intent.CONNECTION" /> <category android:name="您應用的包名" /> </intent-filter> </receiver> <!-- Required. For publish channel feature --> <!-- JPUSH_CHANNEL 是爲了方便開發者統計APK分發渠道。--> <!-- 例如: --> <!-- 發到 Google Play 的APK能夠設置爲 google-play; --> <!-- 發到其餘市場的 APK 能夠設置爲 xxx-market。 --> <!-- 目前這個渠道統計功能的報表還未開放。--> <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/> <!-- Required. AppKey copied from Portal --> <meta-data android:name="JPUSH_APPKEY" android:value="Your AppKey"/> </application> </manifest>
4,在自定義的application中初始化JPush網絡
public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); JPushInterface.setDebugMode(true); JPushInterface.init(this); } }
這樣就基本上都ok了,可是想要接受自定義的消息的時候須要寫一個廣播接受者,並註冊就okapp
自定義的BroadcastREceiver MyJPushBroadcastReceiver.java ide
package com.example.jpush; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import cn.jpush.android.api.JPushInterface; public class MyJPushBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); 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, MainActivity.class); //自定義打開的界面 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } else { } } }
這樣就能夠推送咱們本身App的消息和通知了函數