cordova的android notify消息通知插件

最近在学习用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 || "失败");  
            });  
    

 

cordova的android notify消息通知插件

上一篇:android圆角View实现及不同版本这间的兼容(android3.0过后的版本)


下一篇:android:windowSoftInputMode属性详解 软键盘