Android通知欄(設置多通知,跳轉至對應的界面)

最近在開發項目的過程中需要接入消息推送,綜合各種情況後最終選擇使用環信的消息透傳來實現

其中,遇到一個問題,就是不管推送多少條,信息欄中只顯示最後一條信息,最終查閱資料後,找到了原因

實現的過程如下(前提是app內部已接入環信,並且配置好了環信)

**
 * 獲取透傳信息
 */
public void getTouChuanXinxi() {
    // 註冊一個cmd消息的BroadcastReceiver
    IntentFilter cmdIntentFilter = new IntentFilter(EMChatManager.getInstance().getCmdMessageBroadcastAction());
    mContext.registerReceiver(cmdMessageReceiver, cmdIntentFilter);
}

/**
 * cmd消息BroadcastReceiver
 */
private BroadcastReceiver cmdMessageReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        //獲取cmd message對象
        EMMessage message = intent.getParcelableExtra("message");
        //獲取消息body
        CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody();
        String aciton = cmdMsgBody.action;//獲取自定義action
        i++;
        //獲取擴展屬性
        try {
            String type = message.getStringAttribute("msgid");
            JSONObject js = message.getJSONObjectAttribute("msg");
            Log.i("*********透傳消息*****", js.toString());
            touChuanMessage = JSON.parseObject(js.toString(), TouChuanMessage.class);
            //消息通知欄
            sendNotification(type, touChuanMessage);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }
};
這是我在application中實現的環信消息透傳配置,其中的type值是我和服務器約定好的參數值,不同的值代表着不同類型的推送消息,接下來根據不同的情況,向通知欄發送消息

/**
 * 向通知欄發送消息
 *
 * @param msgid
 * @param
 */
public void sendNotification(String msgid, TouChuanMessage touChuanMessage) {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String edtTitle = "";
    Intent notificationIntent = null; // 點擊該通知後要跳轉的Activity
    if (StringUtils.isEquals(msgid, "系統消息")) {
        if (!AppUtils.isBackground(mContext)) {
            notificationIntent = new Intent(mContext, MainActivity.class); // 點擊該通知後要跳轉的Activity
        }
        edtTitle = touChuanMessage.getContent();
    } else if (StringUtils.isEquals(msgid, "最新評論消息")) {
        notificationIntent = new Intent(mContext, DetailsPageActivity.class); // 點擊該通知後要跳轉的Activity
        Bundle bundle = new Bundle();
        bundle.putString("topicId", touChuanMessage.getTopicId() + "");
        notificationIntent.putExtras(bundle);
        edtTitle = touChuanMessage.getUserName() + "  評論了您的活動";
    } 
可以根據自己的情況定義更多類型的通知,然後對消息進行設置


其中箭頭標識的地方的i值其實是id值,必須對不同的消息設置不同到的id纔可以顯示多條消息,並且跳轉進入不同的界面,兩個地方缺一不可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章