使用信鴿的過程,感覺一路艱辛,各種坑,想必各位使用過的也是深有體會的吧。而且官方文檔也太簡潔了。demo功能也不全,沒辦法只能自己摸索着來,這不剛把自定義通知弄明白,就給各位看官獻上來了。
1. XGPushManager功能類
自定義本地通知樣式
void setPushNotificationBuilder(Context context, int notificationBulderId, XGPushNotificationBuilder notificationBuilder)
本地通知,調用下面這個方法,就可以起來一個推送通知
long addLocalNotification(Context context, XGLocalMessage msg)
2 如何自定義通知
這里主要就是需要構造一個XGPushNotificationBuilder
[代碼]java代碼:
XGCustomPushNotificationBuilder build = new XGCustomPushNotificationBuilder(); build.setSound( RingtoneManager.getActualDefaultRingtoneUri( context, RingtoneManager.TYPE_ALARM)) // 設置聲音 // setSound( // Uri.parse("android.resource://" + getPackageName() // + "/" + R.raw.wind)) 設定Raw下指定聲音文件 .setDefaults(Notification.DEFAULT_VIBRATE) // 振動 .setFlags(Notification.FLAG_NO_CLEAR); // 是否可清除 // 設置自定義通知layout,通知背景等可以在layout里設置 build.setLayoutId(R.layout.layout_notification); // 設置自定義通知內容id build.setLayoutTextId(R.id.ssid); // 設置自定義通知標題id build.setLayoutTitleId(R.id.title); // 設置自定義通知圖片id build.setLayoutIconId(R.id.icon); // 設置自定義通知圖片資源 build.setLayoutIconDrawableId(R.drawable.ic_launcher); // 設置狀態欄的通知小圖標 build.setIcon(R.drawable.ic_launcher); // 設置時間id build.setLayoutTimeId(R.id.time); // 若不設定以上自定義layout,又想簡單指定通知欄圖片資源 build.setNotificationLargeIcon(R.drawable.tenda_icon);
3如何使用我們自定義的通知
這個是替換默認的通知,build是上面的那段代碼的,這樣通知就是使用我們自定義的形式了。
XGPushManager.setDefaultNotificationBuilder(context, build);
4 啟動本地通知
[代碼]java代碼:
XGLocalMessage localMessage = new XGLocalMessage(); XGPushManager.addLocalNotification(context, localMessage);
5如何根據推送信息的不同,來顯示不同的推送通知式樣
最初我總是入不了這個門,原因是沒有理解到推送消息和推送通知的區別,如果我們要想使用自定義通知來顯示,我們就需要使用推送消息,信鴿就只是將推送的內容傳遞過來,它說這是透傳,然后用戶根據這些消息,自己做自己想做的事情。
XGPushBaseReceiver類提供透傳消息的接收和操作結果的反饋,需要開發者繼承本類,並重載相關的方法;
void onTextMessage(Context context,XGPushTextMessage message)該方法就是接收透傳消息的方法。這樣我們需要寫一個receiver來繼承XGPushBaseReceiver,直接上代碼了
[代碼]java代碼:
public class MessageReceiver extends XGPushBaseReceiver { private Intent intent = new Intent("com.qq.xgdemo.activity.UPDATE_LISTVIEW"); public static final String LogTag = "TPushReceiver"; private void show(Context context, String text) { Toast.makeText(context, text, Toast.LENGTH_SHORT).show(); } // 通知展示 @Override public void onNotifactionShowedResult(Context context, XGPushShowedResult notifiShowedRlt) { if (context == null || notifiShowedRlt == null) { return; } show(context, "您有1條新消息, " + "通知被展示 , " + notifiShowedRlt.toString()); } @Override public void onUnregisterResult(Context context, int errorCode) { if (context == null) { return; } String text = ""; if (errorCode == XGPushBaseReceiver.SUCCESS) { text = "反注冊成功"; } else { text = "反注冊失敗" + errorCode; } Log.d(LogTag, text); show(context, text); } @Override public void onSetTagResult(Context context, int errorCode, String tagName) { if (context == null) { return; } String text = ""; if (errorCode == XGPushBaseReceiver.SUCCESS) { text = "\"" + tagName + "\"設置成功"; } else { text = "\"" + tagName + "\"設置失敗,錯誤碼:" + errorCode; } Log.d(LogTag, text); show(context, text); } @Override public void onDeleteTagResult(Context context, int errorCode, String tagName) { if (context == null) { return; } String text = ""; if (errorCode == XGPushBaseReceiver.SUCCESS) { text = "\"" + tagName + "\"刪除成功"; } else { text = "\"" + tagName + "\"刪除失敗,錯誤碼:" + errorCode; } Log.d(LogTag, text); show(context, text); } // 通知點擊回調 actionType=1為該消息被清除,actionType=0為該消息被點擊 @Override public void onNotifactionClickedResult(Context context, XGPushClickedResult message) { if (context == null || message == null) { return; } String text = ""; sendIconCountMessage(context); samsungShortCut(context, "25"); if (message.getActionType() == XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) { // 通知在通知欄被點擊啦。。。。。 // APP自己處理點擊的相關動作 // 這個動作可以在activity的onResume也能監聽,請看第3點相關內容 text = "通知被打開 :" + message; } else if (message.getActionType() == XGPushClickedResult.NOTIFACTION_DELETED_TYPE) { // 通知被清除啦。。。。 // APP自己處理通知被清除后的相關動作 text = "通知被清除 :" + message; } Toast.makeText(context, "廣播接收到通知被點擊:" + message.toString(), Toast.LENGTH_SHORT).show(); // 獲取自定義key-value String customContent = message.getCustomContent(); if (customContent != null && customContent.length() != 0) { try { JSONObject obj = new JSONObject(customContent); // key1為前台配置的key if (!obj.isNull("ID")) { String value = obj.getString("ID"); Log.d(LogTag, "get custom value:" + value); } // ... } catch (JSONException e) { e.printStackTrace(); } } // APP自主處理的過程。。。 Log.d(LogTag, text); show(context, text); } @Override public void onRegisterResult(Context context, int errorCode, XGPushRegisterResult message) { // TODO Auto-generated method stub if (context == null || message == null) { return; } String text = ""; if (errorCode == XGPushBaseReceiver.SUCCESS) { text = message + "注冊成功"; // 在這里拿token String token = message.getToken(); } else { text = message + "注冊失敗,錯誤碼:" + errorCode; } Log.d(LogTag, text); show(context, text); } // 消息透傳 @Override public void onTextMessage(Context context, XGPushTextMessage message) { // TODO Auto-generated method stub show(context, "haha"); String text = "收到消息:" + message.toString(); // 獲取自定義key-value String customContent = message.getCustomContent(); if (customContent != null && customContent.length() != 0) { try { JSONObject obj = new JSONObject(customContent); // key1為前台配置的key if (!obj.isNull("key")) { String value = obj.getString("key"); Log.d(LogTag, "get custom value:" + value); } // ... } catch (JSONException e) { e.printStackTrace(); } } // APP自主處理消息的過程... XGLocalMessage localMessage = new XGLocalMessage(); localMessage.setTitle("haha"); localMessage.setContent(message.getContent()); XGCustomPushNotificationBuilder build = new XGCustomPushNotificationBuilder(); build.setSound( RingtoneManager.getActualDefaultRingtoneUri( context, RingtoneManager.TYPE_ALARM)) // 設置聲音 // setSound( // Uri.parse("android.resource://" + getPackageName() // + "/" + R.raw.wind)) 設定Raw下指定聲音文件 .setDefaults(Notification.DEFAULT_VIBRATE) // 振動 .setFlags(Notification.FLAG_NO_CLEAR); // 是否可清除 // 設置自定義通知layout,通知背景等可以在layout里設置 build.setLayoutId(R.layout.layout_notification); // 設置自定義通知內容id build.setLayoutTextId(R.id.ssid); // 設置自定義通知標題id build.setLayoutTitleId(R.id.title); // 設置自定義通知圖片id build.setLayoutIconId(R.id.icon); // 設置自定義通知圖片資源 build.setLayoutIconDrawableId(R.drawable.ic_launcher); // 設置狀態欄的通知小圖標 build.setIcon(R.drawable.ic_launcher); // 設置時間id build.setLayoutTimeId(R.id.time); // 若不設定以上自定義layout,又想簡單指定通知欄圖片資源 build.setNotificationLargeIcon(R.drawable.tenda_icon); // 客戶端保存build_id XGPushManager.setDefaultNotificationBuilder(context, build); XGPushManager.addLocalNotification(context, localMessage); Log.d(LogTag, text); show(context, text); } private void sendIconCountMessage(Context context) { Intent it = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE"); it.putExtra("android.intent.extra.update_application_component_name", "com.example.wujie.xungetest/.MainActivity"); String iconCount = "50"; it.putExtra("android.intent.extra.update_application_message_text", iconCount); context.sendBroadcast(it); } }
這樣就可以完美自定義了,覺得有用,就請頂一下,謝謝