• 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 || "失败");  
                });  
        
    复制代码
  • 相关阅读:
    C#简单读取MongoDB数据
    百度地图自定义图标
    递归实现DropDownList层级
    [MYSQL]-EXPLAIN用法
    java对 zip文件的压缩和解压(ant解决中文乱码)
    将Excel表结构导入到Powerdesigner
    weblogic解决jar包冲突
    深入理解javascript原型和闭包(3)——prototype原型
    深入理解javascript原型和闭包(2)——函数和对象的关系
    深入理解JavaScript的原型和闭包(一)
  • 原文地址:https://www.cnblogs.com/sunshq/p/3939764.html
Copyright © 2020-2023  润新知