Android開發記錄18-集成推送服務的一點說明
關于推送服務,國內有很多選擇,筆者也對它們進行了一個詳細的對比,一般我們產品選擇推送服務主要考量以下幾個要素:
1、是否收費,如何收費?
2、推送內容是是什么(是否包含通知、消息、富媒體等等)
3、穩定性、及時性如何?
4、集成難度是否簡單
5、支持平臺有哪些(主流Android、IOS)
6、服務端支持語言(Java、C#、PHP、Python等)
例舉國內主要的一些推送服務:
來自Devstore的統計,共收錄了國內21家推送服務,分別是(按關注度排列):
1.個推(個信互動(北京)網絡科技有限公司http://www.igetui.com/)
2.百度云推送(百度http://developer.baidu.com/cloud/push)
3.極光推送(深圳市和訊華谷信息技術有限公司https://www.jpush.cn/)
4.友盟推送(友盟http://www.umeng.com/push)
5.小米推送(小米http://dev.xiaomi.com/doc/?page_id=1670)
6.騰訊信鴿推送(騰訊公司http://xg.qq.com/)
7.Bmob推送(廣州市比目網絡科技有限公司http://www.bmob.cn/)
8.云巴推送(深圳市微智云科技有限公司http://www.yunba.io/)
9.華為推送(華為公司http://developer.huawei.com/push)
10.智游推送(北京智游網安科技有限公司http://www.zypush.com/)
11.盛大云推送(盛大網絡http://www.grandcloud.cn/product/push)
12.原子推送(原子技術有限公司http://www.atom14.com/)
13.魔橋推送(魔橋http://www.mobbridge.com/)
14.魔泊網推送(魔泊網http://helpdocs.sturgeon.mopaas.com/helpdocs/_push.html)
15.有推推送(中國移動通信http://dev.10086.cn/aoi/index.jsp)
16.WeCloud(WeCloud http://www.wecloud.io/)
17.Learn Cloud推送(美味書簽信息技術有限公司https://cn.avoscloud.com/)
18.亞馬遜推送(亞馬遜公司http://aws.amazon.com/cn/sns/)
19.魔方推送(魔方公司[http://www.imofan.com/](http://www.imofan
這里選擇了“極光推送”,它是部分收費的,收費模式各位可以到官網查看;支持推送的內容有通知、消息、富媒體,穩定性好、能及時到達、提供服務API、支持Android、iOS平臺,服務端支持Java、PHP、Python、C#、Ruby、Node.js。
集成極光推送筆者這里也不詳細寫,主要提幾點:
1、使用Portal來進行測試
Portal是服務提供的傳送門,我們可以使用控制臺來進行推送測試,實際應用時一般是根據推送服務提供的服務端API來實現定制推送。
極光Portal如下:
2、通知與消息的區別
通知就是可以再通知欄顯示提醒用戶的信息,而消息不會在通知欄顯示,業務邏輯可以完全有開發者來定。
3、推送對象
可以分為:
廣播:會把通知無區別的推送到每個人身上。
設置標簽:這一般用于群組推送。
設置別名:適用于單播,根據客戶端設置的別名來推送。
設置注冊ID:適用于單播推送,指定推送給某一個人,可以使注冊過的用戶ID,主要用來區分。
4、自定義通知欄樣式、附加字段的作用
我們有時候可能不想直接用Android原生的通知欄樣式,如果服務提供相應的API的話,我們可以通過自定義布局來達到這個目的。極光這里提供了以下方法:
// 自定義Notification樣式
CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
getApplicationContext(),
R.layout.customer_notitfication_layout, R.id.icon, R.id.title,
R.id.text);
builder.layoutIconDrawable = R.drawable.ic_launcher;
builder.developerArg0 = "developerArg2";
JPushInterface.setPushNotificationBuilder(2, builder);
Toast.makeText(getApplicationContext(), "Custom Builder - 2",
Toast.LENGTH_SHORT).show(); ```

我們只需要指定通知欄編號,下次推送通知的時候就會以自定義的通知欄樣式來顯示。
這里還有一個附加字段,我們有時候可能需要根據推送的不同消息來實現跳轉不同的頁面,這時候就可能需要用到附加字段了,我們在Broadcast Receiver來接受推送下來的消息,解析附加字段內容,來達到我們的目的。
代碼示例:
package com.infzm.daily.know.receiver;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import cn.jpush.android.api.JPushInterface;
import com.infzm.daily.know.ArticleDetailActivity;
import com.infzm.daily.know.MainActivity;
import com.infzm.daily.know.utils.LogUtils;
public class PushReceiver extends BroadcastReceiver {
private static final String TAG = "JPush";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
LogUtils.logi(TAG, "[PushReceiver] onReceive - " + intent.getAction() + ", extras: "+ printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
LogUtils.logi(TAG, "[PushReceiver] 接收Registeration Id : " + regId);
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
LogUtils.logi(TAG, "[PushReceiver] 接收到推送下來的自定義消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
}else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
LogUtils.logi(TAG, "[PushReceiver] 接收到推送下來的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
LogUtils.logi(TAG, "[PushReceiver] 接收到推送下來的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
LogUtils.logi(TAG, "[PushReceiver] 用戶點擊打開了通知");
String type = bundle.getString(JPushInterface.EXTRA_EXTRA);
LogUtils.loge(TAG, "type:" + type);
try {
JSONObject jsonObject = new JSONObject(type);
String str = jsonObject.getString("key");
if (str.equals("1")) {
//打開自定義的Activity
Intent i = new Intent(context, MainActivity.class);
bundle.putInt("index", 1);
i.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
context.startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
LogUtils.logi(TAG, "[PushReceiver] 用戶收到到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);
LogUtils.logi(TAG, "[PushReceiver]" + intent.getAction() +" connected state change to "+connected);
} else {
LogUtils.logi(TAG, "[PushReceiver] Unhandled intent - " + intent.getAction());
}
}
// 打印所有的 intent extra 數據
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
}
else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}
} ```