最近在学习用CORDOVA(PHONEGAP)结合SENCHA TOUCH开发应用,想实现一个安卓下的消息通知功能,这个可以通过CORDOVA的插件来实现。

插件目录结构如下:

notifyplugin

  • plugin.xml
  • www/notifysrv.js
  • src/android/NotifysrvPlugin.java
  • libs/android-support-v4.jar

先编写plugin.xml

<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"    id="com.elon.cordova.plugin" version="0.0.1">    <name>NotifysrvPlugin</name>    <description>NotifysrvPlugin Description</description>    <author>elon</author>    <license>Apache 2.0 License</license>    <engines>        <engine name="cordova" version=">=3.0.0" />    </engines>    <js-module src="www/notifysrv.js" name="notifysrv">        <clobbers target="Notify" />    </js-module>    <platform name="android">        <source-file src="src/android/NotifysrvPlugin.java"         target-dir="src/com/elon/cordova/plugin" />        <config-file target="res/xml/config.xml" parent="/*">            <feature name="NotifysrvPlugin">                <param name="android-package" value="com.elon.cordova.plugin.NotifysrvPlugin"/>            </feature>        </config-file>        <config-file target="AndroidManifest.xml" parent="/*">            <uses-permission android:name="android.permission.VIBRATE" />        </config-file>    </platform></plugin>

NotifysrvPlugin.java

package com.elon.cordova.plugin;import org.apache.cordova.CordovaPlugin;import org.apache.cordova.CallbackContext;import org.apache.cordova.CordovaWebView;import org.apache.cordova.CordovaInterface;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.content.Context;import android.support.v4.app.NotificationCompat;public class NotifysrvPlugin extends CordovaPlugin {    public static final String TAG = "NotifysrvPlugin";    public static final String iconname = "icon";//icon res name        public NotificationManager nm;    public Context m_context;    public void initialize(CordovaInterface cordova, CordovaWebView webView) {        super.initialize(cordova, webView);        m_context = this.cordova.getActivity().getApplicationContext();        nm = (NotificationManager) m_context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);    }    @Override    public boolean execute(String action, JSONArray args,     CallbackContext callbackContext) throws JSONException {        if ("send".equals(action)) {            String title = args.getString(0);              String text = args.getString(1);              PendingIntent  m_PendingIntent=PendingIntent.getActivity(this.cordova.getActivity(),                      0, this.cordova.getActivity().getIntent(), 0);            int iconResID = m_context.getResources().getIdentifier(iconname,"drawable", m_context.getPackageName());            Notification notification = new NotificationCompat.Builder(m_context)            .setContentTitle(title)            .setContentText(text)            .setDefaults(Notification.DEFAULT_ALL) //设置默认铃声,震动等            .setSmallIcon(iconResID)            .setContentIntent(m_PendingIntent)            .setAutoCancel(true)        //    .setLargeIcon(aBitmap)            .build();                        nm.notify(1, notification);            callbackContext.success();            return true;        }        return false;    }}

notifysrv.js

var argscheck = require('cordova/argscheck');var exec = require('cordova/exec');var Notify = function() {};Notify.prototype.send = function(message, success, error) {    //argscheck.checkArgs('AFF', 'notify.send', arguments);     console.log("send notification["+message[1]+"]");    if(!message)        error && error("please input message");    else        exec(success, error, 'NotifysrvPlugin', 'send', message);};var notify = new Notify();module.exports = notify;

将插件加入cordova工程的办法

进入CMD,进入cordova工程文件夹,然后输入如下命令

cordova plugin add [插件目录]

使用本插件的方法:

            var msg = ["新消息标题","新的消息内容"];            Notify.send(msg,function(){                console.log("成功");              },function(msg){                  console.log(msg || "失败");              });      

更多相关文章

  1. [Android实例] Android网络收音机项目(内含源码)
  2. android SlidingMenu 侧边菜单划出后剩余部分阴影设置
  3. android对json数据的解析
  4. Android(安卓)Studio插件整理
  5. Android中有关Handler的使用(二)
  6. Android消息处理机制
  7. Android中删除常用联系人(下)
  8. Android(安卓)uevent架构
  9. 解析Android消息处理机制:Handler/Thread/Looper & MessageQueue

随机推荐

  1. FFmpeg4Android:Android摄像头直播
  2. android gallery相关操作
  3. 最新版 Android SDK 无法获取SDK包 的解
  4. js判断Android和Ios
  5. android:exported 属性详解
  6. Android中实现短信发送的一种方式
  7. Understanding Android's LayoutInflater
  8. Android开发环境构建
  9. android之sql例子
  10. Android 数据库操作 创建 添加 删除 查询