在手機應用中,推送是一個非常重要的功能。相對來說ios應用的推送功能很容易做,因為它統一都是用蘋果的APNS服務實現的。但安卓這邊就比較混亂了,雖然谷歌也推出了類似蘋果的官方推送服務,但由於谷歌的服務器在國內經常被牆,所以用谷歌官方提供的推送服務在國內是不可行的,所以安卓的應用就只能自己實現推送服務了。但如果完全由自己實現推送功能,那成本是非常大的。所以一般我們會選擇一些第三方推送服務,比如極光推送就是一個非常不錯的選擇。
首先,下載極光推送的安卓sdk,解壓,找到打開libs文件夾,里面有兩個東西
把armeabi這里文件夾復制到你的Titanium安卓模塊項目根目錄的libs文件夾里面,如果沒有這個文件夾可以創建。模塊項目的libs文件夾是用來放第三方動態鏈接庫文件的地方,也就是后綴名為.so的文件。然后把 jpush-sdk-release1.5.5.jar 這個文件復制到模塊項目根目錄的 lib 文件夾里面。這樣我們就可以在模塊中使用極光推送的api了。
由於極光推送的api比較簡單,所以我們只需要在模塊的主文件中把極光推送提供的api與我們能在titanium項目中使用的js方法進行綁定就行了。
模塊主文件名為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文件里引入你剛安裝的模塊。
之后我們還可以測試一下是不是有用:
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文件,具體怎么配置可以去看極光推送的文檔。
最后附上我已經編譯好的模塊文件:com.yeshcp.jpush-android-1.1.zip