0

I want to send local notifications using cordovaplugin by extending CordovaPlugin on my HelloWorldPlugin.java.. But it seems my code for local notifications doesnt work. If i put this piece of code in the auto-generated AndroidCordova that extends CordovaActivity it works. Here is the code below

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;

It is returning 2 errors. First one is it says "The constructor Notification.Builder(HelloWorldPlugin) is undefined" and NOTIFICATION_SERVICE cannot be resolved to a variable. Also i added the Context context and used context on the part after getActivity, i used this on my other plugin that extends CordovaActivity. I need help please im stuck here for 4 days now..

Ziddorino
  • 73
  • 4
  • 13

2 Answers2

0

Assuming you have context from application which will be adding your plugin..

you can try the following...

  1. Check for context!=null
  2. Replace Notification noti = new Notification.Builder(this) with Notification noti = new Notification.Builder(context)
  3. NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Hope this helps

  • this is my current code and this returns no error but currently also no notification whenever i click my button.. Notification noti = new Notification.Builder(context) NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); – Ziddorino Dec 04 '14 at 00:53
  • Also replace...PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0); with PendingIntent negativePendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); – Anurag Kondeya Dec 04 '14 at 01:00
  • ok will try that.. Currently only running on emulator so im a bit slow. Thanks. Will tell what happens – Ziddorino Dec 04 '14 at 01:06
  • oh, and also, if it matters. I moved Context context up to before the function because if i put it inside the contexts get a red line saying the local variable context may or may not have been initialized. Did i do it right or should i set a value on Context context ? – Ziddorino Dec 04 '14 at 01:33
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()); 
}
  • Thank you for all your efforts. Sorry but I have been using eclipse for just a few days and I still am new to stuff. Is this extending CordovaPlugin? Because my boss requires(i am an 19y/o ojt) me to use cordovaplugin.. Also what do i put on yourfunctionreturncontexthere();.. trying your code now.. Thanks a lot.. – Ziddorino Dec 04 '14 at 02:20
  • Yes it is extending CordovaPlugin. – Anurag Kondeya Dec 04 '14 at 03:26
  • Your functionreturncontexthere is pseudo function for getting context from your application. You can ignore this function and just use context as you were using before. – Anurag Kondeya Dec 04 '14 at 03:32
  • oh my god im the dumbest. I put my onclick on textfield instead of my button so it ofc is not working. thanks for all your efforts again anurag. – Ziddorino Dec 04 '14 at 03:34