2

I am trying to launch a new activity on click of notification. But problem is that notification intent is not working and when i click on notification it do nothing. Means its not launch new activity(Notificationjump.class).
Here is my code

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            notrify();
        }
    });
}

private void notrify() {
    int notificationid = 001;
    NotificationManager mNotifyManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);    
    Intent resultintent = new Intent(getApplicationContext(),Notificationjump.class);
    resultintent.setAction(Intent.ACTION_VIEW);
  //  resultintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this,1,resultintent,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notify)
        .setContentTitle("Mynotification")
        .setContentText("Hello world");
    mBuilder.setContentIntent(resultPendingIntent);
    mBuilder.setOngoing(true);
    mNotifyManager.notify(notificationid,mBuilder.build());
}

Here is my Notificationjump.class

package com.example.notification;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class Notificationjump extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notifyjump);
    System.out.println("hellllllllllllo");
    finish();
}


}

Can somebody tell me why pending intent is not working?

Devendra Dagur
  • 840
  • 1
  • 14
  • 35

1 Answers1

3

Means its not launch new activity(Notificationjump.class).

You are using Intent.FLAG_ACTIVITY_SINGLE_TOP and this cause this, especially when you most likely have the same acivity posting notification on click, so it is front most while you tap notification.

If set, the activity will not be launched if it is already running at the top of the history stack.

So remove this flag if you want different behavior. See docs: https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP and here: https://developer.android.com/guide/components/tasks-and-back-stack.html#TaskLaunchModes

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Its working now .....the problem was that I by mistake used finish(); in my Notificationjump.class thats why I am not able to stay in that class. And this action is so fast and I thought notification intent is not working. And in hurry I asked this question. And I am sorry for that. – Devendra Dagur Aug 08 '15 at 12:51
  • That was actually my guess and main reason I asked to see this class code :) – Marcin Orlowski Aug 08 '15 at 14:03