2017-10-04 4 views
2

J'essaie de générer une Notification avec Target API 26 et min API 19. Je ne peux pas obtenir le Constructeur NotificationCompat.Builder qui prend l'ID de canal comme un 2ème argument .Impossible d'obtenir le Constructeur NotificationCompat.Builder 2 arg

Ceci est ma classe de notification jusqu'à présent. Tout en bas, je veux obtenir le générateur de notifications.

public class NotificationHelper extends ContextWrapper { 
private NotificationManager notificationManager; 

public NotificationHelper(Context base) { 
    super(base); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
     createChannel(); 
    } 
} 

@TargetApi(Build.VERSION_CODES.O) 
public void createChannel() { 
    NotificationChannel channel1 = new NotificationChannel("channel1", "Channel one", NotificationManager.IMPORTANCE_DEFAULT); 
    channel1.enableLights(true); 
    channel1.enableVibration(true); 
    channel1.setLightColor(R.color.colorPrimary); 
    channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); 

    getManager().createNotificationChannel(channel1); 
} 

public NotificationManager getManager() { 
    if (notificationManager == null) { 
     notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    } 
    return notificationManager; 
} 

public NotificationCompat.Builder() { 
    NotificationCompat.Builder notificationBuilder = 
      new NotificationCompat.Builder(getApplicationContext(), "channel1"); 
} 

}

+0

Publiez ici votre fichier 'build.gradle' s'il vous plaît. –

+0

J'essaie mais ne forme pas correctement. Que cherchons-nous? –

+0

Vous devez définir les bibliothèques de support à 26. + afin d'avoir le nouveau constructeur. –

Répondre

1

Vous devez définir soutien Google libs à 26.1.0 ou plus et vérifier votre build.gradle. Il devrait être comme:

apply plugin: 'com.android.application' 
//... 
repositories { 
    maven { url 'https://maven.google.com' } 
//.. 
} 

android { 
//... 
} 
+1

Merci. Je veux juste mentionner que 26.0.1 fonctionne aussi. –