Question

When user click on notification I want to start an Activity and two Intent Service.

Is it possible to do ? If yes Please can any one give me the idea to do this ?

Suppose I have no control on Activity. The Activity belongs to third party app.

Please don't tell me that start intent service from your Activity . I know this.

Was it helpful?

Solution

Follow the steps Hope this helps you

1) Create a BroadCastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Start Activity
        // Start Services
    }

}

2) Just Fire Broadcast on notification click.

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(context, MyBroadcastReceiver.class);
        PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        Notification notification = new Notification(icon, ticker, when);
        notification.setLatestEventInfo(mSmartAndroidActivity, title, message, contentIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);

Use PendingIntent.getBroadcast instead of PendingIntent.getActivity

3) In BroadcastReceiver's onReceive() method

-> call your third party activity

-> Start services

OTHER TIPS

The answer given by Biraj will also do the job.

But for that You have to declare the BroadcastReceiver in your manifest file as well as all activity and services which you are starting from BroadcastReceiver.

So I have not choosen the above way.

How I have implemented ?

I have started an Intent service from where I am starting activity and making a synchronous request to server . By this way I don't have to declare a BroadcastReceiver in my maniest file.

I believe in writing less code so I have chosen the above Intent Service way

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top