NotificationCompat.Builder:无法解析方法build() [英] NotificationCompat.Builder: Cannot resolve method build()

查看:285
本文介绍了NotificationCompat.Builder:无法解析方法build()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的类,我想用它来通知用户收到的消息(随着应用程序的发展,我将对其进行改进)。但是现在,在最后一行中,出现以下错误,我无法运行它:

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:


无法解析方法build()

Cannot resolve method build()

这是代码:

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 ?!

PS 目标(最低)sdk = 21

推荐答案

在支持V4及更高级别的API级别19
中,通知方法有所更改。您可以尝试以下代码块。

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

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

这篇关于NotificationCompat.Builder:无法解析方法build()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆