Local Notification using CordovaPlugin in Hybrid-Native Android eclipse

322 Views Asked by At

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..

2

There are 2 best solutions below

4
On BEST ANSWER
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()); 
}
4
On

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