2016-02-26 8 views
10

Was ist der richtige Weg, um der Benachrichtigung in API 23 eine Aktion hinzuzufügen, da addAction(int icon, CharSequence title, PendingIntent intent) veraltet ist? Konnte kein Beispiel finden, danke.Android-Benachrichtigung .addAction in API abgelehnt 23

Mein alter Aktion: .addAction(R.drawable.ic_prev, "Previous", prevPendingIntent)

+0

Nach den [docs] (http://developer.android.com/reference/android /app/Notification.Builder.html#addAction%28android.app.Notification.Action%29), sollten Sie 'addAction (Notification.Action action)' stattdessen verwenden. – Tigger

+0

http://StackOverflow.com/Questions/16852270/How-to-Implement-the-deprecated-methods-of-notification – sasikumar

+2

Ich sah schon das Beispiel @sasikumar, es hilft mir nicht, ich verstehe nicht Wie ich die 'Notification.Action'-Aktion erstellen kann –

Antwort

11

Statt diese:

addAction (int icon, CharSequence title, PendingIntent intent)

Diese Methode in API-Ebene weiterentwickelt wurde 23.

Verwendung:

addAction (Notification.Action action)

Es ist alles in der Entwickler-Dokumentation!

Also diese verwenden:

zuerst bauen Sie Ihre Aktion mit dem NotificationCompat.Action.Builder

NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_prev, "Previous", prevPendingIntent).build(); 

Hinweis: Verwenden NotificationCompat.Action

Und als fügen Sie Ihre Mitteilung:

yournotification.addAction(action); 
+0

Ich weiß, ich habe es bereits gelesen, aber ich verstehe nicht, wie man die' Notification.Action'-Aktion richtig erstellt –

+0

Ok, ich habe es für Sie bearbeitet – Strider

+0

Das sind falsche, inkompatible Typen , müssen Sie 'notification.Action.Builder()' in 'new Action()' und das Ergebnis: 'new Notification.Action (.....) 'ist immer noch veraltet oder umgekehrt, was immer noch veraltet ist. –

8

Verwenden Icon Klasse auf den ersten Parameter für Drawable wenn api Ebene> = 23 (Eibisch)

https://developer.android.com/reference/android/app/Notification.Action.Builder.html

https://developer.android.com/sdk/api_diff/23/changes/android.app.Notification.Action.Builder.html

Beispiel)

Notification.Action action = new Notification.Action.Builder(
    Icon.createWithResource(this, R.drawable.ic_prev), 
    "action string", 
    pendingIntent).build(); 
+0

alte Frage dreht, aber immer noch es :) gehen, es heute zu testen, und ich werde mit einer Antwort kommen, danke. –

2
//Exemple of notification with Button 
private void scheduleNotificationWithButton(String message) { 

    int notifReCode = 1; 

    //What happen when you will click on button 
    Intent intent = new Intent(this, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_ONE_SHOT); 

    //Button 
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Go", pendingIntent).build(); 

    //Notification 
    Notification notification = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentText("Back to Application ?") 
      .setContentTitle("Amazing news") 
      .addAction(action) //add buton 
      .build(); 

    //Send notification 
    NotificationManager notificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(1, notification); 
} 
1

Sie müssen nur Verwenden Sie den NotificationCompat.Builder-Builder anstelle des Notification.Builder-Builders, da Nein tificationCompat.Builder können Sie Ihre Anwendung unter Android Version 4.1

Gelöst von NotificationCompat.builder mit bauen:

String strTitle = getString(R.string.new_message); 
    String strText = getString(R.string.hi_whats_up); 
    Intent intent = new Intent(this, NotificationView.class); 
    intent.putExtra("title", strTitle); 
    intent.putExtra("text", strText); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setTicker("Notification Ticker") 
      .setContentTitle("Notification Title") 
      .setContentText("Notification Text") 
      .addAction(R.mipmap.ic_launcher, "Notification Action", pendingIntent) 
      .setContentIntent(pendingIntent) 
      .setAutoCancel(true); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
      notificationManager.notify(0, builder.build()); 
+0

Das funktioniert gut für mich. Scheint überflüssig für Android, so viele Möglichkeiten zu haben, eine dumme Benachrichtigung zu erstellen? – Thunderstick

Verwandte Themen