user2718067
user2718067

Reputation: 1603

How to clicking the notification bar to return to the app?

I have successfully created an item on the notification bar with following code in which DownloadService extends the IntentService class.

    public class MyService extends Activity
    {
        ...
        void startService()
        { 
            ...
            Intent myIntent = new Intent(StoryGallery.this,DownloadService.class);
            myIntent.putExtra("story name", story.title);
            startService(myIntent);
        }
    }

Question is how to click the notification item to return to my app?

It seems there is lots of questions like this, but I can't find a straightforward answers on how to launch the app by clicking the notification item. I have tried to make "DownloadService implements OnClickListener", but there is not any response after clicking.

So Thanks in advance for reply.

Upvotes: 0

Views: 402

Answers (1)

Shivam Verma
Shivam Verma

Reputation: 8023

A Service should not implement an onClickListener as it has no UI. It just runs in the background. So, whenever you want to display a notification use :

    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);

    /** IMPORTANT : Create an intent which starts the activity **/
    Intent intent = new Intent(context, ActivityName.class);

    /** IMPORTANT : Ensure that you use getActivity with the PendingIntent. **/
    /**This pending intent will be fired when you click on the notification.**/ 
    builder.setContentIntent(PendingIntent.getActivity(context, 0, intent,
            0));
    builder.setContentTitle(title);
    builder.setContentText(message);
    builder.setSmallIcon(R.drawable.notification_icon);
    Notification notification = builder.build();
    NotificationManager manager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, notification);

Upvotes: 1

Related Questions