Java 混合本机Android eclipse中使用CordovaPlugin的本地通知
我想通过在HelloWorldPlugin.java上扩展cordovaplugin来使用cordovaplugin发送本地通知。。但我的本地通知代码似乎不起作用。如果我把这段代码放在自动生成的AndroidCordova中,它可以扩展Cordova活动。下面是代码Java 混合本机Android eclipse中使用CordovaPlugin的本地通知,java,android,eclipse,cordova,localnotification,Java,Android,Eclipse,Cordova,Localnotification,我想通过在HelloWorldPlugin.java上扩展cordovaplugin来使用cordovaplugin发送本地通知。。但我的本地通知代码似乎不起作用。如果我把这段代码放在自动生成的AndroidCordova中,它可以扩展Cordova活动。下面是代码 public class HelloWorldPlugin extends CordovaPlugin { @Override public boolean execute(String action, JSONArray arg
public class HelloWorldPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
throws JSONException {
if (action.equals("sayHello")){
Context context //Added:
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setTicker("Test Ticker Notification")
.setSmallIcon(R.drawable.icon)
.setContentTitle("Test Title Notification")
.setContentText("Test Content Notification")
.setContentIntent(pIntent).build();
noti.flags=Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
return true;
}
return false;
它返回2个错误。第一个是它说构造函数Notification.BuilderHelloWorldPlugin未定义,通知服务无法解析为变量。
另外,我在getActivity之后的部分添加了上下文并使用了上下文,我在扩展CordovaActivity的其他插件上使用了上下文。我需要帮助,我现在被困在这里4天了。假设您有将添加插件的应用程序的上下文 你可以试试下面的 检查上下文=无效的 将Notification noti=new Notification.Builder替换为Notification noti=new Notification.Buildercontext NotificationManager NotificationManager=NotificationManager context.getSystemServiceContext.NOTIFICATION\u服务;
希望这有帮助这是我的当前代码,每次单击我的按钮都不会返回任何错误,但当前也不会收到任何通知。。Notification noti=新通知。Buildercontext NotificationManager NotificationManager=NotificationManagercontext.getSystemServiceContext.Notification\u服务;同时替换…PendingEvent pIntent=PendingEvent.getActivitycontext,0,intent,0;PendingEvent NegativePendingEvent=PendingEvent.getBroadcastcontext,0,intent,PendingEvent.FLAG_UPDATE_CURRENT;好的,我试试看。。目前只在emulator上运行,所以我有点慢。谢谢会告诉你发生了什么,如果有关系的话。我把上下文移到函数之前,因为如果我把它放在上下文中,会得到一条红线,表示局部变量上下文可能已经初始化,也可能没有初始化。我做得对吗?还是应该根据上下文设定一个值?谢谢你们的努力。很抱歉,我已经使用eclipse几天了,而且我还是个新手。这是一个插件吗?因为我的老板要求我是19岁的ojt我使用cordovaplugin。。还有,我应该在函数ReturnContexthere;上添加什么;。。正在尝试您的代码。。非常感谢。是的,它正在扩展CordovaPlugin。你的函数ReturnContext这里有一个伪函数,用于从你的应用程序中获取上下文。你可以忽略这个函数,像以前一样使用上下文。我把onclick放在textfield上,而不是我的按钮,这样它就不工作了。再次感谢你的努力,阿努拉格。
This is how i got your code working using notificationcompat
Note: you will have to add android-support-v4.jar next to your java file and add following line in plugin.xml
<source-file src="src/android/StreethawkLibrary.jar" target-dir="libs/"/>
Java code:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
if (action.equals("sayHello")){
Context context = yourfunctionReturnsContexthere();
if(context==null){
Log.e(TAG,"context is null.. returning");
}
Intent intent = new Intent();
intent.setAction("com.streethawk.intent.action.gcm.STREETHAWK_ACCEPTED");
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setTicker("Test Ticker Notification");
builder.setContentTitle("Test Title Notification");
builder.setContentText("Test Content Notification");
builder.setContentIntent(pIntent).build();
builder.setAutoCancel(true);
try {
PackageInfo packageInfo =
context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
builder.setSmallIcon(packageInfo.applicationInfo.icon);
} catch (NameNotFoundException e) {
// should never happen
throw new RuntimeException("Could not get package name: " + e);
}
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}