0

I have a notification service in which a notification is created in start method of the service..the intent is directed towards actvityA...I am not clear about the activity nd service binding process..in activityA I want a method to execute that checks the notification and on successful execution of that method..the notification is cancelled..I have red many posts which say notification control isnt possible..Plz help with some to check notification recieving..Thanks in advance..

nm =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        Log.d("nm", "nm created");
                        Intent in=new Intent(UpdateService.this,ActivityA.class);
TaskStackBuilder.create(UpdateService.this);

                        stackBuilder.addParentStack(ActivityA.class);

                        stackBuilder.addNextIntent(in);

                        PendingIntent pi =stackBuilder.getPendingIntent( 0,PendingIntent.FLAG_UPDATE_CURRENT);
                        notification =new NotificationCompat.Builder(UpdateService.this)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setContentTitle("Some Update")
                                .setContentText("Question");
                        notification.setContentIntent(pi);
                        notification.setAutoCancel(true);

                        int id=100;
                        int numMessages = 0;
                         notification.setContentText("You have unread messages..")
                            .setNumber(++numMessages);
                        nm.notify(id, notification.build());
                        stopSelf();

.i for got to mention..there is a dialog..that appears..On the press of positive button..notification mst get cleared...On negative..dialog will be dismissed..nd notification will remain..

AndroidMech
  • 1,676
  • 3
  • 20
  • 31

1 Answers1

1

You'll have to keep record of the notification ID. And then in your method, cancel the notification with that ID.

On How to cancel the notification, here's the link

Add this code where you set notification.

SharedPreferences prefs = this.getSharedPreferences(
      "com.example.app", Context.MODE_PRIVATE);
prefs.edit().putInt("ID", id).commit();

Add this to your onClick listener which is called when you press YES.

SharedPreferences prefs = this.getSharedPreferences(
          "com.example.app", Context.MODE_PRIVATE);
int notificationId = prefs.getInt("ID");

And now using this notificationId, canel the notification.

Community
  • 1
  • 1
Tejas
  • 585
  • 3
  • 15
  • Yup that can be done..but my notification id is in the Service...while i want to cancel that notification after it is accepted...i for got to mention..there is a dialog..that appears..On the press of positive button..notification mst get cleared...On negative..dialog will be dismissed..nd notification will remain.. – AndroidMech Nov 21 '13 at 05:55
  • Store the notification ID in SharedPreferences so that you can access it in your activity(dialog). – Tejas Nov 21 '13 at 05:57