爲Titanium建立本身的安卓推送模塊

在手機應用中,推送是一個很是重要的功能。相對來講ios應用的推送功能很容易作,由於它統一都是用蘋果的APNS服務實現的。但安卓這邊就比較混亂了,雖然谷歌也推出了相似蘋果的官方推送服務,但因爲谷歌的服務器在國內常常被牆,因此用谷歌官方提供的推送服務在國內是不可行的,因此安卓的應用就只能本身實現推送服務了。但若是徹底由本身實現推送功能,那成本是很是大的。因此通常咱們會選擇一些第三方推送服務,好比極光推送就是一個很是不錯的選擇。java

首先,下載極光推送的安卓sdk,解壓,找到打開libs文件夾,裏面有兩個東西android

QQ截圖20131221123659

把armeabi這裏文件夾複製到你的Titanium安卓模塊項目根目錄的libs文件夾裏面,若是沒有這個文件夾能夠建立。模塊項目的libs文件夾是用來放第三方動態連接庫文件的地方,也就是後綴名爲.so的文件。而後把 jpush-sdk-release1.5.5.jar 這個文件複製到模塊項目根目錄的 lib 文件夾裏面。這樣咱們就能夠在模塊中使用極光推送的api了。ios

因爲極光推送的api比較簡單,因此咱們只須要在模塊的主文件中把極光推送提供的api與咱們能在titanium項目中使用的js方法進行綁定就好了。api

模塊主文件名爲JpushModule.java,其源碼以下:數組

 

/**
 * This file was auto-generated by the Titanium Module SDK helper for Android
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 *
 */
package com.yeshcp.jpush;

import java.util.Set;
import java.util.HashSet;
import java.util.HashMap;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

@Kroll.module(name="Jpush", id="com.yeshcp.jpush")
public class JpushModule extends KrollModule
{

    // Standard Debugging variables
    private static final String TAG = "JpushModule";

    // You can define constants with @Kroll.constant, for example:
    // @Kroll.constant public static final String EXTERNAL_NAME = value;
    
    public JpushModule()
    {
        super();
    }

    @Kroll.onAppCreate
    public static void onAppCreate(TiApplication app)
    {
        Log.d(TAG, "inside onAppCreate");
        JPushInterface.setDebugMode(true);
        JPushInterface.init(app); //init jpush
    }
    @Kroll.method
    public void stopPush(){ //stop push
        JPushInterface.stopPush(TiApplication.getInstance());
    }
    
    @Kroll.method
    public void resumePush(){ //resume push
        JPushInterface.resumePush(TiApplication.getInstance());
    }
    
    @Kroll.method
    public void setAliasAndTags(String alias, Object[] tags,final KrollFunction callback){//設置標籤與別名,callback參數必須設置final
        Set set=new HashSet();
        for(Object n : tags){ //把object數組轉化爲set,由於jpush須要傳入一個set類型
           set.add(n.toString());
        }
        JPushInterface.setAliasAndTags(TiApplication.getInstance(),alias,set,new TagAliasCallback(){//使用匿名內部類做爲回調類
            @Override
            public void gotResult(int arg0, String arg1,Set<String> arg2) {
                Log.d("JPush", "Jpush setAliasAndTags status: " + arg0);//狀態
                if(callback != null){
                    KrollDict map = new KrollDict(); //回調函數的參數
                    map.put("code", arg0);           
                    callback.callAsync(getKrollObject(),map); //執行回調
                }
            }
        });
    }
    
    @Kroll.method
    public void setAlias(String alias,final KrollFunction callback){
        JPushInterface.setAlias(TiApplication.getInstance(),alias,new TagAliasCallback(){
            @Override
            public void gotResult(int arg0, String arg1,Set<String> arg2) {
                Log.d("JPush", "Jpush setAlias status: " + arg0);//狀態
                if(callback != null){
                    KrollDict map = new KrollDict();
                    map.put("code", arg0);         
                    callback.callAsync(getKrollObject(),map);
                }
            }
        });
    }
    
    @Kroll.method
    public void setTags(Object[] tags,final KrollFunction callback){
        Set set=new HashSet();
        for(Object n : tags){
           set.add(n.toString());
        }
        JPushInterface.setTags(TiApplication.getInstance(),set,new TagAliasCallback(){
            @Override
            public void gotResult(int arg0, String arg1,Set<String> arg2) {
                Log.d("JPush", "Jpush setTags status: " + arg0);//狀態
                if(callback != null){
                    KrollDict map = new KrollDict();
                    map.put("code", arg0);                
                    callback.callAsync(getKrollObject(),map);
                }
            }
        });
    }
}

 

 

OK,以後就是編譯模塊了,編譯成功後把獲得的模塊文件(位於dist文件夾裏的一個zip壓縮文件)放到你的Titanium項目的根目錄,而後運行你的項目,模塊就會自動安裝了,以後你還要在你項目的tiapp.xml文件裏引入你剛安裝的模塊。服務器

以後咱們還能夠測試一下是否是有用:app

var push = require('com.yeshcp.jpush');
//push.resumePush();
push.setAliasAndTags('chelsea',['grade1','grade2'],function(e){
    alert('setAliasAndTags' + JSON.stringify(e));
});

setTimeout(function(){
    push.setAlias('',function(e){
        alert('setAlias' + JSON.stringify(e));
    });
},5000);

setTimeout(function(){
    push.setTags([],function(e){
        alert('setTags' + JSON.stringify(e));
    });
},10000);

注意:極光推送須要配置AndroidManifest.xml文件,具體怎麼配置能夠去看極光推送的文檔。ide

最後附上我已經編譯好的模塊文件:com.yeshcp.jpush-android-1.1.zip函數

相關文章
相關標籤/搜索