2017-04-18 5 views
2

Ich habe die folgende einfache Klasse, die ich verwenden möchte, um Benutzer von eingehenden Nachrichten zu benachrichtigen (ich werde es entwickeln, wie die App geht). Aber jetzt, in der letzten Zeile, gibt es folgende Fehler und ich kann es nicht laufen:NotificationCompat.Builder: Kann die Methode build() nicht auflösen

Kann nicht lösen Methode Build()

Hier ist der Code:

import android.app.Activity; 
import android.app.NotificationManager; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.NotificationCompat; 

public class UserNotificationActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void triggerNoti() { 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("My notification") 
       .setContentText("Hello World!"); 

     NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(001, mBuilder.build()); 
    } 
} 

Ich habe das versucht solution aber macht keine Änderung

Was mache ich falsch?!

P.S.: Target (& min) = 21 sdk

Antwort

0

Benachrichtigungsmethode wird ein Bit in Unterstützung V4 und über API-Ebene 19 verändert. Sie können den folgenden Codeblock versuchen.

public void sendNotification(String message, String title, Intent intent, int not_id) { 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_ONE_SHOT); 
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notification; 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 
     notification 
       = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.app_icon) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
       .setContentTitle(title) 
       .setPriority(NotificationCompat.PRIORITY_HIGH) 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

    } else { 
     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.app_icon); 
     notification 
       = new NotificationCompat.Builder(this) 
       .setContentTitle(title) 
       .setSmallIcon(R.drawable.small_icon) 
       .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) 
       .setContentText(message) 
       .setAutoCancel(true) 
       //.setColor(Color.parseColor("#1a4994")) 
       .setPriority(NotificationCompat.PRIORITY_HIGH) 
       .setLargeIcon(bitmap) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 
    } 
    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(not_id, notification.build()); 
} 

Update für NotificationChannel:

public void initChannels(Context context) { 
    if (Build.VERSION.SDK_INT < 26) { 
     return; 
    } 
    NotificationManager notificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    NotificationChannel channel = new NotificationChannel("default", 
                  "Channel name", 
                  NotificationManager.IMPORTANCE_DEFAULT); 
    channel.setDescription("Channel description"); 
    notificationManager.createNotificationChannel(channel); 
} 
+0

Vielen Dank für Ihre Lösung, aber leider nicht ganz mein Dilemma lösen, wie noch habe ich den gleichen Fehler !! Derselbe Fehler erscheint in der letzten Zeile –

+0

Überprüfen Sie Ihren Import: import android.support.v4.app.NotificationCompat; – ADM

+0

es ist bereits importiert .. –

Verwandte Themen