Jacob
Jacob

Reputation: 461

Trouble getting a new activity to pop up on notification click

So I want to make this application that receives SMS texts, and when it does, it displays a notification, and when the user clicks on it, a new activity is popped up with the SMS message. So I have most if it done like the BroadcastReceiver class which works in receiving SMS texts and it displays a notification all fine and dandy.

public class SMSReceiver extends BroadcastReceiver {

    static final int notificationId = 1;

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

        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String message = currentMessage.getDisplayMessageBody();
                    newNotification(context, phoneNumber, message);

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void newNotification(Context context, String title, String text) {

        NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notifyIntent = new Intent(context, NotificationActivity.class);
        notifyIntent.putExtra("SMSText", text);
        notifyIntent.setAction(text);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notifyIntent, 0);

        Notification notification = new NotificationCompat.Builder(context)
                .setContentIntent(contentIntent)
                .setContentText(text)
                .setContentTitle(title)
                .setTicker(text)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .build();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notifyMgr.notify(notificationId, notification);

    }

}

The only problem is when I click on the notification. The first time it works perfectly, the NotificationActivity class pops up displaying the SMS message. But after that, the information never updates. I assume the app realizes that it already has NotificationActivity open, so it doesn't open another one. Even though I properly add the flag FLAG_ACTIVITY_NEW_TASK when I create the new notification. Also here is my NotificationActivity class

public class NotificationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        String newString;
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                newString = null;
            } else {
                newString = extras.getString("SMSText");
            }
        } else {
            newString = (String) savedInstanceState.getSerializable("SMSText");
        }

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(newString);

    }
}

I also do have a launcher activity, MainActivity, but felt it was unnecessary to show because all it does is register the SMSReceiver. Any help would be greatly appreciated, thanks!

Upvotes: 0

Views: 37

Answers (2)

Xenolion
Xenolion

Reputation: 12735

You can try overriding the method onNewIntent it is called whenever a new Intent is sent to the same Activity the original Intent will be kept using the getIntent method but this one will be called whenever there is a new Intent.

@Override
protected void onNewIntent(Intent intent) 
{
    //Do what you want to do with this new Intent here!
}

Upvotes: 1

mehul chauhan
mehul chauhan

Reputation: 1802

change this line

    PendingIntent contentIntent = PendingIntent.getActivity(context, 
    notificationId , 
    notifyIntent, PendingIntent.FLAG_ONE_SHOT););

Upvotes: 0

Related Questions