1. 程式人生 > >騰訊信鴿自定義推送通知

騰訊信鴿自定義推送通知

使用信鴿的過程,感覺一路艱辛,各種坑,想必各位使用過的也是深有體會的吧。而且官方文件也太簡潔了。demo功能也不全,沒辦法只能自己摸索著來,這不剛把自定義通知弄明白,就給各位看官獻上來了。

1. XGPushManager功能類

自定義本地通知樣式
void setPushNotificationBuilder(Context context, int notificationBulderId, XGPushNotificationBuilder notificationBuilder)
本地通知,呼叫下面這個方法,就可以起來一個推送通知
long addLocalNotification(Context context, XGLocalMessage msg)

2 如何自定義通知

這裡主要就是需要構造一個XGPushNotificationBuilder

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 啟動本地通知

XGLocalMessage  localMessage = new XGLocalMessage();
XGPushManager.addLocalNotification(context, localMessage);

5如何根據推送資訊的不同,來顯示不同的推送通知式樣

最初我總是入不了這個門,原因是沒有理解到推送訊息和推送通知的區別,如果我們要想使用自定義通知來顯示,我們就需要使用推送訊息,信鴿就只是將推送的內容傳遞過來,它說這是透傳,然後使用者根據這些訊息,自己做自己想做的事情。
XGPushBaseReceiver類提供透傳訊息的接收和操作結果的反饋,需要開發者繼承本類,並重載相關的方法;
void onTextMessage(Context context,XGPushTextMessage message)該方法就是接收透傳訊息的方法。這樣我們需要寫一個receiver來繼承XGPushBaseReceiver,直接上程式碼了

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);
}
}

這樣就可以完美自定義了,覺得有用,就請頂一下,謝謝