Android 如何使用NotificationCompat.Builder创建通知?

Android 如何使用NotificationCompat.Builder创建通知?,android,notifications,android-notifications,android-notification-bar,Android,Notifications,Android Notifications,Android Notification Bar,我需要创建一个简单的通知,这将显示在通知栏连同声音和图标,如果可能的话?我还需要它与Android2.2兼容,所以我发现NotificationCompat.Builder适用于4以上的所有API。如果有更好的解决方案,请随意提及。是在所有Android版本上创建通知的最简单方法。您甚至可以使用Android 4.1提供的功能。如果您的应用程序在Android>=4.1的设备上运行,则将使用新功能,如果在Android上运行,工作示例: Intent intent = new Inten

我需要创建一个简单的通知,这将显示在通知栏连同声音和图标,如果可能的话?我还需要它与Android2.2兼容,所以我发现NotificationCompat.Builder适用于4以上的所有API。如果有更好的解决方案,请随意提及。

是在所有Android版本上创建
通知的最简单方法。您甚至可以使用Android 4.1提供的功能。如果您的应用程序在Android>=4.1的设备上运行,则将使用新功能,如果在Android上运行,工作示例:

    Intent intent = new Intent(ctx, HomeActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder b = new NotificationCompat.Builder(ctx);

    b.setAutoCancel(true)
     .setDefaults(Notification.DEFAULT_ALL)
     .setWhen(System.currentTimeMillis())         
     .setSmallIcon(R.drawable.ic_launcher)
     .setTicker("Hearty365")            
     .setContentTitle("Default notification")
     .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
     .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
     .setContentIntent(contentIntent)
     .setContentInfo("Info");


    NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, b.build());
notify(String tag, int id, Notification notification)
notify(int id, Notification notification)

我用这种方法,效果很好。(在android 6.0.1中测试)

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)

您可以尝试以下代码这对我来说很好:

    NotificationCompat.Builder mBuilder= new NotificationCompat.Builder(this);

    Intent i = new Intent(noti.this, Xyz_activtiy.class);
    PendingIntent pendingIntent= PendingIntent.getActivity(this,0,i,0);

    mBuilder.setAutoCancel(true);
    mBuilder.setDefaults(NotificationCompat.DEFAULT_ALL);
    mBuilder.setWhen(20000);
    mBuilder.setTicker("Ticker");
    mBuilder.setContentInfo("Info");
    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setSmallIcon(R.drawable.home);
    mBuilder.setContentTitle("New notification title");
    mBuilder.setContentText("Notification text");
    mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

    NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(2,mBuilder.build());
notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
使用此代码

            Intent intent = new Intent(getApplicationContext(), SomeActvity.class);
            PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(),
                    (int) System.currentTimeMillis(), intent, 0);

            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(getApplicationContext())
                            .setSmallIcon(R.drawable.your_notification_icon)
                            .setContentTitle("Notification title")
                            .setContentText("Notification message!")
                            .setContentIntent(pIntent);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, mBuilder.build());
notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
深入通报 代码

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
深度知识 可以使用通知生成通知。生成器或NotificationCompat.Builder类。
但是,如果您想要向后兼容,您应该使用NotificationCompat.Builder类,因为它是v4支持库的一部分,因为它负责为API 4及更高版本提供一致的外观和通知功能

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
核心通知属性 通知有4个核心属性(3个基本显示属性+1个单击操作属性)

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
  • 小图标
  • 头衔
  • 正文
  • 按钮单击事件(点击通知时单击事件)
在Android 3.0及以上版本上,按钮点击事件是可选的。这意味着,如果minSdk的目标是Android 3.0或更高版本,则您可以仅使用显示属性生成通知。但是,如果您希望您的通知在比Android 3.0旧的设备上运行,则必须提供Click事件,否则您将看到IllegalArgumentException

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
通知显示 通过调用NotificationManager类的notify()方法显示通知

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
notify()参数 notify方法有两种变体

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
notify方法采用整数id来唯一标识通知。但是,您也可以提供可选的字符串标记,以便在发生冲突时进一步标识通知

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
这种类型的冲突很少见,但是您已经创建了一些库 其他开发人员正在使用您的库。现在他们创造了自己的 通知以及您的通知和其他开发人员的通知 通知id相同,则您将面临冲突

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
API 11之后的通知(更多控制) API 11提供了对通知行为的附加控制

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
  • 解雇通知
    默认情况下,如果用户点击通知,则会执行指定的单击事件,但不会清除通知。如果您希望在何时清除通知,则应添加此项

    notify(String tag, int id, Notification notification)
    
    notify(int id, Notification notification)
    
    mBuilder.setAutoClear(真)

  • notify(String tag, int id, Notification notification)
    
    notify(int id, Notification notification)
    
  • 防止用户解除通知
    用户也可以通过刷卡取消通知。通过在生成通知时添加此项,可以禁用此默认行为

    notify(String tag, int id, Notification notification)
    
    notify(int id, Notification notification)
    
    mBuilder.setconsuming(true)

  • notify(String tag, int id, Notification notification)
    
    notify(int id, Notification notification)
    
  • 通知定位
    您可以通过以下方式设置通知的相对优先级:

    notify(String tag, int id, Notification notification)
    
    notify(int id, Notification notification)
    
    mBuilder.setcontinuous(int pri)

  • notify(String tag, int id, Notification notification)
    
    notify(int id, Notification notification)
    
如果您的应用程序在低于11的API上运行,那么您的通知将在没有上述附加功能的情况下工作。这是选择NotificationCompat.Builder而不是Notification.Builder的优势

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
API 16之后的通知(更多信息) 随着API 16的引入,通知被赋予了许多新功能
通知可以提供更多信息。
你可以给你的标志添加一个大图片。假设您现在使用mBuilder.setLargeIcon(位图)收到某人的消息,您可以显示此人的照片。因此,在状态栏中,当您滚动时,您将看到图标,您将看到人物照片代替图标。 还有其他功能

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
  • 在通知中添加计数器
  • 当您第一次看到通知时,将显示“股票代码”消息
  • 可扩展通知
  • 多行通知等

发出通知的简单方法

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
 NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher) //icon
            .setContentTitle("Test") //tittle
            .setAutoCancel(true)//swipe for delete
            .setContentText("Hello Hello"); //content
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(1, builder.build()
    );

在android 8.0中显示通知

notify(String tag, int id, Notification notification)
notify(int id, Notification notification)
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

  public void show_Notification(){

    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
    String CHANNEL_ID="MYCHANNEL";
    NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"name",NotificationManager.IMPORTANCE_LOW);
    PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
    Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID)
            .setContentText("Heading")
            .setContentTitle("subheading")
            .setContentIntent(pendingIntent)
            .addAction(android.R.drawable.sym_action_chat,"Title",pendingIntent)
            .setChannelId(CHANNEL_ID)
            .setSmallIcon(android.R.drawable.sym_action_chat)
            .build();

    NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    notificationManager.notify(1,notification);


}

它会自动显示在通知栏中吗?那么如何添加声音呢?问题在于此NotificationManager NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_服务);notificationManager.notify(mId,mBuilder.build());它是通知的ID,以便您以后可以引用它来撤销或替换它。请在此处添加信息:在您的回答中-简而言之,您给出的方式将使ICS崩溃-它需要contentIntent。让我知道如何删除注释谢谢
您必须至少设置smallIcon、contentTitle和contentText。如果您错过了一条,通知将不会显示。
那一行解决了我的头疼问题。我不知道你必须设置所有这些。嗨,我已经尝试了这段代码,但我没有任何对NotificationCompat.Builder的引用。。。缺少什么吗?请检查以确保包含android support-v4兼容性库。您也可以尝试清理和重建项目。我有一个问题,我将图标设置为绿色,但当通知出现时,它会自动变为白色。@HUSNAINSARWAR,因为这发生在API>21(棒棒糖及以上)上。要解决此问题,请为
setSmallIcon(R.id.your_icon_small)
指定一个图标,并设置如下主图标
mBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.icon))
for API>21。当您想在android 8上显示它,并且当应用程序打开时,这将不起作用new NotificationCompat.Builder(ctx);在较新版本中不推荐使用,
.addAction
pendingent
作为单个参数获取,因此我们可以构建它