Notification opening launch activity when app is closed

I am Implementing google cloud messaging service (GCM) in my app. I used the gcmIntent service to create a pending intent and open an activity which is not a launch activity. When the app is open it works fine. But when app is closed it opens the launch activity instead of the desired activity. I tried all the solution i could find but nothing is working. I am Struggling for more than a week. Any help will be greatly appreciated.

My code

public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    Notification notification;
    NotificationCompat.Builder builder;

public GcmIntentService() {
    super("GcmIntentService");
}
public static final String TAG = "GCM Demo";

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) {  

        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {

        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {


        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            String [] message={extras.getString("abc"),extras.getString("zyx"),extras.getString("123"),extras.getString("456")};
            if(UserDetails.getPushNotificationStatus(this)){
                sendNotification(message);
            }
        }
    }
    GCMBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String[] msg) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(getBaseContext(), ShowShoutComment.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent =
            PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, 0);

    Uri notificationUri = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setLargeIcon(remote_picture)
    .setContentText(Html.fromHtml(msg[2]))
    .setContentTitle("Shout")
    .setSound(notificationUri)
    .setStyle(new      NotificationCompat.BigTextStyle().bigText(Html.fromHtml(msg[2])));

    mBuilder.setContentIntent(contentIntent);
    notification = new Notification();
    notification = mBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
    mNotificationManager.notify(NOTIFICATION_ID, notification);

}
}

I get the following stacktrace

05-20 09:47:44.926: I/ActivityManager(753): START u0 {cmp=com.shout.shout/.activities.ShowShoutComment bnds=[0,153][1080,441]} from pid -1
05-20 09:47:44.926: W/ActivityManager(753): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=com.shout.shout/.activities.ShowShoutComment bnds=[0,153][1080,441] }
05-20 09:47:44.976: I/ActivityManager(753): Start proc com.shout.shout for activity com.shout.shout/.activities.ShowShoutComment: pid=4742 uid=10195 gids={50195, 3003, 1028, 1015}
05-20 09:47:45.016: W/ActivityThread(4742): Application com.shout.shout can be debugged on port 8100...
05-20 09:47:45.146: I/ActivityManager(753): START u0 {flg=0x4000000 cmp=com.shout.shout/.activities.ShoutFeed} from pid 4742
05-20 09:47:45.626: I/ActivityManager(753): Displayed com.shout.shout/.activities.ShoutFeed: +476ms (total +668ms)

Upvotes: 1

Views: 3196

Answers (2)

curious_coder
curious_coder

Reputation: 2468

Made a few changes.

private void sendNotification(Context context,String[] msg) {

   ....
   Intent notificationIntent = new Intent(context, ShowShoutComment.class);
   ....
   PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   ...    
}

Upvotes: 0

Naveed Ali
Naveed Ali

Reputation: 2619

put this in your above (service class)

Intent intent2 = new Intent(context, yourDesiredActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent2, 0); 

hope this will help you.

Upvotes: 1

Related Questions