2014-12-04 19 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