安卓消息通知栏

  1. package jason.notification;
  2. import android.app.Activity;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.Menu;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. public class MainActivity extends Activity {
  14.        Button button;int id = 0;
  15.        @Override
  16.        protected void onCreate(BundlesavedInstanceState) {
  17.              super.onCreate(savedInstanceState);
  18.              setContentView(R.layout.activity_main);
  19.               button = (Button)findViewById(R.id.notify);
  20.               button.setOnClickListener(newOnClickListener() {
  21.                     
  22.                     @Override
  23.                      publicvoid onClick(View arg0) {
  24.                            //获得通知管理器
  25.                            NotificationManager manager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  26.                            //构建一个通知对象(需要传递的参数有三个,分别是图标,标题和 时间)
  27.                            Notification notification = newNotification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());
  28.                            Intent intent = newIntent(MainActivity.this,MainActivity.class);
  29.                            PendingIntent pendingIntent =PendingIntent.getActivity(MainActivity.this,0,intent,0);                                                         notification.setLatestEventInfo(getApplicationContext(),"通知标题", "通知显示的内容", pendingIntent);
  30.                            notification.flags =Notification.FLAG_AUTO_CANCEL;//点击后自动消失
  31.                            notification.defaults =Notification.DEFAULT_SOUND;//声音默认
  32.                            manager.notify(id,notification);//发动通知,id由自己指定,每一个Notification对应的唯一标志
  33.                            //其实这里的id没有必要设置,只是为了下面要用到它才进行了设置
  34.                     }
  35.               });
  36.        }
  37. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android通知是用来显示推送消息的重要组件之一。要在Android应用中实现通知推送消息,你需要进行以下步骤: 1. 创建通知渠道:从Android 8.0(API级别26)开始,你需要创建通知渠道来管理和组织通知。使用NotificationChannel类来创建通知渠道,并设置其名称、描述和重要性级别等参数。 2. 构建通知内容:使用NotificationCompat.Builder类来构建通知的内容,包括标题、文本、图标、大图等。 3. 设置点击行为:可以为通知设置点击行为,比如打开应用的某个界面或执行特定的操作。使用PendingIntent类来定义点击通知时要执行的动作。 4. 发送通知:通过NotificationManager类的notify()方法发送通知。指定一个唯一的通知ID以及之前创建的NotificationCompat.Builder对象。 下面是一个示例代码,演示了如何发送一个简单的通知: ```java // 创建通知渠道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Channel Description"); // 在NotificationManager中创建通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // 构建通知内容 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Notification Title") .setContentText("Notification Text") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) // 设置点击行为 .setAutoCancel(true); // 点击后自动取消通知 // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春哥111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值