4

I'm delivering a notification which has 2 action buttons namely "Accept" and "Reject".

I'm following this Github repo.

When user clicks "Accept", certain conditions are checked and the logic is performed accordingly.

UPDATE 2.0 - The problem is that upon clicking "Accept" button, operation is happening successfully but the notification isn't disappearing from the status bar because the id generating here: m = (new Random()).nextInt(10000); is different from here: actionIntent.putExtra("id", NotificationARBroadcastReceiver.m); every single time!

Here's the code for notification:

Intent notificationIntent = new Intent(getBaseContext(), NotificationARBroadcastReceiver.class);
notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, getNotificationNewRequestService());
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, pendingIntent);

Here's getNotificationNewRequestService():

private Notification getNotificationNewRequestService() {

        mBuilder =
                new NotificationCompat.Builder(getBaseContext())
                        .setSmallIcon(R.mipmap.app_icon_1)
                        .setContentTitle("Title")
                        .setContentText("text...");

        Intent resultIntent = new Intent(getBaseContext(), Profile.class);

        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        getBaseContext(),
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        // for action button
        Intent actionIntent = new Intent(getBaseContext(), MyBroadcastSender.class);
        actionIntent.putExtra("id", NotificationARBroadcastReceiver.m);
        PendingIntent actionPendingIntent = PendingIntent
                .getBroadcast(getBaseContext(),
                        0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(resultPendingIntent);
        mBuilder.addAction(R.drawable.ic_accepted_request_black_24dp, "Accept", actionPendingIntent);
        mBuilder.addAction(R.drawable.ic_close_black_24dp, "Reject", null);

        return mBuilder.build();
    }

Here's NotificationARBroadcastReceiver.java file:

public class NotificationARBroadcastReceiver extends BroadcastReceiver {

    public static String NOTIFICATION = "notification";
    public static NotificationManager mNotifyMgr;
    public static int m;

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

        m = (new Random()).nextInt(10000);
        Log.d("mMain", String.valueOf(m));

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

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        mNotifyMgr.notify(m, notification);

    }
}

Here's MyBroadcastSender.java file:

public class MyBroadcastSender extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Broadcast Received by MyBroadcastSender.", Toast.LENGTH_SHORT).show();

        int id = intent.getIntExtra("id", 1);

        // send back to your class
        Intent newIntent = new Intent();
        newIntent.setAction(context.getString(R.string.broadcast_id));
        newIntent.putExtra("id1", id);
        context.sendBroadcast(newIntent);
        context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
        Toast.makeText(context, "Broadcast sent back.", Toast.LENGTH_SHORT).show();

    }
}

and here's MyBroadcastReceiver.java file:

// BroadcastReceiver
    public class MyBroadcastReceiver extends BroadcastReceiver {

        public MyBroadcastReceiver(){
            super();
        }

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

            int id2 = intent.getIntExtra("id1", 1);

            if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) {

                NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);

                Intent intent1 = new Intent(MyService.this, MainActivity.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent1);

                Toast.makeText(context, "Broadcast received by MyBroadcastReceiver. Now, you can perform actions.",
                        Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(context, "Intent is null.", Toast.LENGTH_SHORT).show();
            }
        }
    }

In getNotificationNewRequestService(), I'm putting notification id as an extra in "id", then in MyBroadcastSender.java, I'm getting this extra as int id = intent.getIntExtra("id", 1); and then putting again as newIntent.putExtra("id1", id); and then finally getting it in MyBroadcastReceiver.java as int id2 = intent.getIntExtra("id1", 1); and trying to remove the notification using it as NotificationARBroadcastReceiver.mNotifyMgr.cancel(id2);.

Sorry for this much code, I've to upload it all as they all are necessary.

What I want is to know how to deliver the same notification id from NotificationARBroadcastReceiver.java (which is a separate java file) to MyBroadcastReceiver(which is an inner class in MyService.java)?

Update 1.0- this is what happened when I printed out the values of m, mMain, id, id1:

D/m: 0
D/mMain: 9994
D/id: 0
D/id1: 0
Hammad Nasir
  • 2,681
  • 6
  • 44
  • 120

2 Answers2

2

Assuming getNotificationService() == getNotificationNewRequestService() Looks like the NotificationARBroadcastReceiver isn't called before the notfication is built and displayed.

You would do better to generate the notification id where you create the notification and just add it to the intent there as well you don't need to make.

So call getNotificationNewRequestService() from NotificationARBroadcastReceiver.recieve() and make sure the notification ids match up.

siliconeagle
  • 6,869
  • 2
  • 26
  • 35
  • `You would do better to generate the notification id where you create the notification and just add it to the intent there as well you don't need to make.` what do you mean by this lines? Please write a bit code according to this. – Hammad Nasir Dec 13 '16 at 16:09
  • you are adding the id to the intent before its generated as i say before generate your id in `NotificationARBroadcastReceiver.recieve()` and pass it to `getNotificationNewRequestService(id)` then call `notify(id, notif)`. You problem is you are building the notification before you are generating the ID. – siliconeagle Dec 13 '16 at 16:39
  • the problem is that `getNotificationNewRequestService()` is in a `Service` and `NotificationARBroadcastReceiver` is a separate file. How can I pass the id generated in `NotificationARBroadcastReceiver.recieve()` to `getNotificationNewRequestService()` before calling `notify(id, notif)`? – Hammad Nasir Dec 13 '16 at 18:30
  • use an integer resource as an id. add `` in `integers.xml` file under `values` folder. You can also use a static variable to access it in another file. – rupinderjeet Dec 14 '16 at 06:26
  • @rupinderjeet the problem is that I am generating different notifications and for that I'm generating random ids using `m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);` If I'd use the same `id`, all the previous notifications would get cancelled. – Hammad Nasir Dec 14 '16 at 07:32
  • @siliconeagle please help further – Hammad Nasir Dec 14 '16 at 11:30
  • it should be clear where the error is from what i've said. I can't write your code for you sorry. If you have a question then ask away. – siliconeagle Dec 16 '16 at 17:19
2

Edit:

move:

m = (new Random()).nextInt(10000);

before:

actionIntent.putExtra("id", NotificationARBroadcastReceiver.m); // this will be 'm'

Result:

int m = (new Random()).nextInt(10000);
Intent actionIntent = new Intent(getBaseContext(), MyBroadcastSender.class);
actionIntent.putExtra("id", m);
Log.d(getClass().getSimpleName(), "Notification Id is : " + m);

then, you can check what values are in id, id1 and id2. Don't forget to call .notify() with same Id you got from m.

You can, also, create getRandomNotificationId() and getLastGeneratedNotificationId() methods. Whenever you generate an Id, store it in public static integer variable, so that you can access it throughout the class.

Problem might be that you are accessing m from NotificationARBroadcastReceiver before initializing it. So, it will definitely be 0. And, you mentioned something about println error, are you using System.out.println()?

Before Edit:

As seen on your new edit, try closing notification before starting it:

m = (...);

// some code here

mNotifyMgr.cancel(m);
mNotifyMgr.notify(m, notification);

and see if your issue gets resolved.

rupinderjeet
  • 2,597
  • 24
  • 44
  • this surely stopped the notification sound from getting played multiple times and notification appearing in the status bar multiple times, but it still printed out: `D/m: 1481727352 D/m: 1481727352 D/m: 1481727352 D/m: 1481727352 D/id: 1481727352 D/id1: 1481727352` and failed to remove the notification from status bar. – Hammad Nasir Dec 14 '16 at 14:57
  • no, bro... there is some confusion... the `int m = (new Random()).nextInt(10000);` is in `NotificationARBroadcastReceiver.java` which is a separate file where Notification is built and I have to use this same id in `MyBroadcastReceiver.java` (which is an inner class as described by you in your github example) to remove this notification on action button clicked. – Hammad Nasir Dec 15 '16 at 06:37
  • like SiliconEagle said, generate notificationId(m) where you create notification. and call `getNotificationNewRequestService()` from inside NotificationARBroadcastReceiver.recieve() – rupinderjeet Dec 15 '16 at 07:17
  • The question is how?? `getNotificationNewRequestService()` is in another file and `NotificationARBroadcastReceiver.java` is a separate file itself!! – Hammad Nasir Dec 15 '16 at 07:35
  • can i get my hands on your project, or create a minimal example project – rupinderjeet Dec 15 '16 at 08:00
  • what suits you best? – Hammad Nasir Dec 15 '16 at 11:18