2014-12-04 57 views
0

我想通过在我的HelloWorldPlugin.java上扩展CordovaPlugin来使用cordovaplugin发送本地通知..但是看起来我的本地通知代码不起作用。如果我把这段代码放在自动生成的扩展CordovaActivity的AndroidCordova中,它就可以工作。这里是下面的代码在Hybrid-Native Android eclipse中使用CordovaPlugin的本地通知

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.Builder(HelloWorldPlugin)未定义”,NOTIFICATION_SERVICE不能解析为变量。 另外我在getActivity之后在零件上添加了上下文上下文和使用的上下文,我在扩展CordovaActivity的另一个插件上使用了这个。我需要帮助,请即时困在这里现在4天..

回答

0
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()); 
} 
+0

谢谢你所有的努力。对不起,但我一直在使用eclipse几天,我仍然是新东西。这是扩展CordovaPlugin?因为我的老板需要(我是一个19y/o ojt)我使用cordovaplugin ..还有什么我放在yourfunctionreturncontexthere(); .. 现在尝试你的代码..非常感谢 – Ziddorino 2014-12-04 02:20:36

+0

是的,它扩展了CordovaPlugin – 2014-12-04 03:26:32

+0

你的functionreturncontext是伪函数,用于从你的应用程序获取上下文,你可以忽略这个函数,只使用conte xt正如你之前使用的那样。 – 2014-12-04 03:32:52

0

假设你有一个从应用程序,它会增加你的插件上下文..

你可以尝试以下...

  1. 检查上下文!= NULL
  2. 更换通知的NotI =新Notification.Builder(本)与通知的NotI =新Notification.Builder(上下文)
  3. NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICA TION_SERVICE);

希望这有助于

+0

这是我当前的代码,这不返回任何错误,但目前还没有通知每当我点击我的按钮.. 通知的NotI =新Notification.Builder(上下文) NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); – Ziddorino 2014-12-04 00:53:39

+0

我做了第2步和第3步。没有错误,通知无效。 – Ziddorino 2014-12-04 00:55:53

+0

也可以替换... PendingIntent pIntent = PendingIntent.getActivity(context,0,intent,0); PendingIntent negativePendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); – 2014-12-04 01:00:46