Notification的使用

 public void Click(View view) {
        switch (view.getId()) {
            case R.id.text1://只显示一条通知
                count++;
                notification = new Notification.Builder(this).
                        setContentTitle("通知").
                        setContentText("点我。。。").
                        setTicker("新消息").//第一次提示消息的时候显示在通知栏上
                        setSmallIcon(R.mipmap.ic_launcher).
                        setAutoCancel(true).//点击后自动消失
                        setNumber(count).
                        build();
                //通知消息与intent关联
                intent = new Intent(MainActivity.this,MainActivity.class);
                intent.putExtra(DATA,"text1,只显示单条消息+"+count);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,10,intent,PendingIntent.FLAG_CANCEL_CURRENT);
                //设置通知意图
                notification.contentIntent = pendingIntent;
                notificationManager.notify(0,notification);
                break;
            case R.id.text2://显示多条通知,都跳转同一个活动
                count++;
                notification = new Notification.Builder(this).
                        setContentTitle("通知").
                        setContentText("点我。。。").
                        setTicker("新消息").//第一次提示消息的时候显示在通知栏上
                        setSmallIcon(R.mipmap.ic_launcher).
                        setAutoCancel(true).//点击后自动消失
                        setNumber(count).
                        build();
                //通知消息与intent关联
                intent = new Intent(MainActivity.this,MainActivity.class);
                intent.putExtra(DATA,"text1,只显示单条消息+"+count);
                PendingIntent p = PendingIntent.getActivity(MainActivity.this,10,intent,PendingIntent.FLAG_CANCEL_CURRENT);
                //设置通知意图
                notification.contentIntent = p;
                notificationManager.notify(count,notification);//不同的id会显示不同的通知
                break;
            case R.id.text4://显示自定义通知
                RemoteViews remoteViews = new RemoteViews(MainActivity.this.getPackageName(),R.layout.notifity_view);
                remoteViews.setImageViewResource(R.id.app_iv,R.mipmap.ic_launcher);
                intent = new Intent(MainActivity.this,MainActivity.class);
                intent.putExtra(DATA,"text4,自定义通知!");
                //包装意图
                PendingIntent i = PendingIntent.getActivity(MainActivity.this,100,intent,PendingIntent.FLAG_CANCEL_CURRENT);
                notification = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("通知")
                        .setAutoCancel(true).setTicker("自定义通知").setContentIntent(i).build();
                notification.bigContentView = remoteViews;
                notificationManager.notify(0,notification);
                break;
        }
    }

      实现通知的一般步骤:

    1、创建通知管理器

     2、构建通知

     3、包装意图

     4、调用notify方法发送通知

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NotificationAndroid 操作系统中的一个重要组件,可用于向用户发送各种提示信息。在 Android 应用程序中使用 Notification 的步骤如下: 1. 创建 Notification 对象 你可以通过 Notification.Builder 或者 NotificationCompat.Builder 对象来创建 Notification 对象,设置通知的标题、内容、图标等参数。 2. 发送 Notification 使用 NotificationManager 对象的 notify() 方法来发送 Notification。 3. 处理 Notification 的点击事件 如果用户点击了 Notification,你可以通过 PendingIntent 对象来启动一个 Activity 或者执行一个操作。 以下是一个简单的示例代码,用于创建和发送一个 Notification: ```java // 创建 Notification.Builder 对象 Notification.Builder builder = new Notification.Builder(this); builder.setContentTitle("标题"); builder.setContentText("内容"); builder.setSmallIcon(R.drawable.icon); // 发送 Notification NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, builder.build()); // 处理 Notification 的点击事件 Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); ``` 这样就可以发送一个简单的 Notification,并且处理用户的点击事件。需要注意的是,为了让应用程序发送 Notification,你需要在 AndroidManifest.xml 文件中添加相应的权限:`<uses-permission android:name="android.permission.VIBRATE" />` 和 `<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值