Android開發(fā)記錄18-集成推送服務(wù)的一點說明
關(guān)于推送服務(wù)檀轨,國內(nèi)有很多選擇,筆者也對它們進(jìn)行了一個詳細(xì)的對比钙畔,一般我們產(chǎn)品選擇推送服務(wù)主要考量以下幾個要素:
1茧跋、是否收費(fèi),如何收費(fèi)悲幅?
2套鹅、推送內(nèi)容是是什么(是否包含通知、消息汰具、富媒體等等)
3卓鹿、穩(wěn)定性、及時性如何郁副?
4减牺、集成難度是否簡單
5、支持平臺有哪些(主流Android存谎、IOS)
6拔疚、服務(wù)端支持語言(Java、C#既荚、PHP稚失、Python等)
例舉國內(nèi)主要的一些推送服務(wù):
來自Devstore的統(tǒng)計,共收錄了國內(nèi)21家推送服務(wù)恰聘,分別是(按關(guān)注度排列):
1.個推(個信互動(北京)網(wǎng)絡(luò)科技有限公司http://www.igetui.com/)
2.百度云推送(百度http://developer.baidu.com/cloud/push)
3.極光推送(深圳市和訊華谷信息技術(shù)有限公司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推送(廣州市比目網(wǎng)絡(luò)科技有限公司http://www.bmob.cn/)
8.云巴推送(深圳市微智云科技有限公司http://www.yunba.io/)
9.華為推送(華為公司http://developer.huawei.com/push)
10.智游推送(北京智游網(wǎng)安科技有限公司http://www.zypush.com/)
11.盛大云推送(盛大網(wǎng)絡(luò)http://www.grandcloud.cn/product/push)
12.原子推送(原子技術(shù)有限公司http://www.atom14.com/)
13.魔橋推送(魔橋http://www.mobbridge.com/)
14.魔泊網(wǎng)推送(魔泊網(wǎng)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推送(美味書簽信息技術(shù)有限公司https://cn.avoscloud.com/)
18.亞馬遜推送(亞馬遜公司http://aws.amazon.com/cn/sns/)
19.魔方推送(魔方公司[http://www.imofan.com/](http://www.imofan
這里選擇了“極光推送”句各,它是部分收費(fèi)的吸占,收費(fèi)模式各位可以到官網(wǎng)查看;支持推送的內(nèi)容有通知凿宾、消息矾屯、富媒體,穩(wěn)定性好初厚、能及時到達(dá)件蚕、提供服務(wù)API、支持Android产禾、iOS平臺排作,服務(wù)端支持Java、PHP亚情、Python妄痪、C#、Ruby楞件、Node.js衫生。
集成極光推送筆者這里也不詳細(xì)寫,主要提幾點:
1履因、使用Portal來進(jìn)行測試
Portal是服務(wù)提供的傳送門障簿,我們可以使用控制臺來進(jìn)行推送測試盹愚,實際應(yīng)用時一般是根據(jù)推送服務(wù)提供的服務(wù)端API來實現(xiàn)定制推送栅迄。
極光Portal如下:
2、通知與消息的區(qū)別
通知就是可以再通知欄顯示提醒用戶的信息皆怕,而消息不會在通知欄顯示毅舆,業(yè)務(wù)邏輯可以完全有開發(fā)者來定。
3愈腾、推送對象
可以分為:
廣播:會把通知無區(qū)別的推送到每個人身上憋活。
設(shè)置標(biāo)簽:這一般用于群組推送。
設(shè)置別名:適用于單播虱黄,根據(jù)客戶端設(shè)置的別名來推送悦即。
設(shè)置注冊ID:適用于單播推送,指定推送給某一個人橱乱,可以使注冊過的用戶ID辜梳,主要用來區(qū)分。
4泳叠、自定義通知欄樣式作瞄、附加字段的作用
我們有時候可能不想直接用Android原生的通知欄樣式,如果服務(wù)提供相應(yīng)的API的話危纫,我們可以通過自定義布局來達(dá)到這個目的宗挥。極光這里提供了以下方法:
// 自定義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(); ```
![](http://upload-images.jianshu.io/upload_images/5423625-2714f27b9e2d19c8?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
我們只需要指定通知欄編號乌庶,下次推送通知的時候就會以自定義的通知欄樣式來顯示。
這里還有一個附加字段契耿,我們有時候可能需要根據(jù)推送的不同消息來實現(xiàn)跳轉(zhuǎn)不同的頁面瞒大,這時候就可能需要用到附加字段了,我們在Broadcast Receiver來接受推送下來的消息搪桂,解析附加字段內(nèi)容糠赦,來達(dá)到我們的目的。
代碼示例:
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));
//在這里根據(jù) JPushInterface.EXTRA_EXTRA 的內(nèi)容處理代碼锅棕,比如打開新的Activity拙泽, 打開一個網(wǎng)頁等..
} 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 數(shù)據(jù)
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();
}
} ```