1

I want to check if notification was closed by user? Is this possible? I mean, if notification is closed by swiping then I would like set a value to DB.

I know how to check if the notification is active but not sure how to get the notification closed by swiping?

How to do it? Can somebody help me out?

Thanks!

Sanjana Nair
  • 2,663
  • 6
  • 26
  • 44
  • 2
    Hi check this post http://stackoverflow.com/q/14671453/2445061 – ChrisCarneiro Jun 17 '16 at 10:13
  • It seems like you are looking for the [deleteIntent](https://developer.android.com/reference/android/app/Notification.html#deleteIntent) field of the `Notification` class. – Dhruv Jun 17 '16 at 10:29

1 Answers1

3

Try This it may be help to you

1) Make Method for send Notification

public void sendNotification(String message, int notificationId) {
    Bundle b = new Bundle();
    Intent intent = new Intent(this, NotificationDismissedReceiver.class);
    intent.putExtra("com.stack.notificationId", notificationId);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(message)
            .setAutoCancel(true)
            .setDeleteIntent(createOnDismissedIntent(this, 11))
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    Notification note = notificationBuilder.build();
    note.defaults |= Notification.DEFAULT_VIBRATE;
    note.defaults |= Notification.DEFAULT_SOUND;

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

    notificationManager.notify(notificationId /* ID of notification */, notificationBuilder.build());
}

2) Make BroadcastReciver for detecting swipe Notification

public class NotificationDismissedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
  int notificationId = intent.getExtras().getInt("com.stack.notificationId");
  /* Your code to handle the event here */
  Log.d("TAG","GET "+notificationId);
}
}

3) Create dismiss intent method

private PendingIntent createOnDismissedIntent(Context context, int notificationId) {
    Intent intent = new Intent(context, NotificationDismissedReceiver.class);
    intent.putExtra("com.stack.notificationId", notificationId);

    PendingIntent pendingIntent =
            PendingIntent.getBroadcast(context.getApplicationContext(),
                    notificationId, intent, PendingIntent.FLAG_ONE_SHOT);
    return pendingIntent;
}

4) Add BroadcastReciver in manifest

   <receiver
        android:name=".NotificationDismissedReceiver"
        android:exported="false" >
    </receiver>

5) call method for Notification

sendNotification("hello world",11);
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38