2016-06-17 13 views
1

geschlossen Ich möchte überprüfen, ob die Benachrichtigung vom Benutzer geschlossen wurde? Ist das möglich? Ich meine, wenn die Benachrichtigung durch Wischen geschlossen wird, möchte ich einen Wert auf DB setzen.Überprüfen Sie die Benachrichtigung in Android

Ich weiß, wie man überprüft, ob die Benachrichtigung aktiv ist, aber nicht sicher, wie man die Benachrichtigung durch Wischen geschlossen bekommen?

Wie geht das? Kann mir jemand helfen?

Danke!

+2

Hallo BroadcastReciver in manifest

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

5) Call-Methode hinzufügen lesen Sie in diesem Beitrag http://stackoverflow.com/ q/14671453/2445061 – ChrisCarneiro

+0

Es scheint, als ob Sie nach dem [löschen suchen Absicht] (https://developer.android.com/reference/android/app/Notification.html#deleteIntent) Feld der 'Notification' Klasse. – Lawrance

Antwort

2

Try This es Ihnen helfen können

1) Machen Verfahren zur Benachrichtigung senden

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) Machen BroadcastReciver zum Erfassen 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) Erstellen Sie Kündigungsabsicht meth od

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) für die Benachrichtigung

sendNotification("hello world",11); 
+0

Großartig. Vielen Dank! Wie überprüfe ich, ob auf eine Benachrichtigung geklickt wurde oder nicht? Ist das möglich? –