Alex
Alex

Reputation: 44265

How to create a notification in android?

I am trying to create a simple notification in android. The purpose is to show a small icon on top of the phone screen when something is happening. Then I can tap on the notification which will either start the app or brings it in the foreground.

For just testing purposes I am following this hard-to-understand-and-not-simple example and also found this entry for the first error I get.

Anyway here is the code:

    Notification mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon1)
            .setContentTitle("My notification")
            .setContentText("Hello World!");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    int mId = 1001;
    mNotificationManager.notify(mId, mBuilder.build());

and here is what I am importing:

...
import android.app.Notification;
import android.app.NotificationManager;
....
import android.support.v7.app.ActionBar;
//import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.support.v7.widget.Toolbar;

The current error is

Error:(117, 32) error: incompatible types
required: Notification
found:    Builder

How to fix this problem?

Upvotes: 1

Views: 2232

Answers (4)

Abhineet Kumar
Abhineet Kumar

Reputation: 29

Continuing with answer from @Blackbelt, android.support.v7.app.NotificationCompat.Builder is a subclass of android.support.v4.app.NotificationCompat.Builder and its member functions like setSmallIcon(R.drawable.icon1) returns instances of android.support.v4.app.NotificationCompat.Builder which cannot be casted to android.support.v7.app.NotificationCompat.Builder. So, best practice would be:

import android.support.v7.app.NotificationCompat;
.
.
.
NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(getContext());

mBuilder.setSmallIcon(R.drawable.icon1)
   .setContentTitle("My notification")
   .setContentText("Hello World!");

Upvotes: 1

Herin
Herin

Reputation: 141

Use the below code snippet it would work for creating the notification in android

import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;

//build your notification here.
                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(getContext())
                                .setSmallIcon(R.drawable.icon1)
                                .setContentTitle("My notification")
                                .setContentText("Hello World!")
                                .setAutoCancel(true); //notification will be removed when once you enter application.

Hope this helps you..

Cheers

Upvotes: 0

Aereal
Aereal

Reputation: 103

I had the same issue following Udacity tutorial on Android and found out that I was missing a cast. You should change your code to

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

You are using a NotificationCompat and you are assigning to a Notification

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157447

Change

Notification mBuilder 

with

NotificationCompat.Builder mBuilder 

Since you are instantiating new NotificationCompat.Builder(this) you can't assign it to a Notification object

Upvotes: 3

Related Questions