zoukankan      html  css  js  c++  java
  • 为Titanium创建自己的安卓推送模块

    在手机应用中,推送是一个非常重要的功能。相对来说ios应用的推送功能很容易做,因为它统一都是用苹果的APNS服务实现的。但安卓这边就比较混乱了,虽然谷歌也推出了类似苹果的官方推送服务,但由于谷歌的服务器在国内经常被墙,所以用谷歌官方提供的推送服务在国内是不可行的,所以安卓的应用就只能自己实现推送服务了。但如果完全由自己实现推送功能,那成本是非常大的。所以一般我们会选择一些第三方推送服务,比如极光推送就是一个非常不错的选择。

    首先,下载极光推送的安卓sdk,解压,找到打开libs文件夹,里面有两个东西

    QQ截图20131221123659

    把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

  • 相关阅读:
    Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
    旋转二维数组
    replace empty char with new string,unsafe method和native implementation的性能比较
    判断一字符串是否可以另一字符串重新排列而成
    移除重复字符的几个算法简单比较
    也来纠结一下字符串翻转
    判断重复字符存在:更有意义一点
    程序员常去网站汇总
    sublime
    针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)
  • 原文地址:https://www.cnblogs.com/2050/p/3485019.html
Copyright © 2011-2022 走看看