Minho
Minho

Reputation: 927

Notification Activate

i want to call the activity when user pull down the notification and click on that notification...how can i do that?

here's my code :

public class SetReminder extends AppCompatActivity {
int notifyID = 1088;

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

    TextView helpsubtitle = (TextView)findViewById(R.id.subtitle_help);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "beyond_the_mountains.ttf");
    helpsubtitle.setTypeface(typeface);

    NotificationCompat.Builder mBuilder =

            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.diamond)
                    .setContentTitle("Crystallise")
                    .setContentText("making your thoughts crystal clear");
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notifyID, mBuilder.build());
}
}

Upvotes: 0

Views: 54

Answers (1)

adhirajsinghchauhan
adhirajsinghchauhan

Reputation: 725

First, you need to create an Intent which specifies the activity you want to launch when the user clicks on the notification. Assuming you want MainActivity to open when the notification is clicked, use the following code to create the intent:
Intent intent = new Intent(SetReminder.this, MainActivity.class);

Then, you must specify a PendingIntent, which is a token you give to the NotificationManager, or any other foreign application in general:

PendingIntent pendingIntent = PendingIntent.getActivity(
        context, 0,
        intent, PendingIntent.FLAG_CANCEL_CURRENT
);

Apply this pendingIntent to your notification using .setContentIntent(pendingIntent).

Your final code should look like this:

public class SetReminder extends AppCompatActivity {
    int notifyID = 1088;

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

        TextView helpsubtitle = (TextView)findViewById(R.id.subtitle_help);
        Typeface typeface = Typeface.createFromAsset(getAssets(), "beyond_the_mountains.ttf");
helpsubtitle.setTypeface(typeface);

        Intent intent = new Intent(SetReminder.this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context, 0,
                intent, PendingIntent.FLAG_CANCEL_CURRENT
        );
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.diamond)
                .setContentIntent(pendingIntent)
                .setContentTitle("Crystallise")
                .setContentText("making your thoughts crystal clear");
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(notifyID, mBuilder.build());
    }
}

Upvotes: 1

Related Questions