Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Status bar notification plugin error with Cordova

I'm trying to add the Status Bar Notification Plugin for Cordova to my Android App, but I get an error with it's code.

Here's the problematic code:

  Notification noti = new Notification.Builder(context)
    .setContentTitle(contentTitle)
    .setContentText(contentText)
    .setSmallIcon(icon)
    .build();

The error is on the .build(), Eclipse tells me:

"The method build() is undefined for the type Notification.Builder"

like image 305
aridev22 Avatar asked Jul 20 '12 14:07

aridev22


2 Answers

I am having the same issue. It looks like a mismatch the sdk versions and now depreciated methods.

getNotification() is the method to call since API 11 build() was added in API 16

if you are like me, you are using a version < 16, so use .getNotification() instead.

Im not going to worry about API 16 right now but I bet if I download 16 and set my target to such, build() will work.

Let me know if it works for you.

like image 51
johnw182 Avatar answered Oct 13 '22 08:10

johnw182


For me .getNotification() didn't resolve the problem, because I need a solution for API 10 and higher.

I found a way to deal with it. If someone else has the same issue, I recommend to do this :

1) Go through instructions for StatusBarNotification (click)

2) Modify StatusBarNotification.java

  • Add

    private Notification noti;

    private PendingIntent contentIntent;

At the bottom of StatusBarNotification class, for example before NotificationManager declaration
  • Modify showNotification method

Comment or delete:

import android.app.Notification.Builder;

and

Notification noti = new Notification.Builder(context) .setContentTitle(contentTitle) .setContentText(contentText) .setSmallIcon(icon) .build();

Instead of this part, paste:

noti = new Notification(android.R.drawable.btn_star_big_on, contentText, System.currentTimeMillis() );
noti.flags = Notification.FLAG_AUTO_CANCEL;


Intent notificationIntent = new Intent(context, !yourMainActivityClass!.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

noti.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  • Change !yourMainActivityClass! to your class
  • Add calling method in index.html For tests you can make JQM button with

    onclick='window.plugins.statusBarNotification.notify("Put your title here", "Put your message here");return false;'

I know that this solution is using depreciated methods, but I spent a lof of hours to make it works and I didn't see another solution for API 10. If somebody has better idea, share with me ;)

like image 37
witekm Avatar answered Oct 13 '22 06:10

witekm