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
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With