4

I have next code:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        this).setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("My notification")
        .setContentText("Hello World!");

Why it doesn't work? It shows nothing. Testing on Android 2.2.1.

Update. Code of activity:

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;

public class LoginActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

    public void start(View view) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!");
    }
}

Button in layout:

<Button
    android:id="@+id/startButton"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/aboutButton"
    android:layout_alignLeft="@+id/aboutButton"
    android:layout_marginBottom="20dp"
    android:onClick="start"
    android:text="Start" />

UPDATE2. Error log:

02-04 11:40:40.752: D/dalvikvm(23054): GC_EXTERNAL_ALLOC freed 859 objects / 59168 bytes in 61ms
02-04 11:40:56.424: D/AndroidRuntime(23054): Shutting down VM
02-04 11:40:56.424: W/dalvikvm(23054): threadid=1: thread exiting with uncaught exception (group=0x4001d888)
02-04 11:40:56.432: E/AndroidRuntime(23054): FATAL EXCEPTION: main
02-04 11:40:56.432: E/AndroidRuntime(23054): java.lang.IllegalStateException: Could not execute method of the activity
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View$1.onClick(View.java:2082)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View.performClick(View.java:2461)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.view.View$PerformClick.run(View.java:8890)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Handler.handleCallback(Handler.java:587)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Looper.loop(Looper.java:123)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.app.ActivityThread.main(ActivityThread.java:4632)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at dalvik.system.NativeStart.main(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054): Caused by: java.lang.reflect.InvocationTargetException
02-04 11:40:56.432: E/AndroidRuntime(23054):    at pckg.mywebsites.LoginActivity.start(LoginActivity.java:28)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.view.View$1.onClick(View.java:2077)
02-04 11:40:56.432: E/AndroidRuntime(23054):    ... 11 more
02-04 11:40:56.432: E/AndroidRuntime(23054): Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=pckg.mywebsites id=222 notification=Notification(vibrate=null,sound=null,defaults=0x0)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Parcel.readException(Parcel.java:1264)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.os.Parcel.readException(Parcel.java:1248)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.app.NotificationManager.notify(NotificationManager.java:110)
02-04 11:40:56.432: E/AndroidRuntime(23054):    at android.app.NotificationManager.notify(NotificationManager.java:90)
02-04 11:40:56.432: E/AndroidRuntime(23054):    ... 15 more
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Maximus
  • 471
  • 1
  • 10
  • 25
  • 1
    The exceptions says it requires a content intent and you're not providing one – Zharf Feb 04 '13 at 10:33
  • possible duplicate of [Is setContentIntent(PendingIntent) required in NotificationCompat.Builder?](http://stackoverflow.com/questions/20032249/is-setcontentintentpendingintent-required-in-notificationcompat-builder) – GrIsHu Nov 19 '13 at 13:05

2 Answers2

13

It works:

Intent resultIntent = new Intent(this, LoginActivity.class);
PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setContentIntent(resultPendingIntent);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, mBuilder.build());
Maximus
  • 471
  • 1
  • 10
  • 25
0

When writing code for versions that came before API 11 you should include the support library and instead of calling the Notification.Builder you need to call NotificationCompact instead.

Try reading in the Android support Library page and going over the sample projects for these things.

EDITED:

Try out below code:

NotificationCompat.Builder builder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Notifications Example")
        .setContentText("This is a test notification");
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, builder.build());
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • It compiles ok, and I use android.support.v4.app.NotificationCompat. When I launch app on phone It shows nothing when I call "start" function. – Maximus Feb 04 '13 at 08:07
  • Have you tried with my code ? I have updated my answer. Please check it and try out . – GrIsHu Feb 04 '13 at 08:52
  • You can check out the Demo from this link its working awsome. https://github.com/sanathe06/AndroidGuide/tree/master/ExampleCompatNotificationBuilder – GrIsHu Feb 04 '13 at 09:53
  • The reason this was crashing : http://stackoverflow.com/questions/20032249/is-setcontentintentpendingintent-required-in-notificationcompat-builder – Mr_and_Mrs_D Nov 19 '13 at 12:38