NotificationCompat.Builder: Cannot resolve method build()

杀马特。学长 韩版系。学妹 提交于 2020-01-14 12:09:43

问题


I have the following simple class that I would like to use to notify user of incoming messages (I will evolve it as the app goes). But for now, in the last line, there is the following error and I can't run it:

Cannot resolve method build()

Here is the 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());
    }
}

I've tried this solution but doesn't make any change

What am I doing wrong ?!

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


回答1:


Notification method is changed a bit in support V4 and above API level 19 . You can try the below code block.

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 for 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);
}



回答2:


private void createNotification(){
    Intent intent = new Intent(this,NotificationReceive.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,(int) System.currentTimeMillis(),intent,0);
    Notification notification = new Notification.Builder(this)
            .setContentTitle("Message");
        .setContentText("this is a tittle")
        .setSmallIcon(R.drawable.schollo)
        .setContentIntent(pendingIntent);
        .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notification.flags = notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0,notification);
}


来源:https://stackoverflow.com/questions/43472415/notificationcompat-builder-cannot-resolve-method-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!