0

I am implementing one sdk where when user click on notification then certain activity will be open.I am just wondering how can i pass different -different activity on intent.Here is my sample code:-

void fireNotification(Context _context, String appname,
            String appdescription) {

        Intent resultIntent = new Intent(_context, ResultActivity.class);
        try {

            PendingIntent contentIntent = PendingIntent.getActivity(_context,0, resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder = new NotificationCompat.Builder(_context);
            }

Please suggest me how can pass different activity in intent.

Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
Rishabh Agrawal
  • 861
  • 2
  • 15
  • 25
  • when you set the notification you may set a flag, then again when you are to show the activity, on basis of that variable you may switch on different activities. – Rachit Mishra Aug 26 '13 at 05:55
  • Check these link 1. http://stackoverflow.com/questions/12043671/notification-click-activity-already-open 2. http://stackoverflow.com/questions/2917049/android-status-bar-notifications-opening-the-correct-activity-when-selecting-a 3. http://stackoverflow.com/questions/3378886/launch-activity-when-notification-is-clicked 4. http://stackoverflow.com/questions/7262412/android-why-activity-is-open-after-click-notification 5.http://stackoverflow.com/questions/3666250/call-activity-when-notification-click-event – surhidamatya Aug 26 '13 at 05:58

3 Answers3

0

I assume that you wanted to load another Activity instead of ResultActivity, you could just change the ResultActivity.class to the class name of the other activity.

If you are looking to have the ability to determine which activity to load after user taps on the notification, you can create a new Activity that will determine which Activity to load after it launches, kinda like an Activity to "redirect" the screen.

Ivan Foong
  • 209
  • 2
  • 5
  • Hey actually i am developing one sdk then i create jar file .So i wan t ensure developer only put jar file & put activity on method so we could be get on our sdk – Rishabh Agrawal Aug 26 '13 at 05:58
  • I assume that your fireNotification will be used in your SDK to launch user's Activity? you would need to have build in a configuration ability to let your SDK's users to specify which activity should be launch upon those events. Your SDK could then fetch the activity like "com.example.myactivity1" Refer to the documentation for Intent constructor at – Ivan Foong Aug 26 '13 at 06:09
0

Try this..

void fireNotification(Context _context, String appname,
            String appdescription) {

        Intent resultIntent = null;

        if(something)
             resultIntent = new Intent(_context, SomeActivity1.class);
        else
             resultIntent = new Intent(_context, SomeActivity2.class);

        try {

            PendingIntent contentIntent = PendingIntent.getActivity(

            _context,

            0,

            resultIntent,

            PendingIntent.FLAG_UPDATE_CURRENT);

            mBuilder = new NotificationCompat.Builder(

            _context);

}
Kavin
  • 544
  • 3
  • 5
0

Here is some code I use to create notifications. It uses the v4 Compatibility library. As you can see you have to recreate the PentingIntent if you want to change the Activity to be launched. There is nothing wrong with cancelling and reissuing the intent as I have done. If you dont have allowing ticket text user likely not even notice it. Also, note that I know the compatibility builder lets you assign custom views but this crashes for me every time, directly assigning it seems more stable.

public static void setupNotification(Context context) {
    if (mNotificationManager == null) {
        mNotificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
    }
    mNotificationManager.cancel(R.layout.main);
    int icon = R.drawable.ic_stat_notify_connected;
    String tickerText = context.getString(R.string.TickerText);
    createNotification(context, tickerText, icon);
    mNotificationManager.notify(R.layout.main, mNotification);
}

private static void createNotification(Context context, String tickerText, int icon) {
    Intent notificationIntent = new Intent();
    notificationIntent = new Intent(context, NotificationOptionsActivity.class);

    String contentTitle = context.getString(R.string.MessageTitle);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    if (mNotification == null) {
        mNotification = new NotificationCompat.Builder(context.getApplicationContext()).setContentTitle(contentTitle).setSmallIcon(icon).setContentIntent(contentIntent).build();

        mNotification.flags |= Notification.FLAG_ONGOING_EVENT;

        RemoteViews contentView = new RemoteViews(context.getApplicationContext().getPackageName(), R.layout.notification_custom_layout);
        mNotification.contentView = contentView; 
    } else {
        mNotification.contentIntent = contentIntent;
    }
}

Note: You have to use Intent.FLAG_ACTIVITY_NEW_TASK nothing else will work. You can remove the code for the custom view if you don't have a custom view.

If you do have a custom view, you can set values in it as follows:

    mNotification.contentView.setTextViewText(R.id.noti_user, user);
    //default image
    mNotification.contentView.setImageViewResource(R.id.noti_image, R.drawable.ic_user_icon);
Ali
  • 12,354
  • 9
  • 54
  • 83
  • Hey actually i am developing one sdk then i create jar file .So i wan t ensure developer only put jar file & put activity on method so we could be get on our sdk – Rishabh Agrawal Aug 26 '13 at 05:59
  • Oh, you can just take a `Class` object as a paramter so `public void myMethod(Context context, Class> myActivity)` then `notificationIntent = new Intent(context, myActivity);` call the method with `myMethod(context, ActivityName.class);` – Ali Aug 26 '13 at 06:01