1

I have the following code, I want retrieve some data when user taps a notification. But I don't know how to deal with it. I know there is a way to pass the activity as an argument but depends of the content of the notification I should open different activitys.

This is the desire workflow

I publish a notification(with a key-value data) -> User taps notification -> BroadcastReceiver(retrieve key-value data) -> Activity

The problem is my BroadcastReceiver is not receiving data.

    Intent notificationIntent = new Intent(NOTIFICATION_SELECTED_ACTION);
    notificationIntent.putExtra("mykey","myvalue");
    PendingIntent intent = PendingIntent.getBroadcast(context, 0, notificationIntent, 0);
    context.getApplicationContext().registerReceiver(receiverOpen, new IntentFilter(NOTIFICATION_SELECTED_ACTION));

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    ...
    mBuilder.setContentIntent(intent);
    //I publish the notification in the notification bar
    notification = mBuilder.build();
    mNotificationManager.notify(id_push, notification);


   private final BroadcastReceiver receiverOpen = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        Intent i = new Intent(context.getApplicationContext(),MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        //Value is always null ffs
        String value = intent.getStringExtra("mykey");

        count = 0; // Do what you want here
        notificationList.clear();
        editor.clear();
        editor.commit();

        context.startActivity(i);
    }
};
Ricardo
  • 7,921
  • 14
  • 64
  • 111
  • When you press the notification. if you have value came from the notification then there shouldnt be any problem. in the mainActivity depending on the value you can open different different activities. – Im Batman Mar 13 '17 at 11:27

3 Answers3

0

As in your code you are using broadcast service. if you are receiveing push notification while app in foreground. you can use Broadcast service like below

            Intent intent = new Intent("myPush");
            intent.putExtra("message", messageBody);
            intent.putExtra("title", title);
            intent.putExtra("Id", id);
            getApplicationContext().sendBroadcast(intent);

and you can register broadcaster anywhere in your app. like below

@Override
    public void onResume() {
        super.onResume();
        getActivity().registerReceiver(mMessageReceiver, new IntentFilter("myPush"));
    }

so when push arrived you can use following method to fireup any method to go to preferred activity.

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            // Extract data included in the Intent
            String message = intent.getStringExtra("message");
            String title = intent.getStringExtra("title");
            final String orderId = intent.getStringExtra("Id");

            Log.e("Main Activity",title+" : "+message);

           //AnyIntent or Query

        }
    };

When app in Background.

            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("message", messageBody);
            intent.putExtra("title", title);
            intent.putExtra("Id", Id);

            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                    PendingIntent.FLAG_ONE_SHOT);

and handle that in Main Activity on onCreate method.

            Intent intent = getIntent();
            String receivedId = intent.getStringExtra("Id");

           if(receivedId != null){
             //Do Somthing as you want
           }
Im Batman
  • 1,842
  • 1
  • 31
  • 48
  • As I said, I want handle the app when user taps on notification. – Ricardo Mar 13 '17 at 11:45
  • @Ricardo i have updated the question. you have to use pending intent when you press the notification. and it will received to the mainActivity and you can handle it according to your needs – Im Batman Mar 13 '17 at 11:52
  • Nope, because the data is not arriving when user taps notification and the BroadcastReceiver is trigered. You should check my code. and read my comment: "Value is always null ffs". You think I have the data when user taps notification and it is wrong. – Ricardo Mar 13 '17 at 11:56
  • if you are getting null value.then there should be a problem with key value pair. might be you are reading wrong key name – Im Batman Mar 14 '17 at 03:27
0

According to this post https://stackoverflow.com/a/6752571/2139691 I should use PendingIntent.FLAG_CANCEL_CURRENT

PendingIntent intent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Community
  • 1
  • 1
Ricardo
  • 7,921
  • 14
  • 64
  • 111
-3

First you need to have value in Notification. Pass data from Notification activity to your intent activity through Intent. see eg.

 Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("point", "2");
    intent.putExtra("link", link);

Passing this from Notification to MainActivity, In MainActivity,

   if (getIntent().hasExtra("point")) {
        String link = getIntent().getStringExtra("link)

Using this 'link' I opened website. Thank You..!!

RoHiT
  • 372
  • 3
  • 12
  • This is not working since if you read my code You can see my comment "//Value is always null ffs" – Ricardo Mar 13 '17 at 11:43
  • Then instead of 'Intent notificationIntent = new Intent(NOTIFICATION_SELECTED_ACTION);' this try to use 'Intent i=new Intent(this,your_activity_name.class);' – RoHiT Mar 13 '17 at 12:12
  • Nooooo, I said I want to handle the app when user taps the notification. – Ricardo Mar 13 '17 at 12:53