信鴿推送集成華爲渠道通知乾貨

信鴿渠道通過華爲渠道發送通知,關於各種各樣配置自己搞定就可以了;通知可以點擊跳轉到指定頁面帶參數都是可以的,所以信鴿給華爲渠道接口可以沒有傳custom_content,賦值的時候賦空值了,可以通過 intent 傳值*

 ClickAction ca = new ClickAction();
	    ca.setActionType(3);
	    ca.setIntent("intent://com.xxxx/"+ path
	    +"?"+ para.toString() +"#Intent;scheme=xgscheme;launchFlags=0x10000000;end");

參數直接傳到手機端接收

if(getIntent().getData() != null){
			this.pk= this.getIntent().getData().getQueryParameter("pk");
			this.className= this.getIntent().getData().getQueryParameter("class");
		}

這個接口端對於intent的參數怎麼生成給了說明

  Intent intent01 = new Intent(Intent.ACTION_VIEW, Uri.parse("xgscheme://com.xxxxx/PushMesClass?id=849&title="));
        intent01.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        String intentUri = intent01.toUri(Intent.URI_INTENT_SCHEME);
        

生成的 intentUri 直接在api 裏面直接set 就可以了
"xgscheme://com.xxxxx/PushMesClass?id=849&title=
這個東西手機端activity 裏面要配置下

 <activity
            android:name=".activity.xxxxx"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="com.xxxxx"
                    android:path="/ApplicationReady"
                    android:scheme="xgscheme" />
            </intent-filter>
        </activity>

但是對於java 調取發送通知這一塊給說明把封裝的Message 通過 PushAppRequest 推到信鴿的服務器就可以了

public static void pushToAndroid(Message message, String account) {
		XingeApp xinge = new XingeApp(accessIdAndroid, secretKeyAndroid);
		ArrayList<String> account_list = new ArrayList<String>();
		for(String s : account.split(","))
		{
		    account_list.add(s);
		}
		//推送請求信息
		PushAppRequest pushAppRequest=new PushAppRequest();
//        message.setAccept_time(pair);
        //消息體
        pushAppRequest.setMessage(message);
        //消息類型
        pushAppRequest.setMessage_type(MessageType.notify);
       //推送目標
        pushAppRequest.setAudience_type(AudienceType.account_list);
        //推送平臺
        pushAppRequest.setPlatform(Platform.android);
        
        pushAppRequest.setEnvironment(Environment.product);
        //推送賬號
        pushAppRequest.setAccount_list(account_list);
        
        pushAppRequest.setPush_id("0");
        String pushStr = pushAppRequest.toString();
        JSONObject pushJo = new JSONObject(pushStr);
        if(message.getAndroid() != null && message.getAndroid().getAction() != null)
        {
            pushJo.getJSONObject("message").getJSONObject("android").put("action", message.getAndroid().getAction().toJsonObject());
        }
        System.out.println(pushJo.toString());
        
        JSONObject jo = xinge.pushApp(pushJo.toString());
		KLog.print("android->" + jo.toString());
	}

這個地方爲什麼

 if(message.getAndroid() != null && message.getAndroid().getAction() != null)
        {
            pushJo.getJSONObject("message").getJSONObject("android").put("action", message.getAndroid().getAction().toJsonObject());
        }

寫這個呢,那是因爲 這個實體java api ClickAction給了set 賦值,沒給get
哈哈,這個是服務器java 的bug吧

華爲手機端處理通知,點擊就可以了,
但是對於非華爲手機點擊通知執行:
找的手機端這個類 XinGeMessageReceiver 這個方法onNotifactionClickedResult
裏面處理要跳轉的頁面就可以了

   // 獲取自定義key-value
        String customContent = message.getCustomContent();
        if (customContent != null && customContent.length() != 0 && !"{}".equals(customContent)) {
            SharedPreferences sp = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
            if (sp.getBoolean("pushable", true)) {
                // 獲取消息內容
                try {
                    KPushMsg msg = Util4Json.toObject(customContent, KPushMsg.class);
                    Util4Log.d(TAG, "Notifaction click  push msg:" + Util4Json.format(customContent));
                    ClickIntent(context, msg);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        String activityName = message.getActivityName();

        if(activityName != null && activityName.length() != 0){
            String[] alterIntent = activityName.split("#");
            String urlIntent = alterIntent[0].replace("intent","xgscheme");
            Intent activityIntent = new             Intent().setAction(Intent.ACTION_VIEW).setData(Uri.parse(urlIntent));
            context.startActivity(activityIntent);
        }

這裏面是兩種方法都可以實現自己的跳轉的

裏面的

 if (customContent != null && customContent.length() != 0 && !"{}".equals(customContent))
    if(activityName != null && activityName.length() != 0){

這兩個判斷不能少,要不華爲手機進這個方法了,防止華爲手機重複執行的,有問題留言,
讀這個的時候,要熟讀信鴿的android 端,java 接口端文檔,還有華爲的文檔呀,耐心讀,就會有收穫。

發佈了46 篇原創文章 · 獲贊 22 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章