2

My query is that I am trying to redirect the notification intent depending on the different conditions, I am working on the alarm system where I need to redirect the notification intent to some activity say A. If the user clicks it during the alarm playing and on completion, the notification should be redirected to some other activity if still available there. Here is my code. Any help will be appreciated!

   private void DisplayNotification(String AlarmName, Context context) {
    // Context context = context.getApplicationContext();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Intent mIntent;
    SharedPreferences alarmindicator = context.getSharedPreferences(
            "notifyintent", context.MODE_PRIVATE);
    int code;
    SharedPreferences.Editor editor = alarmindicator.edit();
    if (alarmindicator.getBoolean("notifyintentPlaying", true)) {
        mIntent = new Intent(context, AlarmAlertActivity.class);
        Toast.makeText(context, "inalmact", 0).show();
        code = 1;
    }

    else {
        editor.putBoolean("notifyintentPlaying", true);
        editor.commit();
        mIntent = new Intent(context, MainActivity.class);
        // AlarmAlertActivity.this.finish();
        code = 2;
        Toast.makeText(context, "in main act", 0).show();

    }

    Bundle bundle = new Bundle();

    bundle.putString("test", "test");
    mIntent.putExtras(bundle);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, code,
            mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Resources res = context.getResources();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);

    builder.setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.notifyiconlarg)
            .setLargeIcon(
                    BitmapFactory.decodeResource(res,
                            R.drawable.notifyiconlarg))
            // .setTicker(res.getString(R.string.notification_title))
            .setTicker(AlarmName).setAutoCancel(true)
            .setContentTitle(AlarmName)
            .setContentText("Time to offer " + AlarmName + " Prayers");

    notificationManager = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);

    notificationManager.notify(123, builder.build());
}
Bhavik Kamdar
  • 301
  • 1
  • 7
  • 15

1 Answers1

2

You have to fire an broadcast receiver from the notification and in the onReceive() method of BroadcastReceiver you have to decide which activity to start....

UPDATE

Write a BroadcastReceiver like this

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int code;
        Intent mIntent;
        SharedPreferences.Editor editor = alarmindicator.edit();
        if (alarmindicator.getBoolean("notifyintentPlaying", true)) {
            mIntent = new Intent(context, AlarmAlertActivity.class);
            Toast.makeText(context, "inalmact", 0).show();
            code = 1;
        } else {
            editor.putBoolean("notifyintentPlaying", true);
            editor.commit();
            mIntent = new Intent(context, MainActivity.class);
            // AlarmAlertActivity.this.finish();
            code = 2;
            Toast.makeText(context, "in main act", 0).show();
        }
        context.startActivity(intent);
    }

And create the Pending intent like this

Intent myIntent=new Intent("some_action")
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, code,
                myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Chandrakanth
  • 3,711
  • 2
  • 18
  • 31