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

复制代码


    NotifysrvPlugin
    NotifysrvPlugin Description
    elon
    Apache 2.0 License
    
        
    
    
        
    
    
        
        
            
                
            
        
        
            
        
    
复制代码

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 || "失败");  
            });  
    
复制代码

 

转载于:https://www.cnblogs.com/sunshq/p/3939764.html

你可能感兴趣的:(移动开发,java,json)