Android整合極光推送
概述
推送是現在大部分應用都擁有的一項功能,使用推送的目的就是為了讓客戶端接收到最新的訊息以及提醒等,今天我們就來學習一下目前用的比較廣泛的極光推送。
整合過程
首先進入極光推送官網,註冊並且登入帳號,地址如下
登入成功後,會跳到建立應用介面
此時因為還沒有應用,我們點選建立一個新的應用,接下來會跳到填寫應用資訊頁面
我們填上比較重要的兩項,應用名稱和包名,其他不是必填的我們先不管,補充完資訊後會跳到下面的頁面,
這裡顯示了一些比較重要的資訊,在後面的開發中我們需要用到。
在上面的圖中,我們看到有個快速整合,我們滑鼠放上去
在整合到我們的應用之前,我們先下載配置好的Demo,進行一下各種訊息推送的測試吧。
推送效果測試
我們進入到控制檯,點選上方的推送,此時進入到訊息推送頁面,首先我們需要將應用部署到手機上,然後在這個頁面就可以給客戶端進行推送了。
我們看到左邊有幾個選項,傳送訊息,自定義訊息,富媒體訊息,推送歷史,定時訊息等。接下來我們一一去學習一下。
傳送通知
這個是傳送通知欄通知,我們可以設定通知的標題,推送的內容,下面給大家看一下效果圖
注意:推送內容如果內容為空的話,我們是無法在通知欄看到通知的。此時沒有任何效果。
自定義訊息
這裡我們推送的內容會以訊息的形式顯示在介面上
富媒體訊息
包含以下幾種型別
1.Landing page
填下好資訊以後,我們進入下一個頁面輸入推送內容,傳送通知
2.彈窗
彈窗提供了兩個模版,我們隨便選擇一個,
3.URL
輸入URL,這裡輸入慕課網的連結
4.資訊流
資訊流也提供了兩個模版,我們選擇其中一個
然後我們點選資訊流內容,會跳到詳情頁面
5.紅包
由於紅包需要整合第三方SDK,這裡我們就不進行介紹了
推送歷史
這裡檢視推送的歷史訊息
定時訊息
這裡檢視定時訊息的資訊,由於我之前並沒有傳送過定時訊息,所以這裡暫且沒有顯示任何內容。
專案整合
上面詳細演示了各種推送效果,接下來就該真正把需要的功能整合到我哦們的專案中了。
1.Android Studio中建立一個應用,並且填入資訊獲取AppKey。
2.下載極光推送SDK
參照Android SDK 整合指南配置專案
接下來說一下我整合的步驟。
首先配置清單檔案,如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jpushdemo">
<!--JPush自定義的許可權,必須配置,否則無法初始化成功-->
<permission
android:name="com.example.jpushdemo.permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />
<!--需要的系統許可權-->
<uses-permission android:name="com.example.jpushdemo.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.WRITE_SETTINGS" />
<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.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:name=".DemoApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<!--主Activity-->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- SDK核心功能,必須新增 -->
<activity
android:name="cn.jpush.android.ui.PushActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="false"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.jpushdemo" />
</intent-filter>
</activity>
<!-- SDK核心功能,必須新增 -->
<service
android:name="cn.jpush.android.service.DownloadService"
android:enabled="true"
android:exported="false"></service>
<!-- 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>
<!-- SDK核心功能,必須新增 -->
<receiver
<!-- Rich push 核心功能 since 2.0.6-->
<activity
android:name="cn.jpush.android.ui.PopWinActivity"
android:theme="@style/MyDialogStyle"
android:exported="false">
</activity>
android:name="cn.jpush.android.service.PushReceiver"
android:enabled="true"
android:exported="false">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
<!-- 必須新增 顯示通知欄 -->
<category android:name="com.example.jpushdemo" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
<!-- 可選的 -->
<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>
<!-- SDK核心功能,必須新增 -->
<receiver
android:name="cn.jpush.android.service.AlarmReceiver"
android:exported="false" />
<!-- 必須的,可以獲取統計資料 -->
<meta-data
android:name="JPUSH_CHANNEL"
android:value="developer-default" />
<!--應用的appkey-->
<meta-data
android:name="JPUSH_APPKEY"
android:value="be2b1630aab8246b94acacb6" />
<activity android:name=".TestActivity"></activity>
<!--自定義的廣播-->
<receiver
android:name="com.example.jpushdemo.MyReceiver"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" /> <!--Required 使用者註冊SDK的intent-->
<action android:name="cn.jpush.android.intent.UNREGISTRATION" />
<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.example.jpushdemo" />
</intent-filter>
</receiver>
</application>
</manifest>
這部分內容參照官方生成的Demo就可以了,注意一定不要少某些東西,否則會導致無法初始化成功,影響推送功能。
然後將libs,jniLibs,res,中的相應檔案根據文件介紹進行新增即可,這步比較簡單。
這樣,環境差不多搭建好了,然後開始編寫我們的程式碼。
首先建立一個Application,在裡面進行JPush的初始化
public class DemoApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
//設定開啟日誌,釋出時請關閉日誌
JPushInterface.setDebugMode(true);
//初始化
JPushInterface.init(this);
}
}
當然別忘了在清單檔案配置這個Application.
public class MainActivity extends AppCompatActivity {
private MessageReceiver mMessageReceiver;
public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
public static final String KEY_TITLE = "title";
public static final String KEY_MESSAGE = "message";
public static final String KEY_EXTRAS = "extras";
public static boolean isForeground = false;
private TextView tvRegId;
private TextView tvAppKey;
private TextView tvPackageName;
private TextView tvIMEI;
private TextView tvDeviceId;
private TextView tvVersion;
private String registrationID;
private EditText etMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JPushInterface.init(getApplicationContext());
initView();
initData();
registerMessageReceiver();
}
private void initView() {
tvRegId = (TextView) findViewById(R.id.tv_regId);
tvAppKey = (TextView) findViewById(R.id.tv_appkey);
tvPackageName = (TextView) findViewById(R.id.tv_packagename);
tvIMEI = (TextView) findViewById(R.id.tv_imei);
tvDeviceId = (TextView) findViewById(R.id.tv_devicceid);
tvVersion = (TextView) findViewById(R.id.tv_version);
etMsg = (EditText) findViewById(R.id.et_msg);
}
private void initData() {
//裝置串號,在真機上才能獲得,模擬器上為一串0
String imei = ExampleUtil.getImei(this, "");
tvIMEI.setText("IMEI: "+imei);
//建立應用時獲得的AppKey
String appKey = ExampleUtil.getAppKey(this);
tvAppKey.setText("AppKey: "+appKey);
//應用程式第一次註冊到JPush伺服器時,伺服器會返回一個唯一的該裝置的標識:RegistertionID
registrationID = JPushInterface.getRegistrationID(this);
tvRegId.setText("RegId: "+ registrationID);
//應用包名
String packageName = getPackageName();
tvPackageName.setText("PackageName:"+packageName);
//裝置Id
String deviceId = ExampleUtil.getDeviceId(this);
tvDeviceId.setText("deviceId: "+deviceId);
//應用版本號
String version = ExampleUtil.GetVersion(this);
tvVersion.setText("Version: "+version);
}
@Override
protected void onResume() {
super.onResume();
//在所有的Activity都要呼叫,一般放在基類中
JPushInterface.onResume(this);
isForeground = true;
}
@Override
protected void onPause() {
super.onPause();
isForeground = false;
//在所有的Activity都要呼叫,一般放在基類中
JPushInterface.onPause(this);
}
public void registerMessageReceiver() {
mMessageReceiver = new MessageReceiver();
IntentFilter filter = new IntentFilter();
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
filter.addAction(MESSAGE_RECEIVED_ACTION);
registerReceiver(mMessageReceiver, filter);
}
//接收自定義訊息的廣播
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
String messge = intent.getStringExtra(KEY_MESSAGE);
//附加訊息,鍵值對形式,以Json格式展示
String extras = intent.getStringExtra(KEY_EXTRAS);
StringBuilder showMsg = new StringBuilder();
showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
if (!ExampleUtil.isEmpty(extras)) {
showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
}
setCostomMsg(showMsg.toString());
}
}
}
private void setCostomMsg(String msg){
// Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
etMsg.setText(msg);
}
public void customNotification(View view){
CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(this,
R.layout.custom_notification,R.id.iv_icon,R.id.tv_title,R.id.tv_content);
builder.layoutIconDrawable = R.drawable.ym2;//下拉通知欄顯示的圖示
builder.statusBarDrawable = R.mipmap.ic_launcher;//通知欄上方顯示的小圖示
JPushInterface.setPushNotificationBuilder(2,builder);
JPushInterface.setDefaultPushNotificationBuilder(builder);//必須呼叫,否則無法設定成功
}
}
接下來的步驟也沒啥難度,照著Demo就可以了,文章末尾我會給出程式碼下載地址,我們先看看整合到專案以後的執行效果圖吧。
最後給出Demo下載地址